#include <bits/stdc++.h>
#define fr first
#define se second
#define rep(i,a,b) for(int i = a; i < (b); ++i)
#define repr(i,a,b) for(int i = a; i > (b); --i)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define IN(i,l,r) (l<i&&i<r)
#define pb push_back
#define dbg(v) \
cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
using namespace std;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef long long ll;
int h,w;
const int U = 4002;
int grid[U][U],dist[U][U];
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
bool valid(int x,int y){
return IN(x,-1,h) && IN(y,-1,w) && grid[x][y]!=1;
}
int main(){
cin.tie(0)->sync_with_stdio(false);
cin >> h >> w;
rep(i,0,h){
rep(j,0,w){
char x; cin >> x;
if(x=='.') grid[i][j]=1;
else if(x=='F') grid[i][j]=2;
else if(x=='R') grid[i][j]=3;
}
}
deque<pair<int,int>> q;
q.push_back({0,0});
dist[0][0] = 1;
while(q.size()){
pi c = q.front(); q.pop_front();
rep(k,0,4){
int x = c.fr+dx[k],y = c.se+dy[k];
if(valid(x,y) && dist[x][y] == 0){
if(grid[x][y] == grid[c.fr][c.se]){
dist[x][y] = dist[c.fr][c.se];
q.push_front({x,y});
}else{
dist[x][y] = dist[c.fr][c.se]+1;
q.push_back({x,y});
}
}
}
}
int ans = 1;
rep(i,0,h)
rep(j,0,w) ans = max(ans,dist[i][j]);
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
13896 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
8 ms |
13656 KB |
Output is correct |
5 |
Correct |
4 ms |
7900 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
7772 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
6 ms |
8028 KB |
Output is correct |
13 |
Correct |
3 ms |
8028 KB |
Output is correct |
14 |
Correct |
4 ms |
7916 KB |
Output is correct |
15 |
Correct |
12 ms |
13624 KB |
Output is correct |
16 |
Correct |
14 ms |
13816 KB |
Output is correct |
17 |
Correct |
11 ms |
13660 KB |
Output is correct |
18 |
Correct |
7 ms |
13660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
77404 KB |
Output is correct |
2 |
Correct |
52 ms |
30780 KB |
Output is correct |
3 |
Correct |
301 ms |
120548 KB |
Output is correct |
4 |
Correct |
95 ms |
55984 KB |
Output is correct |
5 |
Correct |
176 ms |
90964 KB |
Output is correct |
6 |
Correct |
658 ms |
154508 KB |
Output is correct |
7 |
Correct |
16 ms |
80216 KB |
Output is correct |
8 |
Correct |
15 ms |
77360 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
15 ms |
79964 KB |
Output is correct |
12 |
Correct |
2 ms |
5292 KB |
Output is correct |
13 |
Correct |
50 ms |
30808 KB |
Output is correct |
14 |
Correct |
29 ms |
23128 KB |
Output is correct |
15 |
Correct |
30 ms |
27732 KB |
Output is correct |
16 |
Correct |
23 ms |
12372 KB |
Output is correct |
17 |
Correct |
139 ms |
53588 KB |
Output is correct |
18 |
Correct |
109 ms |
60796 KB |
Output is correct |
19 |
Correct |
92 ms |
56108 KB |
Output is correct |
20 |
Correct |
72 ms |
47440 KB |
Output is correct |
21 |
Correct |
184 ms |
91852 KB |
Output is correct |
22 |
Correct |
185 ms |
90748 KB |
Output is correct |
23 |
Correct |
238 ms |
75012 KB |
Output is correct |
24 |
Correct |
180 ms |
88784 KB |
Output is correct |
25 |
Correct |
542 ms |
141432 KB |
Output is correct |
26 |
Correct |
579 ms |
174884 KB |
Output is correct |
27 |
Correct |
549 ms |
161112 KB |
Output is correct |
28 |
Correct |
685 ms |
154340 KB |
Output is correct |
29 |
Correct |
632 ms |
153152 KB |
Output is correct |
30 |
Correct |
598 ms |
154028 KB |
Output is correct |
31 |
Correct |
488 ms |
112120 KB |
Output is correct |
32 |
Correct |
510 ms |
149892 KB |
Output is correct |