-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21736_bfs.cpp
57 lines (56 loc) · 1.04 KB
/
21736_bfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 210509 #21736 Çå³»±â´Â Ä£±¸°¡ ÇÊ¿äÇØ Silver II
// bfs
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
const int MAX = 601;
int N, M, sx, sy, ans;
char a[MAX][MAX];
int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1,1,0,0 };
bool visit[MAX][MAX];
void bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({ x,y });
visit[y][x] = true;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (a[y][x] == 'P')
ans++;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < M && ny < N) {
if (!visit[ny][nx] && a[ny][nx] != 'X') {
visit[ny][nx] = true;
q.push({ nx, ny });
}
}
}
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < M; j++) {
a[i][j] = s[j];
if (a[i][j] == 'I') {
sy = i, sx = j;
}
}
}
bfs(sx, sy);
if (!ans)
cout << "TT\n";
else
cout << ans << "\n";
}