Advertisement
DarkTXYZ

CUBE-038: Icy Floor

Apr 29th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using pii = pair<int,pair<int,int>>;
  6. int fl[1001][1001];
  7. bool visited[1001][1001];
  8. int n;
  9. int dx[] = {0,0,1,-1};
  10. int dy[] = {1,-1,0,0};
  11. const int INF = 1e9;
  12.  
  13. bool chpos(int x,int y){
  14.     return x>=1 and x<=n and y>=1 and y<=n;
  15. }
  16.  
  17. pii walk(int x,int y,int dxt,int dyt,int dis){
  18.     while(chpos(x+dxt,y+dyt) and fl[x+dxt][y+dyt]!=-1){
  19.         x += dxt;
  20.         y += dyt;
  21.         dis++;
  22.     }
  23.     return {dis,{x,y}};
  24. }
  25.  
  26. int main(){
  27.     scanf("%d",&n);
  28.     for(int i=1;i<=n;i++){
  29.         for(int j=1;j<=n;j++){
  30.             scanf("%d",&fl[i][j]);
  31.             if(fl[i][j] == 1)
  32.                 fl[i][j] = -1;
  33.         }
  34.     }
  35.     priority_queue<pii,vector<pii>,greater<pii> > Q;
  36.     Q.push({0,{1,1}});
  37.     while(!Q.empty()){
  38.         int x = Q.top().second.first;
  39.         int y = Q.top().second.second;
  40.         int d = Q.top().first;
  41.         Q.pop();
  42.         if(x==n and y==n){
  43.             printf("%d",d);
  44.             break;
  45.         }
  46.         if(visited[x][y])
  47.             continue;
  48.         visited[x][y] = true;
  49.         for(int i=0;i<4;i++){
  50.             if(chpos(x+dx[i],y+dy[i])){
  51.                 Q.push(walk(x,y,dx[i],dy[i],d));
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement