#include <bits/stdc++.h>
#include <deque>
#define ll long long
#define ull unsigned long long
#define pii std::pair<int, int>
#define IO (std::string) ""
std::ifstream fin(IO + ".in");
std::ofstream fout(IO + ".out");
#define NMAX 4001
#define INF INT_MAX
int di[] = {1, -1, 0, 0}, dj[] = {0, 0, 1, -1};
int h, w;
char map[NMAX][NMAX];
void citire() {
std::cin >> h >> w;
for (int i = 0; i < h; i++)
std::cin >> map[i];
}
int dist[NMAX][NMAX];
bool inside(int i, int j) {
return (i > -1 && i < h && j > -1 && j < w && map[i][j] != '.');
}
int bfs01() {
std::deque<pii> q;
q.push_back({0, 0});
dist[0][0] = 1;
int ans = 1;
while (q.size()) {
pii c = q.front();
q.pop_front();
ans = std::max(ans, dist[c.first][c.second]);
for (int i = 0; i < 4; i++) {
int x = c.first + di[i], y = c.second + dj[i];
if (inside(x, y) && dist[x][y] == 0) {
if (map[x][y] == map[c.first][c.second]) {
dist[x][y] = dist[c.first][c.second];
q.push_front({x, y});
} else {
dist[x][y] = dist[c.first][c.second] + 1;
q.push_back({x, y});
}
}
}
}
return ans;
}
int main() {
std::cin.tie(0)->std::ios::sync_with_stdio(0);
citire();
std::cout << bfs01();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 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 |
5 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
3 ms |
3164 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
6 ms |
5244 KB |
Output is correct |
16 |
Correct |
8 ms |
5468 KB |
Output is correct |
17 |
Correct |
6 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
30812 KB |
Output is correct |
2 |
Correct |
26 ms |
15224 KB |
Output is correct |
3 |
Correct |
126 ms |
69976 KB |
Output is correct |
4 |
Correct |
43 ms |
32164 KB |
Output is correct |
5 |
Correct |
125 ms |
51800 KB |
Output is correct |
6 |
Correct |
368 ms |
104696 KB |
Output is correct |
7 |
Correct |
14 ms |
32348 KB |
Output is correct |
8 |
Correct |
12 ms |
30848 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
12 ms |
31572 KB |
Output is correct |
12 |
Correct |
1 ms |
1624 KB |
Output is correct |
13 |
Correct |
27 ms |
15060 KB |
Output is correct |
14 |
Correct |
14 ms |
10324 KB |
Output is correct |
15 |
Correct |
13 ms |
13148 KB |
Output is correct |
16 |
Correct |
12 ms |
5788 KB |
Output is correct |
17 |
Correct |
63 ms |
28224 KB |
Output is correct |
18 |
Correct |
48 ms |
35184 KB |
Output is correct |
19 |
Correct |
43 ms |
32340 KB |
Output is correct |
20 |
Correct |
32 ms |
25428 KB |
Output is correct |
21 |
Correct |
82 ms |
50716 KB |
Output is correct |
22 |
Correct |
114 ms |
51400 KB |
Output is correct |
23 |
Correct |
147 ms |
42576 KB |
Output is correct |
24 |
Correct |
80 ms |
47956 KB |
Output is correct |
25 |
Correct |
181 ms |
90796 KB |
Output is correct |
26 |
Correct |
203 ms |
128344 KB |
Output is correct |
27 |
Correct |
276 ms |
117660 KB |
Output is correct |
28 |
Correct |
358 ms |
104444 KB |
Output is correct |
29 |
Correct |
346 ms |
102948 KB |
Output is correct |
30 |
Correct |
467 ms |
106712 KB |
Output is correct |
31 |
Correct |
304 ms |
71372 KB |
Output is correct |
32 |
Correct |
233 ms |
102920 KB |
Output is correct |