/*
Wei Wai Wei Wai
Zumigat nan damu dimi kwayt rayt
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
/*typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod FM(2);
*/
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 4e3 + 10;
int n, m, a[maxn][maxn];
bool vis[maxn][maxn];
int xc[4] = {1, -1, 0, 0};
int yc[4] = {0, 0, 1, -1};
inline bool isvalid(int x, int y){
return (x && x <= n && y && y <= m && !vis[x][y] && a[x][y] != 2);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
char mark;
for (int i = 1; i <= n; i++){
string s; cin >> s;
if (i == 1) mark = s[0];
for (int j = 1; j <= m; j++){
if (s[j-1] == '.') a[i][j] = 2;
else if (s[j-1] != mark) a[i][j] = 1;
}
}
queue<pii> q[2];
int ans = 0;
q[0].push({1, 1});
vis[1][1] = true;
for (int tmp = 0;; tmp++){
int idx = tmp&1;
if (q[idx].empty()) break;
ans++;
while(!q[idx].empty()){
int x = q[idx].front().F, y = q[idx].front().S;
q[idx].pop();
for (int i = 0; i < 4; i++){
int ux = x + xc[i];
int uy = y + yc[i];
if (isvalid(ux, uy)){
q[a[ux][uy]].push({ux, uy});
vis[ux][uy] = true;
}
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5396 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
712 KB |
Output is correct |
4 |
Correct |
9 ms |
5048 KB |
Output is correct |
5 |
Correct |
4 ms |
3028 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2128 KB |
Output is correct |
12 |
Correct |
7 ms |
3028 KB |
Output is correct |
13 |
Correct |
3 ms |
3028 KB |
Output is correct |
14 |
Correct |
4 ms |
3000 KB |
Output is correct |
15 |
Correct |
13 ms |
5576 KB |
Output is correct |
16 |
Correct |
13 ms |
5460 KB |
Output is correct |
17 |
Correct |
10 ms |
5272 KB |
Output is correct |
18 |
Correct |
10 ms |
5092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30908 KB |
Output is correct |
2 |
Correct |
48 ms |
17908 KB |
Output is correct |
3 |
Correct |
203 ms |
81888 KB |
Output is correct |
4 |
Correct |
77 ms |
32932 KB |
Output is correct |
5 |
Correct |
166 ms |
62196 KB |
Output is correct |
6 |
Correct |
1122 ms |
92344 KB |
Output is correct |
7 |
Correct |
17 ms |
32336 KB |
Output is correct |
8 |
Correct |
15 ms |
30900 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
19 ms |
31740 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
13 |
Correct |
49 ms |
17944 KB |
Output is correct |
14 |
Correct |
27 ms |
11920 KB |
Output is correct |
15 |
Correct |
23 ms |
13156 KB |
Output is correct |
16 |
Correct |
22 ms |
6612 KB |
Output is correct |
17 |
Correct |
128 ms |
35208 KB |
Output is correct |
18 |
Correct |
89 ms |
34920 KB |
Output is correct |
19 |
Correct |
71 ms |
33040 KB |
Output is correct |
20 |
Correct |
56 ms |
30748 KB |
Output is correct |
21 |
Correct |
118 ms |
64200 KB |
Output is correct |
22 |
Correct |
162 ms |
62276 KB |
Output is correct |
23 |
Correct |
242 ms |
52540 KB |
Output is correct |
24 |
Correct |
143 ms |
63632 KB |
Output is correct |
25 |
Correct |
396 ms |
81676 KB |
Output is correct |
26 |
Correct |
547 ms |
17148 KB |
Output is correct |
27 |
Correct |
831 ms |
83124 KB |
Output is correct |
28 |
Correct |
1145 ms |
92020 KB |
Output is correct |
29 |
Correct |
1073 ms |
90112 KB |
Output is correct |
30 |
Correct |
932 ms |
87760 KB |
Output is correct |
31 |
Correct |
714 ms |
66144 KB |
Output is correct |
32 |
Correct |
787 ms |
82012 KB |
Output is correct |