結果

問題 No.1588 Connection
ユーザー norikamenorikame
提出日時 2021-07-09 11:30:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,398 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 210,628 KB
実行使用メモリ 24,372 KB
平均クエリ数 551.25
最終ジャッジ日時 2023-09-24 11:59:56
合計ジャッジ時間 5,445 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,976 KB
testcase_01 AC 26 ms
23,904 KB
testcase_02 AC 26 ms
23,964 KB
testcase_03 AC 26 ms
23,592 KB
testcase_04 AC 26 ms
23,964 KB
testcase_05 AC 25 ms
23,448 KB
testcase_06 AC 27 ms
23,472 KB
testcase_07 AC 25 ms
24,300 KB
testcase_08 AC 29 ms
24,372 KB
testcase_09 AC 29 ms
23,868 KB
testcase_10 AC 29 ms
24,336 KB
testcase_11 AC 29 ms
23,316 KB
testcase_12 AC 59 ms
23,412 KB
testcase_13 AC 65 ms
23,856 KB
testcase_14 AC 24 ms
23,964 KB
testcase_15 AC 27 ms
23,436 KB
testcase_16 AC 26 ms
23,988 KB
testcase_17 AC 26 ms
23,556 KB
testcase_18 AC 27 ms
23,964 KB
testcase_19 AC 27 ms
23,592 KB
testcase_20 AC 29 ms
23,964 KB
testcase_21 AC 109 ms
23,904 KB
testcase_22 AC 108 ms
23,736 KB
testcase_23 AC 59 ms
23,676 KB
testcase_24 AC 44 ms
23,580 KB
testcase_25 AC 70 ms
23,304 KB
testcase_26 AC 69 ms
23,544 KB
testcase_27 AC 44 ms
23,700 KB
testcase_28 AC 39 ms
23,304 KB
testcase_29 AC 102 ms
23,436 KB
testcase_30 AC 108 ms
24,120 KB
testcase_31 AC 26 ms
23,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const int INF = (int)(1e9);
const vector<int> dy = { -1, 0, 1, 0 }, dx = { 0, 1, 0, -1 };

int query(int a, int b, int& qcnt) {
    if (qcnt >= 3000) return -1;
    ++a; ++b; ++qcnt;
    cout << a << " " << b << endl;
    string s;
    cin >> s;
    if (s == (string)"Black") return 1;
    else if (s == (string)"White") return 0;
    else if (s == (string)"-1") return -1;
    else return -1;
}

int main() {
    int n, m;
    cin >> n >> m;
    cin.ignore();
    vector<vector<int>> b(n, vector<int>(n, -1));
    b[0][0] = b[n-1][n-1] = 1;
    queue<pair<int, int>> que;
    vector<vector<int>> dist(n, vector<int>(n, INF));
    int mcnt = 1, qcnt = 0;
    bool ok = false;
    que.emplace(0, 0);
    dist[0][0] = 0;
    while (!que.empty()) {
        auto p = que.front(); que.pop();
        int y = p.first, x = p.second, d = dist[y][x];
        rep(i1, 4) {
            int ny = y + dy[i1], nx = x + dx[i1];
            if (ny<0 || ny>=n || nx<0 || nx>=n) continue;
            if (ny==n-1 && nx==n-1) {
                ok = true;
                break;
            }
            if (dist[ny][nx] <= d+1) continue;
            if (b[ny][nx] == -1) {
                if (mcnt >= m) {
                    if (ok) cout << "Yes" << endl;
                    else cout << "No" << endl;
                    return 0;
                }
                int qres = query(ny, nx, qcnt);
                if (qres == -1) {
                    if (ok) cout << "Yes" << endl;
                    else cout << "No" << endl;
                    return 0;
                }
                b[ny][nx] = qres;
            }
            if (b[ny][nx] == 0) continue;
            if (b[ny][nx] == 1) {
                que.emplace(ny, nx);
                dist[ny][nx] = d + 1;
                ++mcnt;
            }
        }
        if (ok || mcnt>=m || qcnt>=3000) {
            if (ok) cout << "Yes" << endl;
            else cout << "No" << endl;
            return 0;
        }
    }
    if (ok) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}
0