#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> X = {-1, 0, 1, 0}, Y = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> a(n);
for (auto &i : a)
cin >> i;
char c = a[0][0];
vector<vector<bool>> mp(n, vector<bool>(m)); mp[0][0] = true;
for (int z = 0; ; z++) {
bool tr = !z;
queue<pair<int, int>> q;
vector<vector<bool>> vis(n, vector<bool>(m));
q.push({0, 0}); vis[0][0] = true;
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + X[i], ny = y + Y[i];
if (0 <= min(nx, ny) && nx < n && ny < m && !vis[nx][ny] && (mp[nx][ny] || c == a[nx][ny])) {
q.push({nx, ny});
tr |= !mp[nx][ny];
vis[nx][ny] = mp[nx][ny] = true;
}
}
}
c = (c == 'F' ? 'R' : 'F');
if (!tr) return cout << z, 0;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
1056 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
16 ms |
856 KB |
Output is correct |
5 |
Correct |
49 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
52 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
48 ms |
604 KB |
Output is correct |
13 |
Correct |
50 ms |
600 KB |
Output is correct |
14 |
Correct |
49 ms |
604 KB |
Output is correct |
15 |
Correct |
312 ms |
860 KB |
Output is correct |
16 |
Correct |
125 ms |
1068 KB |
Output is correct |
17 |
Correct |
323 ms |
860 KB |
Output is correct |
18 |
Correct |
15 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2001 ms |
1112 KB |
Time limit exceeded |
2 |
Execution timed out |
2045 ms |
4188 KB |
Time limit exceeded |
3 |
Execution timed out |
2089 ms |
38208 KB |
Time limit exceeded |
4 |
Execution timed out |
2062 ms |
9204 KB |
Time limit exceeded |
5 |
Execution timed out |
2073 ms |
21592 KB |
Time limit exceeded |
6 |
Execution timed out |
2041 ms |
38224 KB |
Time limit exceeded |
7 |
Correct |
1564 ms |
1116 KB |
Output is correct |
8 |
Execution timed out |
2013 ms |
1116 KB |
Time limit exceeded |
9 |
Correct |
104 ms |
344 KB |
Output is correct |
10 |
Correct |
257 ms |
344 KB |
Output is correct |
11 |
Correct |
664 ms |
1116 KB |
Output is correct |
12 |
Correct |
1330 ms |
512 KB |
Output is correct |
13 |
Execution timed out |
2065 ms |
4188 KB |
Time limit exceeded |
14 |
Execution timed out |
2059 ms |
2652 KB |
Time limit exceeded |
15 |
Execution timed out |
2019 ms |
2648 KB |
Time limit exceeded |
16 |
Execution timed out |
2059 ms |
1884 KB |
Time limit exceeded |
17 |
Execution timed out |
2032 ms |
9812 KB |
Time limit exceeded |
18 |
Execution timed out |
2049 ms |
9820 KB |
Time limit exceeded |
19 |
Execution timed out |
2059 ms |
9044 KB |
Time limit exceeded |
20 |
Execution timed out |
2052 ms |
8540 KB |
Time limit exceeded |
21 |
Execution timed out |
2012 ms |
22160 KB |
Time limit exceeded |
22 |
Execution timed out |
2083 ms |
21980 KB |
Time limit exceeded |
23 |
Execution timed out |
2070 ms |
18524 KB |
Time limit exceeded |
24 |
Execution timed out |
2095 ms |
21840 KB |
Time limit exceeded |
25 |
Execution timed out |
2059 ms |
38356 KB |
Time limit exceeded |
26 |
Correct |
460 ms |
29300 KB |
Output is correct |
27 |
Correct |
1603 ms |
38252 KB |
Output is correct |
28 |
Execution timed out |
2075 ms |
38248 KB |
Time limit exceeded |
29 |
Execution timed out |
2079 ms |
38260 KB |
Time limit exceeded |
30 |
Execution timed out |
2061 ms |
37444 KB |
Time limit exceeded |
31 |
Execution timed out |
2031 ms |
24404 KB |
Time limit exceeded |
32 |
Execution timed out |
2063 ms |
38404 KB |
Time limit exceeded |