#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int N = 4005;
int n, m, dx[]={-1, 0, 0, 1}, dy[]={0, -1, 1, 0};
char a[N][N];
bool vis[N][N];
queue<pll> Q, Q2;
inline bool chk(int x, int y)
{ return x >= 1 && x <= n && y >= 1 && y <= m; }
int main(void)
{ IO
int i, ans = 0;
cin >> n >> m;
for (i=1; i <= n; ++i)
for (int j=1; j <= m; ++j) cin >> a[i][j];
if (a[1][1] != a[n][m]) assert(0);
Q.push({1, 1}), vis[1][1] = 1;
while (1)
{
++ans;
while (!Q.empty())
{
int x = Q.front().F, y = Q.front().S; Q.pop();
for (int d=0; d < 4; ++d)
{
int xx = x + dx[d], yy = y + dy[d];
if (!chk(xx, yy) || vis[xx][yy] || a[xx][yy] == '.') continue;
vis[xx][yy] = 1;
if (a[xx][yy] != a[x][y]) Q2.push({xx, yy});
else Q.push({xx, yy});
}
}
if (Q2.empty()) break;
while (!Q2.empty())
Q.push(Q2.front()), Q2.pop();
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
7 ms |
6816 KB |
Output is correct |
5 |
Correct |
3 ms |
5976 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2520 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
3 ms |
5464 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
6 ms |
5724 KB |
Output is correct |
13 |
Correct |
3 ms |
5720 KB |
Output is correct |
14 |
Correct |
3 ms |
5724 KB |
Output is correct |
15 |
Correct |
10 ms |
6744 KB |
Output is correct |
16 |
Correct |
11 ms |
6748 KB |
Output is correct |
17 |
Correct |
9 ms |
6492 KB |
Output is correct |
18 |
Correct |
7 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
31312 KB |
Output is correct |
2 |
Correct |
44 ms |
12884 KB |
Output is correct |
3 |
Correct |
256 ms |
47280 KB |
Output is correct |
4 |
Correct |
63 ms |
21588 KB |
Output is correct |
5 |
Correct |
174 ms |
35260 KB |
Output is correct |
6 |
Correct |
719 ms |
67932 KB |
Output is correct |
7 |
Correct |
10 ms |
31836 KB |
Output is correct |
8 |
Correct |
10 ms |
31456 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
9 ms |
31496 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
42 ms |
13064 KB |
Output is correct |
14 |
Correct |
25 ms |
11096 KB |
Output is correct |
15 |
Correct |
19 ms |
11356 KB |
Output is correct |
16 |
Correct |
20 ms |
6728 KB |
Output is correct |
17 |
Correct |
109 ms |
22724 KB |
Output is correct |
18 |
Correct |
74 ms |
22352 KB |
Output is correct |
19 |
Correct |
61 ms |
21584 KB |
Output is correct |
20 |
Correct |
69 ms |
18772 KB |
Output is correct |
21 |
Correct |
155 ms |
36292 KB |
Output is correct |
22 |
Correct |
173 ms |
35152 KB |
Output is correct |
23 |
Correct |
207 ms |
30004 KB |
Output is correct |
24 |
Correct |
151 ms |
36152 KB |
Output is correct |
25 |
Correct |
312 ms |
47452 KB |
Output is correct |
26 |
Correct |
349 ms |
42580 KB |
Output is correct |
27 |
Correct |
475 ms |
50772 KB |
Output is correct |
28 |
Correct |
694 ms |
68196 KB |
Output is correct |
29 |
Correct |
732 ms |
64852 KB |
Output is correct |
30 |
Correct |
622 ms |
62588 KB |
Output is correct |
31 |
Correct |
490 ms |
38224 KB |
Output is correct |
32 |
Correct |
461 ms |
49028 KB |
Output is correct |