#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
vector<string> a;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void Solve() {
cin >> n >> m;
for(int i = 0; i < n; i++) {
string s;
cin >> s;
a.push_back(s);
}
int dist[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
dist[i][j] = INT_MAX;
}
}
dist[0][0] = 1;
deque<array<int, 2> > dq;
dq.push_back({0, 0});
int ans = 0;
while(!dq.empty()) {
int curx = dq[0][0], cury = dq[0][1];
ans = max(ans, dist[curx][cury]);
dq.pop_front();
for(int i = 0; i < 4; i++) {
int x = curx + dx[i], y = cury + dy[i];
if(x < 0 || x >= n || y < 0 || y >= m || a[x][y] == '.') {
continue;
}
if(a[x][y] == a[curx][cury] && dist[x][y] > dist[curx][cury]) {
dist[x][y] = dist[curx][cury];
dq.push_front({x, y});
}
if(a[x][y] != a[curx][cury] && dist[x][y] > dist[curx][cury] + 1) {
dist[x][y] = dist[curx][cury] + 1;
dq.push_back({x, y});
}
}
}
cout << ans << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
///cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
12 ms |
2116 KB |
Output is correct |
5 |
Correct |
2 ms |
972 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
844 KB |
Output is correct |
11 |
Correct |
2 ms |
812 KB |
Output is correct |
12 |
Correct |
6 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
972 KB |
Output is correct |
15 |
Correct |
13 ms |
2508 KB |
Output is correct |
16 |
Correct |
16 ms |
2596 KB |
Output is correct |
17 |
Correct |
10 ms |
2472 KB |
Output is correct |
18 |
Correct |
8 ms |
2128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
844 KB |
Output is correct |
2 |
Correct |
50 ms |
14184 KB |
Output is correct |
3 |
Correct |
297 ms |
141440 KB |
Output is correct |
4 |
Correct |
76 ms |
33436 KB |
Output is correct |
5 |
Correct |
215 ms |
79816 KB |
Output is correct |
6 |
Correct |
951 ms |
168092 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
844 KB |
Output is correct |
9 |
Correct |
4 ms |
844 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
51 ms |
14156 KB |
Output is correct |
14 |
Correct |
29 ms |
8336 KB |
Output is correct |
15 |
Correct |
20 ms |
9164 KB |
Output is correct |
16 |
Correct |
36 ms |
5964 KB |
Output is correct |
17 |
Correct |
139 ms |
36192 KB |
Output is correct |
18 |
Correct |
87 ms |
35648 KB |
Output is correct |
19 |
Correct |
76 ms |
33356 KB |
Output is correct |
20 |
Correct |
64 ms |
30668 KB |
Output is correct |
21 |
Correct |
165 ms |
82500 KB |
Output is correct |
22 |
Correct |
200 ms |
79772 KB |
Output is correct |
23 |
Correct |
269 ms |
68692 KB |
Output is correct |
24 |
Correct |
157 ms |
80544 KB |
Output is correct |
25 |
Correct |
582 ms |
141436 KB |
Output is correct |
26 |
Correct |
977 ms |
208752 KB |
Output is correct |
27 |
Correct |
843 ms |
177896 KB |
Output is correct |
28 |
Correct |
923 ms |
168008 KB |
Output is correct |
29 |
Correct |
907 ms |
163248 KB |
Output is correct |
30 |
Correct |
903 ms |
173036 KB |
Output is correct |
31 |
Correct |
803 ms |
91732 KB |
Output is correct |
32 |
Correct |
863 ms |
175172 KB |
Output is correct |