#include<bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector <vector <char> > a(n, vector <char> (m));
vector <vector <bool> > used(n, vector <bool> (m));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
queue <pair <int, int> > q1, q2;
used[0][0] = true;
if(a[0][0] == 'T')
{
q1.push({0, 0});
}
else if(a[0][0] == 'B')
{
q2.push({0, 0});
}
int cnt = 0;
while(q1.size() > 0 || q2.size() > 0)
{
if(q1.size() > 0)
{
cnt++;
}
while(q1.size() > 0)
{
pair <int, int> v = q1.front();
q1.pop();
int x = v.first;
int y = v.second;
if(x > 0 && !used[x - 1][y])
{
used[x - 1][y] = true;
if(a[x - 1][y] == 'T')
{
q1.push({x - 1, y});
}
else if(a[x - 1][y] == 'B')
{
q2.push({x - 1, y});
}
}
if(x < n - 1 && !used[x + 1][y])
{
used[x + 1][y] = true;
if(a[x + 1][y] == 'T')
{
q1.push({x + 1, y});
}
else if(a[x + 1][y] == 'B')
{
q2.push({x + 1, y});
}
}
if(y > 0 && !used[x][y - 1])
{
used[x][y - 1] = true;
if(a[x][y - 1] == 'T')
{
q1.push({x, y - 1});
}
else if(a[x][y - 1] == 'B')
{
q2.push({x, y - 1});
}
}
if(y < m - 1 && !used[x][y + 1])
{
used[x][y + 1] = true;
if(a[x][y + 1] == 'T')
{
q1.push({x, y + 1});
}
else if(a[x][y + 1] == 'B')
{
q2.push({x, y + 1});
}
}
}
if(q2.size() > 0)
{
cnt++;
}
while(q2.size() > 0)
{
pair <int, int> v = q2.front();
q2.pop();
int x = v.first;
int y = v.second;
if(x > 0 && !used[x - 1][y])
{
used[x - 1][y] = true;
if(a[x - 1][y] == 'T')
{
q1.push({x - 1, y});
}
else if(a[x - 1][y] == 'B')
{
q2.push({x - 1, y});
}
}
if(x < n - 1 && !used[x + 1][y])
{
used[x + 1][y] = true;
if(a[x + 1][y] == 'T')
{
q1.push({x + 1, y});
}
else if(a[x + 1][y] == 'B')
{
q2.push({x + 1, y});
}
}
if(y > 0 && !used[x][y - 1])
{
used[x][y - 1] = true;
if(a[x][y - 1] == 'T')
{
q1.push({x, y - 1});
}
else if(a[x][y - 1] == 'B')
{
q2.push({x, y - 1});
}
}
if(y < m - 1 && !used[x][y + 1])
{
used[x][y + 1] = true;
if(a[x][y + 1] == 'T')
{
q1.push({x, y + 1});
}
else if(a[x][y + 1] == 'B')
{
q2.push({x, y + 1});
}
}
}
}
cout << cnt;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
18 ms |
2432 KB |
Output is correct |
17 |
Correct |
17 ms |
2432 KB |
Output is correct |
18 |
Correct |
18 ms |
2560 KB |
Output is correct |
19 |
Correct |
20 ms |
2560 KB |
Output is correct |
20 |
Correct |
18 ms |
2432 KB |
Output is correct |
21 |
Correct |
50 ms |
2560 KB |
Output is correct |
22 |
Correct |
50 ms |
2432 KB |
Output is correct |
23 |
Correct |
49 ms |
2432 KB |
Output is correct |
24 |
Correct |
52 ms |
2432 KB |
Output is correct |
25 |
Correct |
50 ms |
2432 KB |
Output is correct |
26 |
Correct |
51 ms |
2432 KB |
Output is correct |
27 |
Correct |
50 ms |
2304 KB |
Output is correct |
28 |
Correct |
49 ms |
2304 KB |
Output is correct |
29 |
Correct |
53 ms |
2432 KB |
Output is correct |
30 |
Correct |
51 ms |
2432 KB |
Output is correct |