Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- using pii=pair<int,int>;
- using pipii=pair<int,pii>;
- int dis[1010][1010];
- bool visited[1010][1010];
- pii walk[4]={{0,1},{1,0},{0,-1},{-1,0}};//R,D,L,U;
- int main(){
- int n;
- scanf("%d",&n);
- bool a[n][n];
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- scanf("%d",&a[i][j]);
- dis[i][j]=2e9;
- }
- }
- dis[0][0]=0;
- priority_queue<pipii,vector<pipii>,greater<pipii>> Q;
- Q.push({0,{0,0}});
- while(!Q.empty()){
- int d=Q.top().first;
- int x=Q.top().second.first;
- int y=Q.top().second.second;
- Q.pop();
- if(x==n-1&&y==n-1){
- printf("%d",d);
- return 0;
- }
- if(visited[x][y]) continue;
- visited[x][y]=true;
- for(int i=0;i<4;i++){
- int nd=d;
- int nx=x;
- int ny=y;
- while(true){
- nx+=walk[i].first;
- ny+=walk[i].second;
- nd++;
- if(nx<0||ny<0||nx==n||ny==n||a[nx][ny]==1){
- ny-=walk[i].second;
- nx-=walk[i].first;
- nd-=1;
- if(nd<dis[nx][ny]&&!visited[nx][ny]){
- dis[nx][ny]=nd;
- Q.push({nd,{nx,ny}});
- }
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement