#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
const int SIZE = 1005;
int n, m, cnt;
string s[SIZE];
bool vs[SIZE][SIZE];
queue<pair<int, int>> q[2];
int type(char c) {
return c == 'B' ? 0 : c == 'T' ? 1 : 2;
}
void solve() {
cin >> n >> m;
FOR (i, 1, n) cin >> s[i], s[i] = " " + s[i];
char c = s[1][1];
q[type(s[1][1])].emplace(1, 1);
vs[1][1] = 1;
while (q[0].size() || q[1].size()) {
int t = type(c);
while (q[t].size()) {
auto [x, y] = q[t].front();
q[t].pop();
FOR (i, 0, 3) {
int tx = x + dx[i], ty = y + dy[i];
if (1 <= tx && tx <= n && 1 <= ty && ty <= m && !vs[tx][ty] && type(s[tx][ty]) <= 1) {
vs[tx][ty] = 1;
q[type(s[tx][ty])].emplace(tx, ty);
}
}
}
cnt++;
c = c == 'B' ? 'T' : 'B';
}
cout << cnt << '\n';
}
int main() {
Waimai;
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
372 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
372 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
372 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
376 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
372 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
7 ms |
3284 KB |
Output is correct |
17 |
Correct |
7 ms |
3220 KB |
Output is correct |
18 |
Correct |
6 ms |
3284 KB |
Output is correct |
19 |
Correct |
8 ms |
3304 KB |
Output is correct |
20 |
Correct |
8 ms |
3268 KB |
Output is correct |
21 |
Correct |
33 ms |
3068 KB |
Output is correct |
22 |
Correct |
34 ms |
3156 KB |
Output is correct |
23 |
Correct |
34 ms |
3176 KB |
Output is correct |
24 |
Correct |
35 ms |
3308 KB |
Output is correct |
25 |
Correct |
34 ms |
3264 KB |
Output is correct |
26 |
Correct |
34 ms |
3200 KB |
Output is correct |
27 |
Correct |
38 ms |
3284 KB |
Output is correct |
28 |
Correct |
33 ms |
3148 KB |
Output is correct |
29 |
Correct |
35 ms |
3300 KB |
Output is correct |
30 |
Correct |
35 ms |
3156 KB |
Output is correct |