#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
#define X first
#define Y second
#define mp make_pair
#define REP1(i,n) for(int i=1;i<=n;i++)
const ll N=4e3+5;
string grid[N];
bool vis[N][N];
queue<pll> que;
ll n,m,ans;
ll dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
pll dest(pll now,ll dir){
return mp(now.X+dx[dir],now.Y+dy[dir]);
}
bool invalid(pll now){
return (now.X<=0||now.X>n||now.Y<=0||now.Y>m);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP1(i,n)cin>>grid[i],grid[i]='.'+grid[i]+'.';
que.push(mp(1,1));vis[1][1]=1;
que.push(mp(n,m));vis[n][m]=1;
for(char i=grid[1][1];;i=(i=='F'?'R':'F')){
vector<pll> nxt;
++ans;
while(!(que.empty())){
pll nd=que.front();que.pop();
for(int j=0;j<4;j++){
pll to=dest(nd,j);
if(vis[to.X][to.Y]||invalid(to))continue;
if(grid[to.X][to.Y]==i){
vis[to.X][to.Y]=1;
que.push(to);
}else if(grid[to.X][to.Y]!='.'){
vis[to.X][to.Y]=1;
nxt.push_back(to);
}
}
}
if((ll)nxt.size()==0)break;
for(pll j:nxt)que.push(j);
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3052 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
8 ms |
3052 KB |
Output is correct |
5 |
Correct |
3 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
876 KB |
Output is correct |
10 |
Correct |
3 ms |
1644 KB |
Output is correct |
11 |
Correct |
2 ms |
1388 KB |
Output is correct |
12 |
Correct |
6 ms |
1900 KB |
Output is correct |
13 |
Correct |
3 ms |
1772 KB |
Output is correct |
14 |
Correct |
3 ms |
1772 KB |
Output is correct |
15 |
Correct |
12 ms |
3052 KB |
Output is correct |
16 |
Correct |
14 ms |
3052 KB |
Output is correct |
17 |
Correct |
9 ms |
2924 KB |
Output is correct |
18 |
Correct |
7 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15468 KB |
Output is correct |
2 |
Correct |
47 ms |
8556 KB |
Output is correct |
3 |
Correct |
232 ms |
49292 KB |
Output is correct |
4 |
Correct |
62 ms |
15340 KB |
Output is correct |
5 |
Correct |
207 ms |
30764 KB |
Output is correct |
6 |
Correct |
950 ms |
78856 KB |
Output is correct |
7 |
Correct |
16 ms |
16108 KB |
Output is correct |
8 |
Correct |
13 ms |
15468 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
15 ms |
15852 KB |
Output is correct |
12 |
Correct |
2 ms |
1132 KB |
Output is correct |
13 |
Correct |
46 ms |
8556 KB |
Output is correct |
14 |
Correct |
30 ms |
5868 KB |
Output is correct |
15 |
Correct |
17 ms |
6380 KB |
Output is correct |
16 |
Correct |
20 ms |
3308 KB |
Output is correct |
17 |
Correct |
132 ms |
16620 KB |
Output is correct |
18 |
Correct |
81 ms |
16236 KB |
Output is correct |
19 |
Correct |
88 ms |
15476 KB |
Output is correct |
20 |
Correct |
69 ms |
14188 KB |
Output is correct |
21 |
Correct |
136 ms |
31724 KB |
Output is correct |
22 |
Correct |
217 ms |
30700 KB |
Output is correct |
23 |
Correct |
257 ms |
26348 KB |
Output is correct |
24 |
Correct |
136 ms |
31468 KB |
Output is correct |
25 |
Correct |
253 ms |
49260 KB |
Output is correct |
26 |
Correct |
382 ms |
39148 KB |
Output is correct |
27 |
Correct |
635 ms |
52320 KB |
Output is correct |
28 |
Correct |
885 ms |
78832 KB |
Output is correct |
29 |
Correct |
901 ms |
71412 KB |
Output is correct |
30 |
Correct |
702 ms |
69312 KB |
Output is correct |
31 |
Correct |
750 ms |
35268 KB |
Output is correct |
32 |
Correct |
503 ms |
50460 KB |
Output is correct |