結果
| 問題 |
No.1588 Connection
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2022-05-08 16:29:00 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 80 ms / 2,000 ms |
| コード長 | 1,905 bytes |
| コンパイル時間 | 264 ms |
| コンパイル使用メモリ | 6,688 KB |
| 実行使用メモリ | 29,760 KB |
| 平均クエリ数 | 372.81 |
| 最終ジャッジ日時 | 2024-07-08 04:52:19 |
| 合計ジャッジ時間 | 3,306 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 31 |
ソースコード
local mfl, mce = math.floor, math.ceil
local function query(str)
io.write(str .. "\n")
io.flush()
local ret = io.read("*l")
return ret
end
local n, m = io.read("*n", "*n", "*l")
local t = {}
for i = 1, 4 * n * n do
t[i] = false
end
local map = {}
-- 0:unknown, 1:black, 2:white
for i = 1, n * n do
map[i] = 0
end
map[1], map[n * n] = 1, 1
local function checkmap(row, col)
local mapidx = (row - 1) * n + col
if map[mapidx] == 0 then
local ans = query(row .. " " .. col)
if ans == "Black" then
map[mapidx] = 1
else
map[mapidx] = 2
end
end
end
local pos = 1
while true do
-- print(pos)
if t[pos] then
io.write("No\n")
io.flush()
os.exit()
end
t[pos] = true
local way = mce(pos / n / n)
local mapidx = pos - (way - 1) * n * n
if mapidx == n * n then
io.write("Yes\n")
io.flush()
os.exit()
end
local row = mce(mapidx / n)
local col = mapidx - (row - 1) * n
if way == 1 then
-- up
if 1 < row then
checkmap(row - 1, col)
end
if 1 < row and map[mapidx - n] == 1 then
pos = 3 * n * n + (row - 2) * n + col
else
pos = 1 * n * n + (row - 1) * n + col
end
elseif way == 2 then
-- right
if col < n then
checkmap(row, col + 1)
end
if col < n and map[mapidx + 1] == 1 then
pos = 0 * n * n + (row - 1) * n + col + 1
else
pos = 2 * n * n + (row - 1) * n + col
end
elseif way == 3 then
-- down
if row < n then
checkmap(row + 1, col)
end
if row < n and map[mapidx + n] == 1 then
pos = 1 * n * n + row * n + col
else
pos = 3 * n * n + (row - 1) * n + col
end
else
-- left
if 1 < col then
checkmap(row, col - 1)
end
if 1 < col and map[mapidx - 1] == 1 then
pos = 2 * n * n + (row - 1) * n + col - 1
else
pos = 0 * n * n + (row - 1) * n + col
end
end
end