#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
pi operator+(pi a, pi b){
return {a.f + b.f, a.s + b.s};
}
const int maxn = 1000;
int n, m;
int a[maxn][maxn];
queue<pi> q[2];
pi d[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool ok(pi p){
return p.f >= 0 && p.s >= 0 && p.f < n && p.s < m && a[p.f][p.s] != -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
char c;
cin >> c;
a[i][j] = c == '*' ? -1 : c == 'B';
}
int ret = 0;
q[a[0][0]].push({0, 0});
for(int t = a[0][0]; !q[t].empty(); t ^= 1, ret++){
while(!q[t].empty()){
pi p = q[t].front();
q[t].pop();
for(int i = 0; i < 4; i++){
pi np = p + d[i];
if(ok(np)){
q[a[np.f][np.s]].push(np);
a[np.f][np.s] = -1;
}
}
}
}
cout << ret << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
768 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
768 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
768 KB |
Output is correct |
12 |
Correct |
5 ms |
768 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
768 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
25 ms |
5120 KB |
Output is correct |
17 |
Correct |
25 ms |
5120 KB |
Output is correct |
18 |
Correct |
25 ms |
5248 KB |
Output is correct |
19 |
Correct |
26 ms |
5120 KB |
Output is correct |
20 |
Correct |
24 ms |
5120 KB |
Output is correct |
21 |
Correct |
45 ms |
4992 KB |
Output is correct |
22 |
Correct |
45 ms |
4992 KB |
Output is correct |
23 |
Correct |
46 ms |
5112 KB |
Output is correct |
24 |
Correct |
47 ms |
5248 KB |
Output is correct |
25 |
Correct |
50 ms |
5120 KB |
Output is correct |
26 |
Correct |
46 ms |
4992 KB |
Output is correct |
27 |
Correct |
48 ms |
4984 KB |
Output is correct |
28 |
Correct |
45 ms |
4984 KB |
Output is correct |
29 |
Correct |
49 ms |
5240 KB |
Output is correct |
30 |
Correct |
47 ms |
5120 KB |
Output is correct |