#include "bits/stdc++.h"
#define endl '\n'
const int maxn = 1005;
const int inf = 2e9;
const int mod = 1e9 + 7;
using namespace std;
using lint = long long;
using pii = pair<int,int>;
int n, m;
int ans;
char g[maxn][maxn];
int d[maxn][maxn];
deque<pii> dq;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '*' && !d[x][y];
}
signed main() {
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> g[i];
d[0][0] = 1;
dq.push_back({0, 0});
while(!dq.empty()) {
auto [x1, y1] = dq.front();
ans = max(ans, d[x1][y1]);
dq.pop_front();
for(int i = 0; i < 4; i++) {
int x2 = x1 + dx[i], y2 = y1 + dy[i];
if(!check(x2, y2))
continue;
int w = g[x2][y2] != g[x1][y1];
if(w == 0) {
dq.push_front({x2, y2});
d[x2][y2] = d[x1][y1];
}
else {
dq.push_back({x2, y2});
d[x2][y2] = d[x1][y1] + 1;
}
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
16 |
Correct |
8 ms |
5940 KB |
Output is correct |
17 |
Correct |
9 ms |
5924 KB |
Output is correct |
18 |
Correct |
7 ms |
5916 KB |
Output is correct |
19 |
Correct |
13 ms |
6008 KB |
Output is correct |
20 |
Correct |
7 ms |
5836 KB |
Output is correct |
21 |
Correct |
37 ms |
5700 KB |
Output is correct |
22 |
Correct |
36 ms |
5712 KB |
Output is correct |
23 |
Correct |
36 ms |
5796 KB |
Output is correct |
24 |
Correct |
39 ms |
5956 KB |
Output is correct |
25 |
Correct |
38 ms |
5904 KB |
Output is correct |
26 |
Correct |
36 ms |
5708 KB |
Output is correct |
27 |
Correct |
37 ms |
5708 KB |
Output is correct |
28 |
Correct |
40 ms |
5768 KB |
Output is correct |
29 |
Correct |
49 ms |
5944 KB |
Output is correct |
30 |
Correct |
37 ms |
5836 KB |
Output is correct |