結果
問題 |
No.197 手品
|
ユーザー |
![]() |
提出日時 | 2025-06-09 21:01:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 48 ms / 1,000 ms |
コード長 | 1,377 bytes |
コンパイル時間 | 500 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 54,016 KB |
最終ジャッジ日時 | 2025-06-09 21:01:54 |
合計ジャッジ時間 | 4,708 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 43 |
ソースコード
from collections import deque before = input() N = int(input()) after = input() if before.count('o') != after.count('o'): exit(print('SUCCESS')) # これ以降 o の個数は同じ if before == 'ooo' or before == 'xxx': exit(print('FAILURE')) # o1つかo2つのパターンのみ if (before == 'oxo' or before == 'xox') and (before == after) and N == 1: exit(print('SUCCESS')) # 最初のマスで時間稼ぎ可能 def convert(s:str): res = 0 for i in range(3): if s[i] == 'o': res += 1<<(2 - i) return res def revert(n:int): memo = format(n, 'b').zfill(3) res = [] for i in range(3): if memo[i] == '1': res.append('o') else: res.append('x') return res dist = [10]*8 start = convert(before) goal = convert(after) dist[start] = 0 que = deque([start]) while que: pos = que.popleft() # 頂点番号から文字列 memo = revert(pos) # 2方向への遷移を検討 memo1 = [memo[1], memo[0], memo[2]] nex1 = convert(''.join(memo1)) if dist[nex1] == 10: dist[nex1] = dist[pos] + 1 que.append(nex1) memo2 = [memo[0], memo[2], memo[1]] nex2 = convert(''.join(memo2)) if dist[nex2] == 10: dist[nex2] = dist[pos] + 1 que.append(nex2) if N < dist[goal]: print('SUCCESS') else: print('FAILURE')