#include <iostream>
#include <fstream>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cstring>
#include <numeric>
#include <string>
#include <bitset>
#include <set>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
ll dr[4] = {0, 1, 0, -1};
ll dc[4] = {1, 0, -1, 0};
int main()
{
cin.tie(0) -> sync_with_stdio(0);
ll h, w;
cin >> h >> w;
string grid[h];
for (ll i = 0; i < h; i++) {
cin >> grid[i];
}
deque<pair<pair<ll, ll>, ll>> q;
q.push_front({{0, 0}, 1});
ll dist[h][w];
memset(dist, -1, sizeof(dist));
while (!q.empty()) {
pair<pair<ll, ll>, ll> cur = q.front();
q.pop_front();
if (dist[cur.first.first][cur.first.second] != -1) {
continue;
}
dist[cur.first.first][cur.first.second] = cur.second;
for (ll i = 0; i < 4; i++) {
ll nr = cur.first.first+dr[i];
ll nc = cur.first.second+dc[i];
if (nr>=0 && nr<h && nc>=0 && nc<w && grid[nr][nc]!='.') {
if (grid[cur.first.first][cur.first.second] != grid[nr][nc]) {
q.push_back({{nr, nc}, cur.second+1});
} else {
q.push_front({{nr, nc}, cur.second});
}
}
}
}
ll ans = 0;
for (ll i=0; i<h; i++) {
for (ll j=0; j<w; j++) {
ans=max(ans,dist[i][j]);
}
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
3416 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
10 ms |
3416 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
4 ms |
1116 KB |
Output is correct |
12 |
Correct |
6 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
17 ms |
3164 KB |
Output is correct |
16 |
Correct |
21 ms |
3420 KB |
Output is correct |
17 |
Correct |
9 ms |
2908 KB |
Output is correct |
18 |
Correct |
10 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
45 ms |
15860 KB |
Output is correct |
3 |
Correct |
271 ms |
159172 KB |
Output is correct |
4 |
Correct |
70 ms |
37508 KB |
Output is correct |
5 |
Correct |
135 ms |
89456 KB |
Output is correct |
6 |
Correct |
1445 ms |
261520 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
3 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
45 ms |
15964 KB |
Output is correct |
14 |
Correct |
26 ms |
9308 KB |
Output is correct |
15 |
Correct |
15 ms |
10076 KB |
Output is correct |
16 |
Correct |
24 ms |
6744 KB |
Output is correct |
17 |
Correct |
119 ms |
40784 KB |
Output is correct |
18 |
Correct |
76 ms |
39764 KB |
Output is correct |
19 |
Correct |
68 ms |
37504 KB |
Output is correct |
20 |
Correct |
61 ms |
34384 KB |
Output is correct |
21 |
Correct |
161 ms |
92404 KB |
Output is correct |
22 |
Correct |
127 ms |
89428 KB |
Output is correct |
23 |
Correct |
231 ms |
77204 KB |
Output is correct |
24 |
Correct |
135 ms |
90172 KB |
Output is correct |
25 |
Correct |
485 ms |
159120 KB |
Output is correct |
26 |
Correct |
1492 ms |
716052 KB |
Output is correct |
27 |
Correct |
1326 ms |
443492 KB |
Output is correct |
28 |
Correct |
1320 ms |
261936 KB |
Output is correct |
29 |
Correct |
1327 ms |
257500 KB |
Output is correct |
30 |
Correct |
1288 ms |
323264 KB |
Output is correct |
31 |
Correct |
680 ms |
106680 KB |
Output is correct |
32 |
Correct |
1254 ms |
435212 KB |
Output is correct |