백준 21736 헌내기는 친구가 필요해

Caden·2023년 9월 2일
0

백준

목록 보기
13/20

boardstring으로 관리하는 정석적인 DFS/BFS 탐색 문제.

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    vector<string> board(n);
    queue<pii> Q;
    vector<vector<int>> vis(n, vector<int>(m));
    for (int i = 0; i < n; ++i) {
        cin >> board[i];
        for (int j = 0; j < m; ++j) {
            if (board[i][j] == 'I') {
                Q.push({i, j});
                vis[i][j] = 1;
            }
        }
    }
    int ans = 0;
    while (Q.size()) {
        int nowX = Q.front().first;
        int nowY = Q.front().second;
        Q.pop();
        for (int i = 0; i < 4; ++i) {
            int nextX = nowX + dx[i];
            int nextY = nowY + dy[i];
            if (nextX < 0 || nextX > n-1 || nextY < 0 || nextY > m-1) continue;
            if (board[nextX][nextY] == 'X' || vis[nextX][nextY]) continue;
            if (board[nextX][nextY] == 'P') ans++;
            vis[nextX][nextY] = 1;
            Q.push({nextX, nextY});
        }
    }
    if (ans) cout << ans;
    else cout << "TT";
}

0개의 댓글