QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#427708#8742. 黑白xhguaAC ✓76ms15100kbC++141.2kb2024-06-01 15:09:132024-06-01 15:09:13

Judging History

你现在查看的是最新测评结果

  • [2024-06-01 15:09:13]
  • 评测
  • 测评结果:AC
  • 用时:76ms
  • 内存:15100kb
  • [2024-06-01 15:09:13]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 1e3 + 5, dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

int T, n, m;
bool vis[N][N];
char a[N][N];

void dfs(int x, int y) {
    if (vis[x][y]) return;
    vis[x][y] = true;

    for (int d = 0; d < 4; d++) {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] != 'W') continue;
        dfs(nx, ny);
    }
}

void solve() {
    std::cin >> n >> m; 
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) std::cin >> a[i][j];
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) vis[i][j] = false;

    if (a[1][1] != 'W' || a[n][m] != 'W') {
        std::cout << "J" << "\n";
        return;
    }

    dfs(1, 1);
    if (!vis[n][m]) {
        std::cout << "J" << "\n";
        return;
    }

    int cnt = 0;
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cnt += (a[i][j] == 'W');

    if (cnt % 2 == (n + m - 1) % 2) std::cout << "J" << "\n";
    else std::cout << "I" << "\n";
}

int main() {

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> T;
    while (T--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 76ms
memory: 15100kb

input:

100
2 6
WWBBWW
WWWWWB
3 8
WWWBBBBB
WWWBWWWB
BBWWWBWW
5 2
WB
BB
BB
BW
BB
6 4
WWBW
WBWB
WWWW
WWWB
BBWW
BWBW
2 3
WWW
WBW
124 125
BWWWWWWWWWWWWWWWWWWWWWWWWWWBWWWWBWWWWWWWWBWWWWWWWWWWWBBBBWWWWWWWWWWWWWWWWWBWWWWWWWWWBWWWWWWWWWWBWWWWWWWWBBWWWWWWWWWWWWWWWWWWB
WWWWWWWBWWBWWWWWWWWWWWBWWBWWBWWWWBWWWWWWWWWBWBWB...

output:

J
J
J
I
I
J
I
I
I
J
I
J
J
J
J
J
I
I
I
I
J
J
I
I
I
J
J
I
J
J
J
J
I
J
J
J
J
J
J
I
J
J
I
I
I
J
J
I
J
J
J
I
J
I
J
J
J
J
I
I
J
J
J
I
J
J
I
J
I
I
J
J
J
I
J
I
I
J
J
I
J
J
J
J
J
I
J
J
J
I
I
J
J
I
I
J
J
J
J
I

result:

ok 100 lines