-
Notifications
You must be signed in to change notification settings - Fork 0
/
21736.cpp
61 lines (53 loc) · 1.22 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
61
#include <iostream>
#include <queue>
using namespace std;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int graph[600][600];
int visited[600][600];
queue<pair<int,int> > q;
int N,M;
int sx,sy,cnt;
void BFS(int sx,int sy){
cnt = 0;
q.push(pair<int,int>(sx,sy));
visited[sy][sx]=1;
while(!q.empty()){
int vx = q.front().first;
int vy = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int xx = vx + dx[i];
int yy = vy + dy[i];
if(xx>=0 && xx<M && yy>=0 && yy<N && visited[yy][xx]==0 && graph[yy][xx]!=1){
q.push(pair<int,int>(xx,yy));
visited[yy][xx]=1;
if(graph[yy][xx]==2)
cnt++;
}
}
}
}
int main(){
cin >>N >>M;
string a;
for(int i=0; i<N; i++){
cin >> a;
for(int j=0; j<M;j++){
if (a[j]=='X')
graph[i][j]=1;
else if(a[j]=='P')
graph[i][j]=2;
else if(a[j]=='I'){
graph[i][j]=-1;
sx=j;
sy=i;
}
}
}
BFS(sx,sy);
if (cnt ==0)
cout << "TT";
else
cout << cnt;
}