#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vl vector<ll>
#define sl set<ll>
#define conn continue
#define tests while(t--)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define s second
using namespace std;
const int sz = 4005;
int neighborx[4] = {1, -1, 0, 0};
int neighbory[4] = {0, 0, 1, -1};
int depth[sz][sz], n, m, i, j;
string a[sz];
bool isvalid(int c, int d)
{
if(c < 0 || d < 0 || c >= n || d >= m || a[c][d] == '.') return false;
return true;
}
int main()
{
cin >> n >> m;
for(i = 0; i < n; i++) cin >> a[i];
depth[0][0] = 1;
deque<pii>q;
q.pb({0, 0});
int ans = 0;
while(!q.empty())
{
auto s = q.front();
q.pop_front();
int x = s.f, y = s.s;
ans = max(ans, depth[x][y]);
for(i = 0; i < 4; i++)
{
int nx = x + neighborx[i], ny = y + neighbory[i];
if(isvalid(nx, ny) && depth[nx][ny] == 0)
{
if(a[x][y] == a[nx][ny])
{
depth[nx][ny] = depth[x][y];
q.push_front({nx, ny});
}
else
{
depth[nx][ny] = depth[x][y] + 1;
q.pb({nx, ny});
}
}
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
4052 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
10 ms |
3516 KB |
Output is correct |
5 |
Correct |
4 ms |
2004 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
4 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
1492 KB |
Output is correct |
12 |
Correct |
6 ms |
2076 KB |
Output is correct |
13 |
Correct |
4 ms |
2072 KB |
Output is correct |
14 |
Correct |
4 ms |
1984 KB |
Output is correct |
15 |
Correct |
14 ms |
3912 KB |
Output is correct |
16 |
Correct |
17 ms |
4080 KB |
Output is correct |
17 |
Correct |
12 ms |
3924 KB |
Output is correct |
18 |
Correct |
11 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15956 KB |
Output is correct |
2 |
Correct |
62 ms |
12652 KB |
Output is correct |
3 |
Correct |
456 ms |
87844 KB |
Output is correct |
4 |
Correct |
126 ms |
32516 KB |
Output is correct |
5 |
Correct |
287 ms |
52956 KB |
Output is correct |
6 |
Correct |
838 ms |
122376 KB |
Output is correct |
7 |
Correct |
11 ms |
16596 KB |
Output is correct |
8 |
Correct |
10 ms |
15976 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
11 ms |
16324 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
64 ms |
12632 KB |
Output is correct |
14 |
Correct |
38 ms |
8664 KB |
Output is correct |
15 |
Correct |
37 ms |
11172 KB |
Output is correct |
16 |
Correct |
28 ms |
4932 KB |
Output is correct |
17 |
Correct |
159 ms |
28488 KB |
Output is correct |
18 |
Correct |
136 ms |
35412 KB |
Output is correct |
19 |
Correct |
127 ms |
32492 KB |
Output is correct |
20 |
Correct |
111 ms |
25672 KB |
Output is correct |
21 |
Correct |
283 ms |
52172 KB |
Output is correct |
22 |
Correct |
300 ms |
53236 KB |
Output is correct |
23 |
Correct |
297 ms |
43204 KB |
Output is correct |
24 |
Correct |
286 ms |
49508 KB |
Output is correct |
25 |
Correct |
608 ms |
108864 KB |
Output is correct |
26 |
Correct |
574 ms |
130800 KB |
Output is correct |
27 |
Correct |
745 ms |
135524 KB |
Output is correct |
28 |
Correct |
826 ms |
122372 KB |
Output is correct |
29 |
Correct |
806 ms |
120964 KB |
Output is correct |
30 |
Correct |
773 ms |
124508 KB |
Output is correct |
31 |
Correct |
613 ms |
73112 KB |
Output is correct |
32 |
Correct |
666 ms |
120708 KB |
Output is correct |