#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
char arr[1005][1005];
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);
}
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];
}
}
queue<pair<int,int> > bq;
queue<pair<int,int> > tq;
int cnt = 0;
if(arr[n-1][m-1]=='B') bq.push({n-1,m-1});
else tq.push({n-1,m-1});
while(tq.size() || bq.size()){
if(tq.size()) cnt++;
while(tq.size()){
auto p = tq.front();
int x = p.first;
int y = p.second;
tq.pop();
for(int i=0;i<4;i++){
if(inbound(x+dx[i],y+dy[i])){
if(arr[x+dx[i]][y+dy[i]]=='B'){
bq.push({x+dx[i],y+dy[i]});
}
if(arr[x+dx[i]][y+dy[i]]=='T'){
tq.push({x+dx[i],y+dy[i]});
}
arr[x+dx[i]][y+dy[i]] = '*';
}
}
}
if(bq.size()) cnt++;
while(bq.size()){
auto p = bq.front();
int x = p.first;
int y = p.second;
bq.pop();
for(int i=0;i<4;i++){
if(inbound(x+dx[i],y+dy[i])){
if(arr[x+dx[i]][y+dy[i]]=='B'){
bq.push({x+dx[i],y+dy[i]});
}
if(arr[x+dx[i]][y+dy[i]]=='T'){
tq.push({x+dx[i],y+dy[i]});
}
arr[x+dx[i]][y+dy[i]] = '*';
}
}
}
}
cout<<cnt<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
2224 KB |
Output is correct |
17 |
Correct |
6 ms |
2260 KB |
Output is correct |
18 |
Correct |
7 ms |
2352 KB |
Output is correct |
19 |
Correct |
8 ms |
2232 KB |
Output is correct |
20 |
Correct |
7 ms |
2244 KB |
Output is correct |
21 |
Correct |
28 ms |
2204 KB |
Output is correct |
22 |
Correct |
28 ms |
2148 KB |
Output is correct |
23 |
Correct |
29 ms |
2132 KB |
Output is correct |
24 |
Correct |
30 ms |
2240 KB |
Output is correct |
25 |
Correct |
30 ms |
2280 KB |
Output is correct |
26 |
Correct |
29 ms |
2132 KB |
Output is correct |
27 |
Correct |
29 ms |
2136 KB |
Output is correct |
28 |
Correct |
30 ms |
2128 KB |
Output is correct |
29 |
Correct |
30 ms |
2236 KB |
Output is correct |
30 |
Correct |
29 ms |
2236 KB |
Output is correct |