#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef vector<int> vi;
typedef long long ll;
typedef pair<short, short> pii;
typedef vector<pii> vii;
const int MAXN = 16000100;
int n, m;
char grid[4100][4100];
bool checked[4100][4100];
deque<pii> q;
int d[4100][4100];
int curD = 0;
int main()
{
FAST_IO;
//ifstream in ("in.txt");
cin >> n >> m;
q.push_front({1,1});
FOR(i, 1, n) FOR(j, 1, m) cin >> grid[i][j];
//int cnt = 0;
while (!q.empty()) {
//int v = 0;
while (!q.empty() && (checked[q.front().f][q.front().s] || grid[q.front().f][q.front().s]=='*')) {q.pop_front();}
if (q.empty()) break;
//cnt++; //curT++;c
pii v = q.front(); q.pop_front();
checked[v.f][v.s] = true;
curD = d[v.f][v.s];
if (v.f > 1 && !checked[v.f-1][v.s] && grid[v.f-1][v.s] == grid[v.f][v.s]) {
d[v.f-1][v.s] = d[v.f][v.s];
q.push_front({v.f-1, v.s});
}
if (v.s > 1 && !checked[v.f][v.s-1] && grid[v.f][v.s-1] == grid[v.f][v.s]) {
d[v.f][v.s-1] = d[v.f][v.s];
q.push_front({v.f, v.s-1});
}
if (v.f < n && !checked[v.f+1][v.s] && grid[v.f+1][v.s] == grid[v.f][v.s]) {
d[v.f+1][v.s] = d[v.f][v.s];
q.push_front({v.f+1, v.s});
}
if (v.s < m && !checked[v.f][v.s+1] && grid[v.f][v.s+1] == grid[v.f][v.s]) {
d[v.f][v.s+1] = d[v.f][v.s];
q.push_front({v.f, v.s+1});
}
if (v.f > 1 && !checked[v.f-1][v.s] && grid[v.f-1][v.s] != grid[v.f][v.s] && grid[v.f-1][v.s] != '*') {
d[v.f-1][v.s] = d[v.f][v.s]+1;
q.push_back({v.f-1, v.s});
}
if (v.s > 1 && !checked[v.f][v.s-1] && grid[v.f][v.s-1] != grid[v.f][v.s] && grid[v.f][v.s-1] != '*') {
d[v.f][v.s-1] = d[v.f][v.s]+1;
q.push_back({v.f, v.s-1});
}
if (v.f < n && !checked[v.f+1][v.s] && grid[v.f+1][v.s] != grid[v.f][v.s] && grid[v.f+1][v.s] != '*') {
d[v.f+1][v.s] = d[v.f][v.s]+1;
q.push_back({v.f+1, v.s});
}
if (v.s < m && !checked[v.f][v.s+1] && grid[v.f][v.s+1] != grid[v.f][v.s] && grid[v.f][v.s+1] != '*') {
d[v.f][v.s+1] = d[v.f][v.s]+1;
q.push_back({v.f, v.s+1});
}
//cout << " d[("<<v.f << ", " << v.s << ") = " << d[v.f][v.s] << endl;
//dfs (q.front().f, q.front().s);
}
// dfsTree (1, -1);
cout << curD+1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
6 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
7 ms |
1664 KB |
Output is correct |
7 |
Correct |
6 ms |
1664 KB |
Output is correct |
8 |
Correct |
6 ms |
1536 KB |
Output is correct |
9 |
Correct |
6 ms |
1536 KB |
Output is correct |
10 |
Correct |
6 ms |
1664 KB |
Output is correct |
11 |
Correct |
6 ms |
1536 KB |
Output is correct |
12 |
Correct |
7 ms |
1664 KB |
Output is correct |
13 |
Correct |
6 ms |
1408 KB |
Output is correct |
14 |
Correct |
6 ms |
1664 KB |
Output is correct |
15 |
Correct |
6 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
4 |
Correct |
6 ms |
1024 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
6 |
Correct |
7 ms |
1664 KB |
Output is correct |
7 |
Correct |
6 ms |
1664 KB |
Output is correct |
8 |
Correct |
6 ms |
1536 KB |
Output is correct |
9 |
Correct |
6 ms |
1536 KB |
Output is correct |
10 |
Correct |
6 ms |
1664 KB |
Output is correct |
11 |
Correct |
6 ms |
1536 KB |
Output is correct |
12 |
Correct |
7 ms |
1664 KB |
Output is correct |
13 |
Correct |
6 ms |
1408 KB |
Output is correct |
14 |
Correct |
6 ms |
1664 KB |
Output is correct |
15 |
Correct |
6 ms |
1408 KB |
Output is correct |
16 |
Correct |
29 ms |
14072 KB |
Output is correct |
17 |
Correct |
29 ms |
13952 KB |
Output is correct |
18 |
Correct |
28 ms |
14080 KB |
Output is correct |
19 |
Correct |
30 ms |
14200 KB |
Output is correct |
20 |
Correct |
28 ms |
13824 KB |
Output is correct |
21 |
Correct |
69 ms |
16332 KB |
Output is correct |
22 |
Correct |
69 ms |
16376 KB |
Output is correct |
23 |
Correct |
70 ms |
16632 KB |
Output is correct |
24 |
Correct |
72 ms |
17144 KB |
Output is correct |
25 |
Correct |
71 ms |
17016 KB |
Output is correct |
26 |
Correct |
68 ms |
16632 KB |
Output is correct |
27 |
Correct |
66 ms |
16508 KB |
Output is correct |
28 |
Correct |
69 ms |
16376 KB |
Output is correct |
29 |
Correct |
68 ms |
17144 KB |
Output is correct |
30 |
Correct |
71 ms |
16760 KB |
Output is correct |