-
Notifications
You must be signed in to change notification settings - Fork 0
/
11561.cpp
86 lines (83 loc) · 1.36 KB
/
11561.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
11561 - Getting Gold
*/
#include<bits/stdc++.h>
using namespace std;
int row,col,cnt;
char v[105][105];
int vis[105][105];
int dx[]= {-1,+0,+1,+0};
int dy[]= {+0,+1,+0,-1};
void dfs(int a,int b)
{
if(v[a][b]=='#' || vis[a][b])
return;
if(v[a][b]=='G')
cnt++;
vis[a][b]=true;
for(int i=0; i<4; i++)
{
int x = dx[i]+a;
int y = dy[i]+b;
if(v[x][y]=='T')
return;
}
for(int i=0; i<4; i++)
{
int x1 = dx[i]+a;
int y1 = dy[i]+b;
dfs(x1,y1);
}
}
int main()
{
/// freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
while(cin>>row>>col)
{
for(int i=0; i<col; i++)
{
cin>>v[i];
}
memset(vis,false,sizeof(vis));
bool used =false;
cnt=0;
int a,b;
for(int i=0; i<col; i++)
{
for(int j=0; j<row; j++)
{
if(v[i][j]=='P')
{
a=i;
b=j;
used = true;
break;
}
}
if(used)
break;
}
dfs(a,b);
cout<<cnt<<endl;
}
return 0;
}
/*
Sample Input
7 4
#######
#P.GTG#
#..TGG#
#######
8 6
########
#...GTG#
#..PG.G#
#...G#G#
#..TG.G#
########
Sample Output
1
4
*/