#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define MOD 1000000007
const int MAXN=4e3+1;
//bool vis[MAXN][MAXN];
int grid[MAXN][MAXN];
int h,w;
int xyon[4]={1,0,-1,0};
int yyon[4]={0,-1,0,1};
bool isvalid(int x,int y){
if(x>=h or x<0 or y>=w or y<0) return false;
if(grid[x][y]==-1) return false;
return true;
}
int d[MAXN][MAXN];
int ans=1;
void bfs(int x,int y){
deque<pii> q;
q.push_back({x,y});
//vis[x][y]=true;
d[x][y]=1;
while(q.size()){
auto el=q.front();
q.pop_front();
ans=max(ans,d[el.fi][el.se]);
for(int i=0;i<4;i++){
int newx=el.fi+xyon[i];
int newy=el.se+yyon[i];
if(isvalid(newx,newy) and d[newx][newy]==0){
//vis[newx][newy]=true;
if(grid[newx][newy]==grid[el.fi][el.se]){
d[newx][newy]=d[el.fi][el.se];
q.push_front({newx,newy});
}
else{
d[newx][newy]=d[el.fi][el.se]+1;
q.push_back({newx,newy});
}
//cout<<"distance of "<<newx<<'-'<<newy<<" is: "<<d[newx][newy]<<endl;
}
}
//grid[el.fi][el.se]=!grid[el.fi][el.se];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>h>>w;
map<char,int> anim;
anim['F']=1;
anim['R']=0;
anim['.']=-1;
//cout<<d[0][2]<<endl;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
char c;
cin>>c;
grid[i][j]=anim[c];
}
}
bfs(0,0);
cout<<ans<<endl;
//cout<<d[h][w]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
13656 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
8 ms |
13572 KB |
Output is correct |
5 |
Correct |
3 ms |
8024 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 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 |
4952 KB |
Output is correct |
10 |
Correct |
3 ms |
7512 KB |
Output is correct |
11 |
Correct |
2 ms |
5464 KB |
Output is correct |
12 |
Correct |
5 ms |
8044 KB |
Output is correct |
13 |
Correct |
4 ms |
8028 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
12 ms |
13656 KB |
Output is correct |
16 |
Correct |
13 ms |
13660 KB |
Output is correct |
17 |
Correct |
11 ms |
13404 KB |
Output is correct |
18 |
Correct |
8 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
77404 KB |
Output is correct |
2 |
Correct |
51 ms |
29268 KB |
Output is correct |
3 |
Correct |
312 ms |
104924 KB |
Output is correct |
4 |
Correct |
101 ms |
52696 KB |
Output is correct |
5 |
Correct |
192 ms |
82100 KB |
Output is correct |
6 |
Correct |
624 ms |
139472 KB |
Output is correct |
7 |
Correct |
15 ms |
78364 KB |
Output is correct |
8 |
Correct |
14 ms |
77404 KB |
Output is correct |
9 |
Correct |
2 ms |
2720 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
19 ms |
77916 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
56 ms |
29268 KB |
Output is correct |
14 |
Correct |
29 ms |
22364 KB |
Output is correct |
15 |
Correct |
28 ms |
26716 KB |
Output is correct |
16 |
Correct |
23 ms |
12296 KB |
Output is correct |
17 |
Correct |
125 ms |
53584 KB |
Output is correct |
18 |
Correct |
103 ms |
56860 KB |
Output is correct |
19 |
Correct |
96 ms |
52308 KB |
Output is correct |
20 |
Correct |
79 ms |
47328 KB |
Output is correct |
21 |
Correct |
195 ms |
82988 KB |
Output is correct |
22 |
Correct |
188 ms |
82004 KB |
Output is correct |
23 |
Correct |
233 ms |
67668 KB |
Output is correct |
24 |
Correct |
197 ms |
89468 KB |
Output is correct |
25 |
Correct |
504 ms |
125520 KB |
Output is correct |
26 |
Correct |
358 ms |
174864 KB |
Output is correct |
27 |
Correct |
485 ms |
170752 KB |
Output is correct |
28 |
Correct |
611 ms |
139212 KB |
Output is correct |
29 |
Correct |
636 ms |
137296 KB |
Output is correct |
30 |
Correct |
574 ms |
159268 KB |
Output is correct |
31 |
Correct |
521 ms |
112212 KB |
Output is correct |
32 |
Correct |
442 ms |
159584 KB |
Output is correct |