#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=1000, di[4]={1, -1, 0, 0}, dj[4]={0, 0, 1, -1};
int n, m, d[mxN][mxN], ans;
string g[mxN];
bool vis[mxN][mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i)
cin >> g[i];
memset(d, 0x3f, sizeof(d));
d[0][0]=1;
deque<ar<int, 2>> q={{0, 0}};
while(q.size()) {
int i=q.front()[0], j=q.front()[1];
q.pop_front();
if (vis[i][j])
continue;
vis[i][j]=1;
ans=max(ans, d[i][j]);
for (int k=0; k<4; ++k) {
int ni=i+di[k], nj=j+dj[k];
if (0<=ni&&ni<n&&0<=nj&&nj<m&&!vis[ni][nj]&&g[ni][nj]!='*') {
if (g[i][j]==g[ni][nj]&&d[i][j]<d[ni][nj]) {
d[ni][nj]=d[i][j];
q.push_front({ni, nj});
} else if (g[i][j]!=g[ni][nj]&&d[i][j]+1<d[ni][nj]) {
d[ni][nj]=d[i][j]+1;
q.push_back({ni, nj});
}
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4172 KB |
Output is correct |
2 |
Correct |
2 ms |
4172 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
2 ms |
4300 KB |
Output is correct |
6 |
Correct |
2 ms |
4300 KB |
Output is correct |
7 |
Correct |
2 ms |
4300 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
3 ms |
4300 KB |
Output is correct |
12 |
Correct |
3 ms |
4300 KB |
Output is correct |
13 |
Correct |
2 ms |
4300 KB |
Output is correct |
14 |
Correct |
2 ms |
4300 KB |
Output is correct |
15 |
Correct |
2 ms |
4300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4172 KB |
Output is correct |
2 |
Correct |
2 ms |
4172 KB |
Output is correct |
3 |
Correct |
2 ms |
4172 KB |
Output is correct |
4 |
Correct |
2 ms |
4300 KB |
Output is correct |
5 |
Correct |
2 ms |
4300 KB |
Output is correct |
6 |
Correct |
2 ms |
4300 KB |
Output is correct |
7 |
Correct |
2 ms |
4300 KB |
Output is correct |
8 |
Correct |
3 ms |
4300 KB |
Output is correct |
9 |
Correct |
3 ms |
4300 KB |
Output is correct |
10 |
Correct |
3 ms |
4300 KB |
Output is correct |
11 |
Correct |
3 ms |
4300 KB |
Output is correct |
12 |
Correct |
3 ms |
4300 KB |
Output is correct |
13 |
Correct |
2 ms |
4300 KB |
Output is correct |
14 |
Correct |
2 ms |
4300 KB |
Output is correct |
15 |
Correct |
2 ms |
4300 KB |
Output is correct |
16 |
Correct |
9 ms |
7160 KB |
Output is correct |
17 |
Correct |
11 ms |
7220 KB |
Output is correct |
18 |
Correct |
9 ms |
7248 KB |
Output is correct |
19 |
Correct |
11 ms |
7292 KB |
Output is correct |
20 |
Correct |
9 ms |
7132 KB |
Output is correct |
21 |
Correct |
48 ms |
7072 KB |
Output is correct |
22 |
Correct |
48 ms |
6988 KB |
Output is correct |
23 |
Correct |
49 ms |
7116 KB |
Output is correct |
24 |
Correct |
50 ms |
7244 KB |
Output is correct |
25 |
Correct |
50 ms |
7200 KB |
Output is correct |
26 |
Correct |
49 ms |
7124 KB |
Output is correct |
27 |
Correct |
57 ms |
7096 KB |
Output is correct |
28 |
Correct |
47 ms |
6988 KB |
Output is correct |
29 |
Correct |
50 ms |
7116 KB |
Output is correct |
30 |
Correct |
49 ms |
7164 KB |
Output is correct |