#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl "\n"
#define ll long long
#define mp make_pair
#define ins insert
#define lb lower_bound
#define pb push_back
#define ub upper_bound
#define lll __int128
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int n, m;
cin >> n >> m;
char a[n + 5][m + 5];
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
cin >> a[i][j];
deque<pair<int, pair<int, int>>> bfs;
bfs.push_back(mp(0, mp(1, 1)));
bool vis[n + 5][m + 5];
int res = 0;
memset(vis, 0, sizeof(vis));
vector<pair<int, int>> nxt = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
function<bool(int, int)> check = [&] (int x, int y) -> int {
return x <= n && x >= 1 && y >= 1 && y <= m;
};
while(bfs.size()) {
int d = bfs.front().fi, x = bfs.front().se.fi, y = bfs.front().se.se;
bfs.pop_front();
if(vis[x][y])
continue;
vis[x][y] = 1;
res = max(res, d);
// cout << x << " " << y << " " << d << endl;
for(auto p : nxt) {
if(check(x + p.fi, y + p.se) && !vis[x + p.fi][y + p.se] && a[x + p.fi][y + p.se] != '.') {
if(a[x + p.fi][y + p.se] == a[x][y])
bfs.push_front(mp(d, mp(x + p.fi, y + p.se)));
else
bfs.pb(mp(d + 1, mp(x + p.fi, y + p.se)));
}
}
}
cout << res + 1 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
0 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 |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
9 ms |
1116 KB |
Output is correct |
16 |
Correct |
12 ms |
1116 KB |
Output is correct |
17 |
Correct |
7 ms |
1116 KB |
Output is correct |
18 |
Correct |
6 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
35 ms |
4956 KB |
Output is correct |
3 |
Correct |
233 ms |
47440 KB |
Output is correct |
4 |
Correct |
50 ms |
11344 KB |
Output is correct |
5 |
Correct |
168 ms |
26960 KB |
Output is correct |
6 |
Correct |
625 ms |
83980 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
584 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
36 ms |
4956 KB |
Output is correct |
14 |
Correct |
22 ms |
2908 KB |
Output is correct |
15 |
Correct |
17 ms |
3164 KB |
Output is correct |
16 |
Correct |
20 ms |
2396 KB |
Output is correct |
17 |
Correct |
90 ms |
12368 KB |
Output is correct |
18 |
Correct |
60 ms |
12208 KB |
Output is correct |
19 |
Correct |
50 ms |
11348 KB |
Output is correct |
20 |
Correct |
51 ms |
10328 KB |
Output is correct |
21 |
Correct |
139 ms |
27732 KB |
Output is correct |
22 |
Correct |
162 ms |
26704 KB |
Output is correct |
23 |
Correct |
205 ms |
23128 KB |
Output is correct |
24 |
Correct |
136 ms |
26960 KB |
Output is correct |
25 |
Correct |
297 ms |
47444 KB |
Output is correct |
26 |
Correct |
466 ms |
186456 KB |
Output is correct |
27 |
Correct |
544 ms |
121848 KB |
Output is correct |
28 |
Correct |
622 ms |
83712 KB |
Output is correct |
29 |
Correct |
662 ms |
79776 KB |
Output is correct |
30 |
Correct |
591 ms |
98856 KB |
Output is correct |
31 |
Correct |
510 ms |
31568 KB |
Output is correct |
32 |
Correct |
519 ms |
121316 KB |
Output is correct |