結果
問題 | No.424 立体迷路 |
ユーザー |
|
提出日時 | 2016-09-22 22:45:12 |
言語 | Python2 (2.7.18) |
結果 |
AC
|
実行時間 | 33 ms / 2,000 ms |
コード長 | 1,106 bytes |
コンパイル時間 | 476 ms |
コンパイル使用メモリ | 7,040 KB |
実行使用メモリ | 6,912 KB |
最終ジャッジ日時 | 2024-07-05 07:02:20 |
合計ジャッジ時間 | 1,643 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
H, W = map(int, raw_input().split())sx, sy, gx, gy = map(int, raw_input().split())goal = (gx-1, gy-1)board = [map(int, list(raw_input())) for _ in xrange(H)]dh = [1, -1, 0, 0]dw = [0, 0, 1, -1]stack = [(sx-1, sy-1)]visited = set()while len(stack) > 0:node = stack.pop()if node == goal:print 'YES'exit()if node in visited:continuevisited.add(node)for i in xrange(4):nh, nw = node[0]+dh[i], node[1]+dw[i]if nh < 0 or nh >= H or nw < 0 or nw >= W:continueif (nh, nw) in visited:continueif abs(board[nh][nw] - board[node[0]][node[1]]) > 1:continuestack.append((nh, nw))for i in xrange(4):nh, nw = node[0]+dh[i]*2, node[1]+dw[i]*2if nh < 0 or nh >= H or nw < 0 or nw >= W:continueif (nh, nw) in visited:continuea, b, c = board[node[0]][node[1]], board[nh][nw], board[node[0]+dh[i]][node[1]+dw[i]]if a != b or c >= a or c >= b:continuestack.append((nh, nw))print 'NO'