SPOJ:运行时错误(NZEC)

mar*_*oxe 1 python

我试图在spoj.com 上做这个问题,但我不断收到错误运行时错误(NZEC).我在python中编码.我不知道为什么.这是我的代码,

import sys

def unique(lines, exact=True):
    for L in lines:
        if L.count('#') != 1 and exact:
            return False
        if L.count('#') > 1 and not exact:
            return False
    return True

def resolve(N, lines):

    diags = [ 
             [lines[i][i+j] for i in range(max(0, -j), min(N, N-j))]
             for j in range(-N+1, N)]
    anti_diags = [ 
             [lines[i][N-1 -(i+j)] for i in range(max(0, -j), min(N, N-j))]
             for j in range(-N+1, N)]
    if unique(lines) and unique(zip(*lines)) and unique(diags, False) and unique(anti_diags, False):
        return "YES"
    return "NO" 



input_file = sys.stdin
output_file = sys.stdout

T = int(raw_input())
for i in range(1, T + 1):

    n = int(raw_input())

    lines = []
    for _ in range(n):
        line = raw_input().strip()
        lines.append(list(line))

        print resolve(n, lines)
Run Code Online (Sandbox Code Playgroud)

它在本地工作正常,输入如下:

2
3
..# 
#..
.#.
4
.#..
...#
#...
..#.
Run Code Online (Sandbox Code Playgroud)

kad*_*adu 5

当我运行你的代码时,我得到以下内容:

$ python spoj.py < input
Traceback (most recent call last):
  File "spoj.py", line 38, in <module>
    print resolve(n, lines)
  File "spoj.py", line 15, in resolve
    for j in range(-N+1, N)]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

那是你得到的运行时错误.但问题实际上在这部分:

for _ in range(n):
    line = raw_input().strip()
    lines.append(list(line))

    print resolve(n, lines)
Run Code Online (Sandbox Code Playgroud)

由于print语句是缩进的,因此程序读取一行,在读取以下行之前追加到行并调用resolve.删除额外的缩进后,它在我的计算机上正常工作.

在风格和逻辑方面,还有足够的空间来进一步改进这个项目.如果您想要进一步的建议,请告诉我.

编辑:

您在评论中提到此缩进问题是一个copypasting错误.在这种情况下,正如sp1r在他的评论中指出的那样,最有可能的是,正如问题页面中的评论所暗示的那样,在线评委使用的输入格式不正确.

在这种情况下,您必须对输入进行更严格的解析,因为它可能没有像示例输入所示那样格式化.也就是说,而不是:

 2
 3
 ..# 
 #..
 .#.
 (etc)
Run Code Online (Sandbox Code Playgroud)

你可以有类似的东西:

2 3
..# 
#.. .#.
(etc)
Run Code Online (Sandbox Code Playgroud)

这可能会破坏您的代码,因为它假设发布问题的人有足够的能力做出看起来像示例的输入.在SPOJ中情况并非总是如此,许多输入文件格式不正确,您可以找到其他情况,其中格式错误的输入会导致正确的程序失败.