Codechef ORMATRIX

时间:2018-09-27 22:13:52

标签: python python-2.7

为什么这会返回WA?在尝试了一些我自己的测试用例之后,我尝试检查其他解决方案,但我无法提出失败的案例。另外,如果有任何优化的可能,请提出建议。

问题链接:https://www.codechef.com/COOK96B/problems/ORMATRIX

def findans(m, n, grid):
    same=0
    for i in range(m):
        if grid[i].count('0')==len(grid):
            same+=1
    if same==m:
        grid=[['-1' for k in range(n)] for j in range(m)]
        return grid
    else:
        row=[0 for i in range(m)]
        col=[0 for k in range(n)]
        for i in range(m):
            for j in range(n):
                if grid[i][j]=='1':
                    row[i]=1
                    col[j]=1

        for i in range(m):
            for j in range(n):
                if grid[i][j]=='0':
                    if row[i]==1 or col[j]==1:
                        grid[i][j]='1'
                    else:
                        grid[i][j]='2'
                else:
                    grid[i][j]='0'
        return grid



tcases=int(raw_input())
for i in range(tcases):
    m, n = map(int, raw_input().split(' '))
    grid=[[0 for k in range(n)] for j in range(m)]
    for i in range(m):
        grid[i]=list(raw_input())
    grid=findans(m, n, grid)
    for i in range(m):
        print " ".join(l for l in grid[i])

0 个答案:

没有答案