이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |