#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) {
fc[i][j] = true;
vis[i][j] = true;
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])dfs(nx, ny);
} 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);
return 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 |
Incorrect |
4 ms |
9816 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
11096 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
5720 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
6744 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
5468 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
7000 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
7000 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
5720 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
5720 KB |
Output isn't correct |
15 |
Incorrect |
4 ms |
9560 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
9308 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
11352 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
46680 KB |
Output isn't correct |
2 |
Incorrect |
18 ms |
19168 KB |
Output isn't correct |
3 |
Incorrect |
138 ms |
47992 KB |
Output isn't correct |
4 |
Incorrect |
40 ms |
24656 KB |
Output isn't correct |
5 |
Incorrect |
81 ms |
38608 KB |
Output isn't correct |
6 |
Incorrect |
228 ms |
87368 KB |
Output isn't correct |
7 |
Incorrect |
10 ms |
47912 KB |
Output isn't correct |
8 |
Incorrect |
10 ms |
46680 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
4952 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
11 |
Incorrect |
10 ms |
47448 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
6744 KB |
Output isn't correct |
13 |
Incorrect |
17 ms |
19036 KB |
Output isn't correct |
14 |
Incorrect |
13 ms |
12892 KB |
Output isn't correct |
15 |
Incorrect |
11 ms |
15960 KB |
Output isn't correct |
16 |
Incorrect |
8 ms |
7000 KB |
Output isn't correct |
17 |
Incorrect |
40 ms |
28496 KB |
Output isn't correct |
18 |
Incorrect |
37 ms |
26192 KB |
Output isn't correct |
19 |
Incorrect |
41 ms |
24656 KB |
Output isn't correct |
20 |
Incorrect |
33 ms |
25168 KB |
Output isn't correct |
21 |
Incorrect |
82 ms |
41040 KB |
Output isn't correct |
22 |
Incorrect |
80 ms |
38476 KB |
Output isn't correct |
23 |
Incorrect |
70 ms |
31828 KB |
Output isn't correct |
24 |
Incorrect |
81 ms |
41040 KB |
Output isn't correct |
25 |
Incorrect |
145 ms |
48084 KB |
Output isn't correct |
26 |
Runtime error |
662 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Incorrect |
498 ms |
351308 KB |
Output isn't correct |
28 |
Incorrect |
226 ms |
87484 KB |
Output isn't correct |
29 |
Incorrect |
231 ms |
96188 KB |
Output isn't correct |
30 |
Incorrect |
238 ms |
88996 KB |
Output isn't correct |
31 |
Incorrect |
91 ms |
42064 KB |
Output isn't correct |
32 |
Incorrect |
148 ms |
48208 KB |
Output isn't correct |