#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
int h, w;
const int maxn = 4000;
char a[maxn + 3][maxn + 3];
int d[maxn + 3][maxn + 3];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool inside(int i, int j)
{
return (i >= 1 && i <= h && j >= 1 && j <= w && a[i][j] != '.');
}
int main()
{
//freopen("TRACK.INP", "r", stdin);
//freopen("TRACK.OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
for(int i = 1; i <= h; i++)
{
for(int j = 1; j <= w; j++)
{
cin >> a[i][j];
}
}
d[1][1] = 1;
deque<pair<int,int>>dq;
dq.push_front({1, 1});
int ans = 1;
while(!dq.empty())
{
pii p = dq.front();
dq.pop_front();
ans = max(ans, d[p.first][p.second]);
for(int i = 0; i < 4; ++i)
{
int i1 = p.first + dx[i];
int j1 = p.second + dy[i];
if(inside(i1, j1) && d[i1][j1] == 0)
{
if(a[i1][j1] == a[p.first][p.second])
{
d[i1][j1] = d[p.first][p.second];
dq.push_front({i1, j1});
}
else{
d[i1][j1] = d[p.first][p.second] + 1;
dq.push_back({i1, j1});
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
5464 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
6 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
3164 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
7 ms |
3024 KB |
Output is correct |
13 |
Correct |
3 ms |
2920 KB |
Output is correct |
14 |
Correct |
3 ms |
3164 KB |
Output is correct |
15 |
Correct |
9 ms |
5212 KB |
Output is correct |
16 |
Correct |
10 ms |
5468 KB |
Output is correct |
17 |
Correct |
8 ms |
5212 KB |
Output is correct |
18 |
Correct |
6 ms |
5080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
30860 KB |
Output is correct |
2 |
Correct |
42 ms |
14956 KB |
Output is correct |
3 |
Correct |
241 ms |
73428 KB |
Output is correct |
4 |
Correct |
74 ms |
32592 KB |
Output is correct |
5 |
Correct |
130 ms |
53332 KB |
Output is correct |
6 |
Correct |
612 ms |
107456 KB |
Output is correct |
7 |
Correct |
16 ms |
32344 KB |
Output is correct |
8 |
Correct |
13 ms |
30736 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
12 ms |
31744 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
38 ms |
14968 KB |
Output is correct |
14 |
Correct |
23 ms |
10576 KB |
Output is correct |
15 |
Correct |
20 ms |
13148 KB |
Output is correct |
16 |
Correct |
18 ms |
5724 KB |
Output is correct |
17 |
Correct |
96 ms |
28876 KB |
Output is correct |
18 |
Correct |
79 ms |
35600 KB |
Output is correct |
19 |
Correct |
70 ms |
32592 KB |
Output is correct |
20 |
Correct |
56 ms |
25560 KB |
Output is correct |
21 |
Correct |
141 ms |
52584 KB |
Output is correct |
22 |
Correct |
145 ms |
53320 KB |
Output is correct |
23 |
Correct |
184 ms |
43600 KB |
Output is correct |
24 |
Correct |
135 ms |
49748 KB |
Output is correct |
25 |
Correct |
331 ms |
94288 KB |
Output is correct |
26 |
Correct |
552 ms |
131012 KB |
Output is correct |
27 |
Correct |
543 ms |
112636 KB |
Output is correct |
28 |
Correct |
601 ms |
107488 KB |
Output is correct |
29 |
Correct |
598 ms |
105164 KB |
Output is correct |
30 |
Correct |
581 ms |
109772 KB |
Output is correct |
31 |
Correct |
384 ms |
73396 KB |
Output is correct |
32 |
Correct |
515 ms |
110292 KB |
Output is correct |