#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>
using namespace std;
typedef int ll;
ll max(ll a, ll b) {
if (a > b) {
return a;
} else {
return b;
}
}
ll min(ll a, ll b) {
if (a < b) {
return a;
} else {
return b;
}
}
const ll MAXN = 4001;
const ll MAXM = 2097153;
const ll MOD = 1e9 + 7;
const ll INF = 1e7;
ll dr[4] = {0, 1, 0, -1};
ll dc[4] = {1, 0, -1, 0};
void solve(ll ca) {
ll h, w;
cin >> h >> w;
char grid[h][w];
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++) {
cin >> grid[i][j];
}
}
ll cheapest[h][w];
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++) {
cheapest[i][j] = INF;
}
}
deque<pair<ll, ll>> dq1;
deque<ll> dq2;
dq1.push_back({0, 0});
dq2.push_back(0);
ll ans = 0;
while (!dq1.empty()) {
pair<ll, ll> cur = dq1.front();
ll cos = dq2.front();
dq1.pop_front();
dq2.pop_front();
if (cheapest[cur.first][cur.second] != INF) {
continue;
}
ans = max(ans, cos);
cheapest[cur.first][cur.second] = cos;
for (ll k = 0; k < 4; k++) {
ll nr = cur.first + dr[k];
ll nc = cur.second + dc[k];
if (nr >= 0 && nr < h && nc >= 0 && nc < w && cheapest[nr][nc] == INF && grid[nr][nc] != '.') {
if (grid[nr][nc] == grid[cur.first][cur.second]) {
dq1.push_front({nr, nc});
dq2.push_front(cos);
} else {
dq1.push_back({nr, nc});
dq2.push_back(cos+1);
}
}
}
}
cout << ans+1 << endl;
}
int main() {
mt19937 rng(0);
// Fast IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
/*
freopen("piggyback.in", "r", stdin);
freopen("piggyback.out", "w", stdout);
*/
ll t = 1;
//cin >> t;
ll co = 0;
while (t--) {
solve(co);
co++;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
11 ms |
1748 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
652 KB |
Output is correct |
12 |
Correct |
7 ms |
844 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
16 ms |
1840 KB |
Output is correct |
16 |
Correct |
19 ms |
1920 KB |
Output is correct |
17 |
Correct |
13 ms |
1756 KB |
Output is correct |
18 |
Correct |
10 ms |
1736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
66 ms |
9520 KB |
Output is correct |
3 |
Correct |
458 ms |
82644 KB |
Output is correct |
4 |
Correct |
110 ms |
21452 KB |
Output is correct |
5 |
Correct |
257 ms |
46972 KB |
Output is correct |
6 |
Correct |
1142 ms |
118344 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
72 ms |
9496 KB |
Output is correct |
14 |
Correct |
36 ms |
5628 KB |
Output is correct |
15 |
Correct |
31 ms |
6140 KB |
Output is correct |
16 |
Correct |
35 ms |
4052 KB |
Output is correct |
17 |
Correct |
169 ms |
23008 KB |
Output is correct |
18 |
Correct |
120 ms |
22672 KB |
Output is correct |
19 |
Correct |
102 ms |
21340 KB |
Output is correct |
20 |
Correct |
103 ms |
19900 KB |
Output is correct |
21 |
Correct |
253 ms |
48648 KB |
Output is correct |
22 |
Correct |
256 ms |
47052 KB |
Output is correct |
23 |
Correct |
333 ms |
40712 KB |
Output is correct |
24 |
Correct |
223 ms |
47192 KB |
Output is correct |
25 |
Correct |
546 ms |
81088 KB |
Output is correct |
26 |
Correct |
1081 ms |
210916 KB |
Output is correct |
27 |
Correct |
1030 ms |
154208 KB |
Output is correct |
28 |
Correct |
1079 ms |
115392 KB |
Output is correct |
29 |
Correct |
1068 ms |
111036 KB |
Output is correct |
30 |
Correct |
1080 ms |
130536 KB |
Output is correct |
31 |
Correct |
821 ms |
51848 KB |
Output is correct |
32 |
Correct |
1012 ms |
153688 KB |
Output is correct |