#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX=4e3+5;
const int INF=1e18;
const int MOD=1e9+7;
const int MAXL=32;
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int g[MAX][MAX][2],h,w; // {T,R,B,L} , vertex-info (4)
bool chk(int x,int y) {
return (x>=0 && y>=0 && x<h && y<w && g[x][y][0]!=-1);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cout<<setprecision(6)<<fixed;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
cin>>h>>w;
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
char s;
cin>>s;
if(s=='R') {
g[i][j][0]=0;
} else if(s=='F') {
g[i][j][0]=1;
} else {
g[i][j][0]=-1;
}
}
}
deque<tuple<int,int,int>>dq; //{x,y,dist}
dq.push_front({0,0,1});
int ans=1;
while(!dq.empty()) {
tuple<int,int,int>curr=dq.front();
dq.pop_front();
int x=get<0>(curr),y=get<1>(curr),d=get<2>(curr);
if(g[x][y][1]>0) {
continue;
}
g[x][y][1]=d;
ans=max(ans,d);
for(int i=0;i<4;i++) {
int nx=x+dx[i],ny=y+dy[i];
if(!chk(nx,ny) || g[nx][ny][1]>0) {
continue;
}
if(g[x][y][0]==g[nx][ny][0]) {
dq.push_front({nx,ny,d});
} else {
dq.push_back({nx,ny,d+1});
}
}
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6484 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
12 ms |
5844 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
3 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
8 ms |
3028 KB |
Output is correct |
13 |
Correct |
4 ms |
2772 KB |
Output is correct |
14 |
Correct |
4 ms |
2772 KB |
Output is correct |
15 |
Correct |
20 ms |
6328 KB |
Output is correct |
16 |
Correct |
19 ms |
6484 KB |
Output is correct |
17 |
Correct |
12 ms |
5972 KB |
Output is correct |
18 |
Correct |
11 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16788 KB |
Output is correct |
2 |
Correct |
70 ms |
29848 KB |
Output is correct |
3 |
Correct |
439 ms |
251216 KB |
Output is correct |
4 |
Correct |
118 ms |
66664 KB |
Output is correct |
5 |
Correct |
282 ms |
153216 KB |
Output is correct |
6 |
Correct |
1114 ms |
325492 KB |
Output is correct |
7 |
Correct |
9 ms |
17364 KB |
Output is correct |
8 |
Correct |
8 ms |
16788 KB |
Output is correct |
9 |
Correct |
3 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
7 ms |
17108 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
69 ms |
29976 KB |
Output is correct |
14 |
Correct |
43 ms |
18204 KB |
Output is correct |
15 |
Correct |
36 ms |
19920 KB |
Output is correct |
16 |
Correct |
34 ms |
12068 KB |
Output is correct |
17 |
Correct |
167 ms |
72068 KB |
Output is correct |
18 |
Correct |
136 ms |
71028 KB |
Output is correct |
19 |
Correct |
114 ms |
66548 KB |
Output is correct |
20 |
Correct |
119 ms |
61184 KB |
Output is correct |
21 |
Correct |
275 ms |
158584 KB |
Output is correct |
22 |
Correct |
254 ms |
153256 KB |
Output is correct |
23 |
Correct |
379 ms |
131916 KB |
Output is correct |
24 |
Correct |
292 ms |
155168 KB |
Output is correct |
25 |
Correct |
722 ms |
251108 KB |
Output is correct |
26 |
Correct |
736 ms |
502704 KB |
Output is correct |
27 |
Correct |
928 ms |
409116 KB |
Output is correct |
28 |
Correct |
1190 ms |
325592 KB |
Output is correct |
29 |
Correct |
1136 ms |
310972 KB |
Output is correct |
30 |
Correct |
1002 ms |
362516 KB |
Output is correct |
31 |
Correct |
975 ms |
176104 KB |
Output is correct |
32 |
Correct |
817 ms |
388308 KB |
Output is correct |