#include <bits/stdc++.h>
using namespace std;
int dir_x[] = {1, -1, 0, 0};
int dir_y[] = {0, 0, 1, -1};
int main()
{
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char> (m));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> grid[i][j];
}
}
if(grid[0][0] == '.' || grid[n - 1][m - 1] == '.')
{
cout << 0;
return 0;
}
queue<pair<int, int>> fox, rabbit;
vector<vector<bool>> visited(n, vector<bool> (m));
if(grid[0][0] == 'F')
{
fox.push({0, 0});
}
else
{
rabbit.push({0, 0});
}
visited[0][0] = true;
int ans = 0;
while(fox.size() || rabbit.size())
{
if(fox.size())
{
ans++;
}
while(fox.size())
{
auto src = fox.front();
fox.pop();
for(int i = 0; i < 4; i++)
{
int x = src.first + dir_x[i], y = src.second + dir_y[i];
if(x >= 0 && x < n && y >= 0 && y < n && visited[x][y] == false && grid[x][y] != '.')
{
visited[x][y] = true;
if(grid[x][y] == 'F')
{
fox.push({x, y});
}
else
{
rabbit.push({x, y});
}
}
}
}
if(rabbit.size())
{
ans++;
}
while(rabbit.size())
{
auto src = rabbit.front();
rabbit.pop();
for(int i = 0; i < 4; i++)
{
int x = src.first + dir_x[i], y = src.second + dir_y[i];
if(x >= 0 && x < n && y >= 0 && y < n && visited[x][y] == false && grid[x][y] != '.')
{
visited[x][y] = true;
if(grid[x][y] == 'F')
{
fox.push({x, y});
}
else
{
rabbit.push({x, y});
}
}
}
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
856 KB |
Output isn't correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
428 KB |
Output is correct |
4 |
Correct |
9 ms |
852 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
584 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
636 KB |
Output is correct |
14 |
Correct |
4 ms |
608 KB |
Output is correct |
15 |
Correct |
15 ms |
860 KB |
Output is correct |
16 |
Incorrect |
16 ms |
860 KB |
Output isn't correct |
17 |
Correct |
14 ms |
860 KB |
Output is correct |
18 |
Correct |
9 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
1116 KB |
Output isn't correct |
2 |
Correct |
71 ms |
3776 KB |
Output is correct |
3 |
Correct |
563 ms |
33872 KB |
Output is correct |
4 |
Incorrect |
148 ms |
8216 KB |
Output isn't correct |
5 |
Correct |
372 ms |
19280 KB |
Output is correct |
6 |
Correct |
1032 ms |
44104 KB |
Output is correct |
7 |
Correct |
12 ms |
856 KB |
Output is correct |
8 |
Incorrect |
11 ms |
1116 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
352 KB |
Output isn't correct |
11 |
Correct |
12 ms |
872 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
78 ms |
3780 KB |
Output is correct |
14 |
Incorrect |
41 ms |
2404 KB |
Output isn't correct |
15 |
Correct |
37 ms |
2392 KB |
Output is correct |
16 |
Incorrect |
19 ms |
1640 KB |
Output isn't correct |
17 |
Correct |
201 ms |
9180 KB |
Output is correct |
18 |
Correct |
177 ms |
8792 KB |
Output is correct |
19 |
Incorrect |
134 ms |
8348 KB |
Output isn't correct |
20 |
Incorrect |
104 ms |
7668 KB |
Output isn't correct |
21 |
Correct |
343 ms |
20048 KB |
Output is correct |
22 |
Correct |
386 ms |
19352 KB |
Output is correct |
23 |
Incorrect |
380 ms |
16720 KB |
Output isn't correct |
24 |
Correct |
347 ms |
19748 KB |
Output is correct |
25 |
Correct |
645 ms |
33872 KB |
Output is correct |
26 |
Correct |
566 ms |
26232 KB |
Output is correct |
27 |
Correct |
827 ms |
35664 KB |
Output is correct |
28 |
Correct |
1002 ms |
43952 KB |
Output is correct |
29 |
Correct |
1014 ms |
42560 KB |
Output is correct |
30 |
Correct |
856 ms |
40932 KB |
Output is correct |
31 |
Incorrect |
723 ms |
22356 KB |
Output isn't correct |
32 |
Correct |
747 ms |
34640 KB |
Output is correct |