#54508: 範例測資輸出錯誤,實際測資AC


kita197 (KK)


//使用GPT

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

int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,-1,1,-1,0,1};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T, G;
    cin >> T >> G;

    while (T--) {
        long long totalComponents = 0;

        for (int g = 0; g < G; g++) {
            int M, N;
            cin >> M >> N;

            vector<string> grid(M);
            for (int i = 0; i < M; i++) {
                cin >> grid[i];
            }

            vector<vector<char>> visited(M, vector<char>(N, 0));

            for (int i = 0; i < M; i++) {
                for (int j = 0; j < N; j++) {
                    if (grid[i][j] == 'D' && !visited[i][j]) {
                        totalComponents++;

                        queue<pair<int,int>> q;
                        q.push({i, j});
                        visited[i][j] = 1;

                        while (!q.empty()) {
                            auto [x, y] = q.front();
                            q.pop();

                            for (int d = 0; d < 8; d++) {
                                int nx = x + dx[d];
                                int ny = y + dy[d];

                                if (nx >= 0 && nx < M && ny >= 0 && ny < N) {
                                    if (grid[nx][ny] == 'D' && !visited[nx][ny]) {
                                        visited[nx][ny] = 1;
                                        q.push({nx, ny});
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // D 鳴:每筆測資只能一次
        if (totalComponents >= 2) {
            cout << totalComponents - 1 << " DD好醜\n";
        } else {
            cout << totalComponents << "\n";
        }
    }

    return 0;
}

                queue<pair<int,int>> q;

                        q.push({i, j});
                        visited[i][j] = 1;

                        while (!q.empty()) {
                            auto [x, y] = q.front();
                            q.pop();

                            for (int d = 0; d < 8; d++) {
                                int nx = x + dx[d];
                                int ny = y + dy[d];

                                if (nx >= 0 && nx < M && ny >= 0 && ny < N) {
                                    if (grid[nx][ny] == 'D' && !visited[nx][ny]) {
                                        visited[nx][ny] = 1;
                                        q.push({nx, ny});
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (totalComponents >= 2) {
            cout << totalComponents - 1 << " DD好醜\n";
        } else {
            cout << totalComponents << "\n";
        }
    }

    return 0;
}