#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 4000;
int n, m, ans = 1;
char c[N][N];
bool fc[N][N];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
vector<pair<int, int>> f, r;
bool vis[N][N];
void dfs(int i, int j) {
stack<pair<int, int>> q;
q.push({i, j});
while (!q.empty()) {
i = q.top().F, j = q.top().S;
q.pop();
for (int ii = 0; ii < 4; ii++) {
int nx = i + dx[ii], ny = j + dy[ii];
if (nx < 0 || ny < 0 || nx >= n || ny >= m)continue;
if (c[nx][ny] == c[0][0]) {
if (!fc[nx][ny]) {
q.push({nx, ny});
vis[nx][ny] = fc[nx][ny] = true;
}
} else if (!vis[nx][ny]) {
if (c[nx][ny] == 'F')f.pb({nx, ny});
else if (c[nx][ny] == 'R')r.pb({nx, ny});
vis[nx][ny] = true;
}
}
}
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c[i][j];
}
}
dfs(0, 0);
char cur = 'R';
if (c[0][0] == cur)cur = 'F';
while (!f.empty() || !r.empty()) {
if (cur == 'R') {
if (!r.empty())ans++;
while (!r.empty()) {
int x = r.back().F, y = r.back().S;
r.pop_back();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= m)continue;
if (vis[nx][ny])continue;
vis[nx][ny] = true;
if (c[nx][ny] == 'R')r.pb({nx, ny});
else if (c[nx][ny] == 'F')f.pb({nx, ny});
}
}
} else {
if (!f.empty()) {
ans++;
}
while (!f.empty()) {
int x = f.back().F, y = f.back().S;
f.pop_back();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= m)continue;
if (vis[nx][ny])continue;
vis[nx][ny] = true;
if (c[nx][ny] == 'R')r.pb({nx, ny});
else if (c[nx][ny] == 'F')f.pb({nx, ny});
}
}
}
if (cur == 'F')cur = 'R';
else cur = 'F';
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9564 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
6 ms |
9952 KB |
Output is correct |
5 |
Correct |
2 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
6796 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5592 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
4 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
9 ms |
9696 KB |
Output is correct |
16 |
Correct |
11 ms |
9564 KB |
Output is correct |
17 |
Correct |
9 ms |
9432 KB |
Output is correct |
18 |
Correct |
8 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
46172 KB |
Output is correct |
2 |
Correct |
35 ms |
17500 KB |
Output is correct |
3 |
Correct |
225 ms |
47420 KB |
Output is correct |
4 |
Correct |
57 ms |
24404 KB |
Output is correct |
5 |
Correct |
157 ms |
37972 KB |
Output is correct |
6 |
Correct |
476 ms |
71788 KB |
Output is correct |
7 |
Correct |
11 ms |
47192 KB |
Output is correct |
8 |
Correct |
10 ms |
46172 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
11 ms |
46940 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
43 ms |
17584 KB |
Output is correct |
14 |
Correct |
21 ms |
13404 KB |
Output is correct |
15 |
Correct |
17 ms |
15708 KB |
Output is correct |
16 |
Correct |
21 ms |
7260 KB |
Output is correct |
17 |
Correct |
88 ms |
27608 KB |
Output is correct |
18 |
Correct |
60 ms |
25972 KB |
Output is correct |
19 |
Correct |
59 ms |
24544 KB |
Output is correct |
20 |
Correct |
70 ms |
24916 KB |
Output is correct |
21 |
Correct |
137 ms |
40528 KB |
Output is correct |
22 |
Correct |
172 ms |
38088 KB |
Output is correct |
23 |
Correct |
163 ms |
31956 KB |
Output is correct |
24 |
Correct |
175 ms |
40388 KB |
Output is correct |
25 |
Correct |
285 ms |
47300 KB |
Output is correct |
26 |
Correct |
336 ms |
94296 KB |
Output is correct |
27 |
Correct |
441 ms |
73936 KB |
Output is correct |
28 |
Correct |
468 ms |
71596 KB |
Output is correct |
29 |
Correct |
470 ms |
68404 KB |
Output is correct |
30 |
Correct |
471 ms |
76836 KB |
Output is correct |
31 |
Correct |
322 ms |
43980 KB |
Output is correct |
32 |
Correct |
360 ms |
64956 KB |
Output is correct |