結果
問題 | No.228 ゆきこちゃんの 15 パズル |
ユーザー |
|
提出日時 | 2015-06-25 13:45:11 |
言語 | Python2 (2.7.18) |
結果 |
AC
|
実行時間 | 10 ms / 5,000 ms |
コード長 | 1,236 bytes |
コンパイル時間 | 97 ms |
コンパイル使用メモリ | 7,040 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-07 17:33:04 |
合計ジャッジ時間 | 918 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
# -*- coding:utf-8 -*- def solve(): global board,ans,isMoved for i in xrange(4): if 0 in board[i]: y = i x = board[i].index(0) if x + 1 < 4: if board[y][x+1] == ans[y][x] and isMoved[y][x+1] == False: board[y][x],board[y][x+1] = board[y][x+1],board[y][x] isMoved[y][x+1] = True return True if x - 1 >= 0: if board[y][x-1] == ans[y][x]and isMoved[y][x-1] == False: board[y][x],board[y][x-1] = board[y][x-1],board[y][x] isMoved[y][x-1] = True return True if y + 1 < 4: if board[y+1][x] == ans[y][x]and isMoved[y+1][x] == False: board[y][x],board[y+1][x] = board[y+1][x],board[y][x] isMoved[y+1][x] = True return True if y - 1 >= 0: if board[y-1][x] == ans[y][x]and isMoved[y-1][x] == False: board[y][x],board[y-1][x] = board[y-1][x],board[y][x] isMoved[y-1][x] = True return True return False if __name__ == "__main__": ans = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,0]] isMoved = [[False for i in xrange(4)]for i in xrange(4)] board = [[]for i in xrange(4)] for i in xrange(4): board[i] = map(int,raw_input().split()) i = 0 while board != ans and i < 16: if solve() == False: print "No" exit() i += 1 if ans == board: print "Yes" else: print "No"