#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const int INF = 2e9;
const int MAXN = 4000;
char mat[MAXN + 1][MAXN + 1];
int dist[MAXN + 1][MAXN + 1];
char dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1};
int n, m;
inline bool in(int l, int c) {
return 1 <= l && l <= n && 1 <= c && c <= m;
}
deque < pair <short, short> > deq;
inline int solve() {
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
dist[i][j] = INF;
}
}
deq.push_back({1, 1});
dist[1][1] = 1;
while(deq.size()) {
int l = deq.front().first;
int c = deq.front().second;
deq.pop_front();
for(i = 0; i < 4; i++) {
int lin = l + dl[i];
int col = c + dc[i];
if(in(lin, col) && mat[lin][col] != '.') {
int cur = dist[l][c] + (mat[l][c] != mat[lin][col]);
if(dist[lin][col] > cur) {
dist[lin][col] = cur;
if(mat[lin][col] == mat[l][c]) {
deq.push_front({lin, col});
}
else {
deq.push_back({lin, col});
}
}
}
}
}
int ans = 0;
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
if(mat[i][j] != '.') {
ans = max(ans, dist[i][j]);
}
}
}
return ans;
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, j;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
int cntF = 0, cntR = 0;
for(i = 1; i <= n; i++) {
cin >> mat[i] + 1;
for(j = 1; j <= m; j++) {
if(mat[i][j] == 'R') {
cntR++;
}
if(mat[i][j] == 'F') {
cntF++;
}
}
}
if(cntF + cntR == 0) {
cout << 0;
return 0;
}
int ans = solve();
cout << ans;
//cin.close();
//cout.close();
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:75:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin >> mat[i] + 1;
~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5240 KB |
Output is correct |
2 |
Correct |
2 ms |
5240 KB |
Output is correct |
3 |
Correct |
2 ms |
5240 KB |
Output is correct |
4 |
Correct |
11 ms |
5240 KB |
Output is correct |
5 |
Correct |
5 ms |
5240 KB |
Output is correct |
6 |
Correct |
2 ms |
5240 KB |
Output is correct |
7 |
Correct |
2 ms |
5240 KB |
Output is correct |
8 |
Correct |
3 ms |
5240 KB |
Output is correct |
9 |
Correct |
3 ms |
5240 KB |
Output is correct |
10 |
Correct |
5 ms |
5240 KB |
Output is correct |
11 |
Correct |
5 ms |
5240 KB |
Output is correct |
12 |
Correct |
8 ms |
5240 KB |
Output is correct |
13 |
Correct |
6 ms |
5240 KB |
Output is correct |
14 |
Correct |
6 ms |
5240 KB |
Output is correct |
15 |
Correct |
20 ms |
5520 KB |
Output is correct |
16 |
Correct |
17 ms |
5520 KB |
Output is correct |
17 |
Correct |
14 ms |
5520 KB |
Output is correct |
18 |
Correct |
11 ms |
5520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
31080 KB |
Output is correct |
2 |
Correct |
55 ms |
31080 KB |
Output is correct |
3 |
Correct |
271 ms |
78796 KB |
Output is correct |
4 |
Correct |
97 ms |
78796 KB |
Output is correct |
5 |
Correct |
193 ms |
78796 KB |
Output is correct |
6 |
Correct |
823 ms |
85884 KB |
Output is correct |
7 |
Correct |
27 ms |
85884 KB |
Output is correct |
8 |
Correct |
25 ms |
85884 KB |
Output is correct |
9 |
Correct |
4 ms |
85884 KB |
Output is correct |
10 |
Correct |
2 ms |
85884 KB |
Output is correct |
11 |
Correct |
25 ms |
85884 KB |
Output is correct |
12 |
Correct |
3 ms |
85884 KB |
Output is correct |
13 |
Correct |
54 ms |
85884 KB |
Output is correct |
14 |
Correct |
33 ms |
85884 KB |
Output is correct |
15 |
Correct |
33 ms |
85884 KB |
Output is correct |
16 |
Correct |
26 ms |
85884 KB |
Output is correct |
17 |
Correct |
133 ms |
85884 KB |
Output is correct |
18 |
Correct |
116 ms |
85884 KB |
Output is correct |
19 |
Correct |
89 ms |
85884 KB |
Output is correct |
20 |
Correct |
70 ms |
85884 KB |
Output is correct |
21 |
Correct |
169 ms |
85884 KB |
Output is correct |
22 |
Correct |
195 ms |
85884 KB |
Output is correct |
23 |
Correct |
252 ms |
85884 KB |
Output is correct |
24 |
Correct |
174 ms |
85884 KB |
Output is correct |
25 |
Correct |
520 ms |
85884 KB |
Output is correct |
26 |
Correct |
361 ms |
94240 KB |
Output is correct |
27 |
Correct |
592 ms |
94240 KB |
Output is correct |
28 |
Correct |
795 ms |
94240 KB |
Output is correct |
29 |
Correct |
753 ms |
94240 KB |
Output is correct |
30 |
Correct |
687 ms |
94240 KB |
Output is correct |
31 |
Correct |
602 ms |
94240 KB |
Output is correct |
32 |
Correct |
512 ms |
94240 KB |
Output is correct |