#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<bool>> visit(600, vector<bool>(600));
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
pair<int, int> startPos;
vector<vector<char>> campus(n);
for (int i = 0; i < n; i++) {
campus[i] = vector<char>(m);
string input;
cin >> input;
for (int j = 0; j < input.size(); j++) {
campus[i][j] = input[j];
if (input[j] == 'I') {
startPos = { i, j };
}
}
}
queue<pair<int, int>> nexts;
nexts.push({ startPos });
visit[startPos.first][startPos.second] = true;
int ans = 0;
while (!nexts.empty()) {
pair<int, int> cur = nexts.front();
nexts.pop();
if (campus[cur.first][cur.second] == 'P') {
ans++;
}
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (visit[nx][ny] == 1 || campus[nx][ny] == 'X') continue;
nexts.push({ nx, ny });
visit[nx][ny] = true; // 방문ㅊ ㅓ리!
}
}
if (ans == 0) {
cout << "TT\n";
}
else {
cout << ans << "\n";
}
return 0;
}
그냥 BFS 돌리면 쉽게 해결할 수 있는 문제!
현재 위치가 P 일 때 ans 의 값을 1 증가시켜 주는 코드를 작성해주면 된다!