結果
| 問題 |
No.5022 XOR Printer
|
| コンテスト | |
| ユーザー |
prussian_coder
|
| 提出日時 | 2025-07-24 13:07:39 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 92 ms / 2,000 ms |
| コード長 | 2,258 bytes |
| コンパイル時間 | 428 ms |
| コンパイル使用メモリ | 82,584 KB |
| 実行使用メモリ | 79,820 KB |
| スコア | 4,585,080,131 |
| 最終ジャッジ日時 | 2025-07-26 12:43:42 |
| 合計ジャッジ時間 | 6,856 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
# 貪欲解法
# 操作Wを実施することでスコアが上がるセルのうち最も近いセルを選び、操作Wをする
# 操作Wを実施することでスコアが上がるセルが存在しない場合は、ランダムに1つのセルを選び移動して、操作Cをする
from __future__ import annotations
import random
import sys
from typing import List, Tuple
Cell = Tuple[int, int]
def move_commands(start: Cell, end: Cell) -> List[str]:
cmds: List[str] = []
x, y = start
tx, ty = end
while x < tx:
cmds.append("D")
x += 1
while x > tx:
cmds.append("U")
x -= 1
while y < ty:
cmds.append("R")
y += 1
while y > ty:
cmds.append("L")
y -= 1
return cmds
def main() -> None:
n, t = map(int, sys.stdin.readline().split())
board = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
x, y = 0, 0
s_val = 0
ops: List[str] = []
rng = random.Random(0)
while len(ops) < t:
best_cell: Cell | None = None
best_dist = 10**9
for i in range(n):
for j in range(n):
if (board[i][j] ^ s_val) > board[i][j]:
d = abs(x - i) + abs(y - j)
if d < best_dist:
best_dist = d
best_cell = (i, j)
if best_cell is not None:
path = move_commands((x, y), best_cell)
if len(ops) + len(path) + 1 > t:
break
ops.extend(path)
x, y = best_cell
ops.append("W")
board[x][y] ^= s_val
else:
rand_cell = (rng.randrange(n), rng.randrange(n))
# 最初の操作の場合は19bit目が1であるセルに移動して操作Cをする
if len(ops) == 0 and (board[rand_cell[0]][rand_cell[1]] >> 19) & 1 == 0:
continue
path = move_commands((x, y), rand_cell)
if len(ops) + len(path) + 1 > t:
break
ops.extend(path)
x, y = rand_cell
ops.append("C")
s_val ^= board[x][y]
sys.stdout.write("\n".join(ops[:t]) + "\n")
if __name__ == "__main__":
main()
prussian_coder