#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 4004;
char a[N][N];
bool used[N][N];
void solve(){
int n, m;
cin >> n >> m;
int animals_cnt = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> a[i][j];
animals_cnt += a[i][j] == 'F' || a[i][j] == 'R';
}
}
char animal = a[0][0];
if (animal == '.'){
cout << 0 << endl;
return;
}
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
int ans = 0;
queue<pair<int, int>> q;
q.emplace(0, 0);
used[0][0] = true;
queue<pair<int, int>> _q;
while (animals_cnt){
ans++;
while (!q.empty()){
auto [x, y] = q.front();
q.pop();
if (a[x][y] == animal) animals_cnt--;
a[x][y] = '?';
for (int it = 0; it < 4; it++){
int i = x + dx[it];
int j = y + dy[it];
if (i >= 0 && i < n && j >= 0 && j < m && !used[i][j]){
if (a[i][j] == '?' || a[i][j] == animal){
q.emplace(i, j);
} else if (a[i][j] != '.'){
_q.emplace(i, j);
}
used[i][j] = true;
}
}
}
q.swap(_q);
animal ^= 'F' ^ 'R';
}
cout << ans << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:91:13: warning: unused variable 'startTime' [-Wunused-variable]
91 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
4180 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
10 ms |
4308 KB |
Output is correct |
5 |
Correct |
4 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
468 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 |
4 ms |
2296 KB |
Output is correct |
11 |
Correct |
4 ms |
1876 KB |
Output is correct |
12 |
Correct |
7 ms |
2612 KB |
Output is correct |
13 |
Correct |
4 ms |
2516 KB |
Output is correct |
14 |
Correct |
5 ms |
2564 KB |
Output is correct |
15 |
Correct |
15 ms |
4280 KB |
Output is correct |
16 |
Correct |
16 ms |
4236 KB |
Output is correct |
17 |
Correct |
12 ms |
4088 KB |
Output is correct |
18 |
Correct |
10 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
30200 KB |
Output is correct |
2 |
Correct |
68 ms |
10072 KB |
Output is correct |
3 |
Correct |
420 ms |
31728 KB |
Output is correct |
4 |
Correct |
108 ms |
15096 KB |
Output is correct |
5 |
Correct |
287 ms |
23716 KB |
Output is correct |
6 |
Correct |
1068 ms |
41824 KB |
Output is correct |
7 |
Correct |
16 ms |
31620 KB |
Output is correct |
8 |
Correct |
17 ms |
30180 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
16 ms |
30988 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
80 ms |
10132 KB |
Output is correct |
14 |
Correct |
42 ms |
7472 KB |
Output is correct |
15 |
Correct |
31 ms |
8048 KB |
Output is correct |
16 |
Correct |
30 ms |
3484 KB |
Output is correct |
17 |
Correct |
173 ms |
16128 KB |
Output is correct |
18 |
Correct |
111 ms |
15976 KB |
Output is correct |
19 |
Correct |
106 ms |
15084 KB |
Output is correct |
20 |
Correct |
94 ms |
14060 KB |
Output is correct |
21 |
Correct |
246 ms |
24512 KB |
Output is correct |
22 |
Correct |
320 ms |
23828 KB |
Output is correct |
23 |
Correct |
326 ms |
19964 KB |
Output is correct |
24 |
Correct |
271 ms |
24636 KB |
Output is correct |
25 |
Correct |
452 ms |
31660 KB |
Output is correct |
26 |
Correct |
500 ms |
27824 KB |
Output is correct |
27 |
Correct |
840 ms |
33464 KB |
Output is correct |
28 |
Correct |
1079 ms |
42024 KB |
Output is correct |
29 |
Correct |
1018 ms |
40428 KB |
Output is correct |
30 |
Correct |
926 ms |
39100 KB |
Output is correct |
31 |
Correct |
724 ms |
25852 KB |
Output is correct |
32 |
Correct |
707 ms |
32444 KB |
Output is correct |