一个测试数据能AC但是能被WA掉的代码

回复帖子

@Rika 2018-06-13 22:57 回复
n = int(input())
ipt = [[int(_) for _ in input().split()] for i in range(n)]
for i in range(n):
    for j in range(n):
        if ipt[i][j] == 0:
            a = [idx for idx, elem in enumerate(ipt[i]) if elem == 1]
            b = [idx for idx, elem in enumerate([k[j] for k in ipt]) if elem == 1]
            if len(a) > 1 and len(b) > 1 and min(a) < j < max(a) and min(b) < i < max(b):
                ipt[i][j] = 2
        print(ipt[i][j], end=' ')
    print()

原理:如果一个0上下左右都有1(不限距离)则认为是圈内的。(是错误的)

WA用:

8
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1
1 0 1 0 0 0 0 0
1 0 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1
@nothingness 2018-06-14 07:46 回复

我建议用这样的思路:正方形最外一圈都进行一次广搜,如果发现0就变成2,这样做以后不被1所包围的0都变成了2.然后输出$2-map[i][j]$,即如果是内圈的0就变2,是1不变,外圈的2变0