#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef long long ll;
int H, W;
vector<string> grid;
vector<vector<ll>> visited; // bitset ?
//priority_queue<vector<ll>> q;
vector<vector<pair<ll, ll>>> pq_vec;
void dfs(int hat, int wat, ll num)
{
//cerr << hat << " " << wat << " " <<num << "\n";
//if (visited[hat][wat]>0) return;
//if (grid[hat][wat] == '.') return;
//visited[hat][wat] = num;
char ch = grid[hat][wat];
if (hat + 1 < H) {
if (grid[hat + 1][wat] != '.' && visited[hat + 1][wat] < 0)
{
if (grid[hat + 1][wat] == ch) {
pq_vec[num].push_back({hat + 1, wat});
visited[hat + 1][wat] = num;
}
else {
pq_vec[num + 1].push_back({hat + 1, wat});
visited[hat + 1][wat] = num + 1;
}
}
}
if (wat + 1 < W) {
if (grid[hat][wat + 1] != '.' && visited[hat][wat + 1] < 0)
{
if (grid[hat][wat + 1] == ch) {
pq_vec[num].push_back({hat, wat + 1});
visited[hat][wat + 1] = num;
}
else {
pq_vec[num + 1].push_back({hat, wat + 1});
visited[hat][wat + 1] = num + 1;
}
}
}
if (hat - 1 >= 0) {
if (grid[hat - 1][wat] != '.' && visited[hat - 1][wat] < 0)
{
if (grid[hat - 1][wat] == ch) {
pq_vec[num].push_back({hat - 1, wat});
visited[hat - 1][wat] = num;
}
else {
pq_vec[num + 1].push_back({hat - 1, wat});
visited[hat - 1][wat] = num + 1;
}
}
}
if (wat - 1 >= 0) {
if (grid[hat][wat - 1] != '.' && visited[hat][wat - 1] < 0)
{
if (grid[hat][wat - 1] == ch) {
pq_vec[num].push_back({hat, wat - 1});
visited[hat][wat - 1] = num;
}
else {
pq_vec[num + 1].push_back({hat, wat - 1});
visited[hat][wat - 1] = num + 1;
}
}
}
}
int32_t main()
{
cin>>H>>W;
grid.assign(H, "");
rep(i,0,H){
cin>>grid[i];
}
visited.assign(H, vector<ll>(W, -1));
//q.push({-1,0,0});
ll hw10 = H*W;
hw10 += 10;
pq_vec.assign(hw10, {});
pq_vec[1].push_back({0,0});
visited[0][0] = 1; // hm true ??
// v
ll i = 1;
while(i < ll(pq_vec.size()))
{
ll j = 0;
while (j < ll(pq_vec[i].size()))
{
pair<ll,ll> x = pq_vec[i][j];
dfs(x.first, x.second, i);
j++;
}
i++;
}
// ^
ll ans = 1;
rep(i,0,H){
rep(j,0,W){
ans = max(ans, visited[i][j]);
}
}
cout << ans;
}
// pq too slow
// H*W , int * int no good, make ll = int * int
// MLE ?
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12112 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
11 ms |
8584 KB |
Output is correct |
5 |
Correct |
4 ms |
3412 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
3 ms |
3028 KB |
Output is correct |
11 |
Correct |
3 ms |
2608 KB |
Output is correct |
12 |
Correct |
6 ms |
4564 KB |
Output is correct |
13 |
Correct |
4 ms |
3412 KB |
Output is correct |
14 |
Correct |
4 ms |
3412 KB |
Output is correct |
15 |
Correct |
18 ms |
11800 KB |
Output is correct |
16 |
Correct |
17 ms |
12116 KB |
Output is correct |
17 |
Correct |
13 ms |
10068 KB |
Output is correct |
18 |
Correct |
10 ms |
8656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2388 KB |
Output is correct |
2 |
Correct |
89 ms |
63276 KB |
Output is correct |
3 |
Correct |
722 ms |
621132 KB |
Output is correct |
4 |
Correct |
158 ms |
135588 KB |
Output is correct |
5 |
Correct |
509 ms |
434292 KB |
Output is correct |
6 |
Correct |
1476 ms |
836812 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
3 ms |
2388 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
1492 KB |
Output is correct |
11 |
Correct |
4 ms |
2132 KB |
Output is correct |
12 |
Correct |
2 ms |
1364 KB |
Output is correct |
13 |
Correct |
81 ms |
63340 KB |
Output is correct |
14 |
Correct |
48 ms |
37256 KB |
Output is correct |
15 |
Correct |
52 ms |
40780 KB |
Output is correct |
16 |
Correct |
37 ms |
27740 KB |
Output is correct |
17 |
Correct |
223 ms |
163216 KB |
Output is correct |
18 |
Correct |
224 ms |
161516 KB |
Output is correct |
19 |
Correct |
162 ms |
135528 KB |
Output is correct |
20 |
Correct |
171 ms |
135468 KB |
Output is correct |
21 |
Correct |
432 ms |
356928 KB |
Output is correct |
22 |
Correct |
509 ms |
434320 KB |
Output is correct |
23 |
Correct |
431 ms |
307584 KB |
Output is correct |
24 |
Correct |
482 ms |
373924 KB |
Output is correct |
25 |
Correct |
1089 ms |
648676 KB |
Output is correct |
26 |
Correct |
822 ms |
660056 KB |
Output is correct |
27 |
Correct |
1192 ms |
864616 KB |
Output is correct |
28 |
Correct |
1480 ms |
836872 KB |
Output is correct |
29 |
Correct |
1409 ms |
829912 KB |
Output is correct |
30 |
Correct |
1286 ms |
877364 KB |
Output is correct |
31 |
Correct |
889 ms |
494200 KB |
Output is correct |
32 |
Correct |
1088 ms |
797736 KB |
Output is correct |