#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
char arr[1005][1005];
int tcnt = 0;
int bcnt = 0;
int n,m;
int dx[4]={0,0,-1,1};
int dy[4] = {1,-1,0,0};
bool inbound(int x,int y){
return (x>=0 && x<n && y>=0 && y<m);
}
void dfs(int x,int y,char t,char re){
if(t=='B'){
bcnt--;
tcnt++;
}else{
tcnt--;
bcnt++;
}
arr[x][y]=re;
for(int i=0;i<4;i++){
if(!inbound(x+dx[i],y+dy[i])) continue;
if(arr[x+dx[i]][y+dy[i]]==t){
dfs(x+dx[i],y+dy[i],t,re);
}
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++){
string s;cin>>s;
for(int j=0;j<m;j++){
arr[i][j] = s[j];
if(s[j]=='B') bcnt++;
if(s[j]=='T') tcnt++;
}
}
if(arr[n-1][m-1]=='*'){
cout<<0<<"\n";
return 0;
}
int cnt = 1;
while(bcnt && tcnt){
if(arr[n-1][m-1]=='B'){
dfs(n-1,m-1,'B','T');
}else{
dfs(n-1,m-1,'T','B');
}
cnt++;
/*for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<arr[i][j];
}
cout<<"\n";
}*/
}
cout<<cnt<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
5 ms |
468 KB |
Output is correct |
11 |
Correct |
5 ms |
468 KB |
Output is correct |
12 |
Correct |
7 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
5 ms |
468 KB |
Output is correct |
11 |
Correct |
5 ms |
468 KB |
Output is correct |
12 |
Correct |
7 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
12 ms |
3972 KB |
Output is correct |
17 |
Correct |
16 ms |
3652 KB |
Output is correct |
18 |
Correct |
16 ms |
4316 KB |
Output is correct |
19 |
Correct |
21 ms |
4584 KB |
Output is correct |
20 |
Correct |
12 ms |
4400 KB |
Output is correct |
21 |
Execution timed out |
2072 ms |
13116 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |