結果
問題 | No.1588 Connection |
ユーザー |
|
提出日時 | 2021-07-09 14:28:37 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 105 ms / 2,000 ms |
コード長 | 1,744 bytes |
コンパイル時間 | 2,304 ms |
コンパイル使用メモリ | 196,628 KB |
実行使用メモリ | 25,220 KB |
平均クエリ数 | 551.19 |
最終ジャッジ日時 | 2024-07-17 12:57:19 |
合計ジャッジ時間 | 5,523 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 |
コンパイルメッセージ
main.cpp: In function 'bool bfs()': main.cpp:32:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 32 | auto [x, y] = q.front(); | ^
ソースコード
#pragma GCC optimize("Ofast", "unroll-loops") #include <bits/stdc++.h> using namespace std; int N, M; void readNM(void){ cin >> N >> M; } bool isBlack(int i, int j){ static map<pair<int, int>, int> memo; // 1: 白, 2: 黒 static int black_found = 2; if (i == 0 && j == 0) return true; if (i == N - 1 && j == N - 1) return true; int tmp = memo[{i, j}]; if (tmp > 0) return (tmp == 2); if (black_found == M) return false; cout << (i + 1) << " " << (j + 1) << endl; string res; cin >> res; assert(res != "-1"); memo[{i, j}] = res == "Black" ? 2 : 1; if (res == "Black") ++black_found; return res == "Black"; } bool bfs(void){ queue<pair<int, int>> q; vector<vector<int>> visited(N, vector<int>(N, 0)); q.emplace(0, 0); while (!q.empty()){ auto [x, y] = q.front(); q.pop(); // 上 if (x > 0 && !visited[x - 1][y] && isBlack(x - 1, y)){ visited[x - 1][y] = 1; q.emplace(x - 1, y); } // 下 if (x < N - 1 && !visited[x + 1][y] && isBlack(x + 1, y)){ if (x == N - 2 && y == N - 1) return true; visited[x + 1][y] = 1; q.emplace(x + 1, y); } // 右 if (y < N - 1 && !visited[x][y + 1] && isBlack(x, y + 1)){ if (x == N - 1 && y == N - 2) return true; visited[x][y + 1] = 1; q.emplace(x, y + 1); } // 左 if (y > 0 && !visited[x][y - 1] && isBlack(x, y - 1)){ visited[x][y - 1] = 1; q.emplace(x, y - 1); } } return false; } int main(void){ readNM(); if (bfs()) cout << "Yes" << endl; else cout << "No" << endl; return 0; }