-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21736.cpp
60 lines (56 loc) · 1.39 KB
/
21736.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
58
59
60
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int n, m;
char map[601][601] = {0, };
bool visited[601][601] = {0, };
int bfs();
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
queue<pair<int, int>>q;
int main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)
cin >> map[i][j];
}
int answer = bfs();
if(answer == 0)
cout << "TT";
else cout << answer;
}
int bfs(){
int ret = 0;
//도연이 찾기
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(map[i][j] == 'I'){
visited[i][j] = 1;
q.push(make_pair(i, j));
break;
}
}
}
while(!q.empty()){
for(int i = 0; i < 4; i++){
int x = q.front().second + dx[i];
int y = q.front().first + dy[i];
if(x >= 1 && x <= m && y >= 1 && y <= n)
if(map[y][x] == 'O'&& visited[y][x] == 0){
visited[y][x] = 1;
q.push(make_pair(y, x));
}
else if(map[y][x] == 'P' && visited[y][x] == 0){
ret++;
visited[y][x] = 1;
q.push(make_pair(y, x));
}
}
q.pop();
}
return ret;
}