AC代码

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
const int N = 120;
int t, n;
int a[N][N];
int vis[N][N];
int dx[6] = {0, 0, 1, 1, -1, -1};
int dy[6] = {1, -1, 0, -1, 0, 1};
struct node {
    int x, y;
};

bool bfs(int r, int c) {
    queue<node> q;
    vis[r][c] = 1;
    q.push({r, c});

    while (q.size()) {
        node p = q.front();
        q.pop();

        if (p.x == n) {
            return true;
        }

        for (int i = 0; i < 6; i++) {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];

            if (nx < 1 || nx > n || ny < 1 || ny > n || vis[nx][ny] || a[nx][ny] != 1) {
                continue;
            }

            vis[nx][ny] = 1;
            q.push({nx, ny});
        }
    }

    return false;
}

bool bfs2(int r, int c) {
    queue<node> q;
    vis[r][c] = 1;
    q.push({r, c});

    while (q.size()) {
        node p = q.front();
        q.pop();

        if (p.y == n) {
            return true;
        }

        for (int i = 0; i < 6; i++) {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];

            if (nx < 1 || nx > n || ny < 1 || ny > n || vis[nx][ny] || a[nx][ny] != -1) {
                continue;
            }

            vis[nx][ny] = 1;
            q.push({nx, ny});
        }
    }

    return false;
}

int main() {
    cin >> t;

    while (t--) {
        cin >> n;
        bool ok = false;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> a[i][j];
            }
        }

        for (int i = 1; i <= n; i++) {
            memset(vis, 0, sizeof vis);

            if (a[1][i] == 1) {
                if (bfs(1, i)) {
                    cout << "Jimmy" << endl;
                    ok = true;
                    break;
                }
            }
        }

        if (ok == false) {
            for (int i = 1; i <= n; i++) {
                memset(vis, 0, sizeof vis);

                if (a[i][1] == -1) {
                    if (bfs2(i, 1)) {
                        cout << "Chen" << endl;
                        ok = true;
                        break;
                    }
                }
            }
        }

        if (ok == false) {
            cout << "yet" << endl;
        }
    }

    return 0;
}