Advertisement
Josif_tepe

Untitled

Feb 6th, 2022
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.     int i;
  9.     int j;
  10.     int dis;
  11.  
  12.     node(){}
  13.     node(int _i,int _j, int _dis)
  14.     {
  15.         i=_i;
  16.         j=_j;
  17.         dis=_dis;
  18.     }
  19.  
  20.     bool operator <(const node &tmp) const
  21.     {
  22.         return dis< tmp.dis;
  23.     }
  24. };
  25. char mat[1005][1005];
  26. bool vis[1005][1005];
  27. int m[1005][1005];
  28.  
  29. int main()
  30. {
  31.     int a,b;
  32.     cin>>a>>b;
  33.    
  34.     int r2=0,c2=0;
  35.     int r=0,c=0;
  36.     queue<int>q;
  37.     for(int i=0;i<a;i++)
  38.     {
  39.         for(int j=0;j<b;j++)
  40.         {
  41.             cin>>mat[i][j];
  42.             vis[i][j]=false;
  43.             if(mat[i][j]=='G')
  44.             {
  45.                 q.push(i);
  46.                 q.push(j);
  47.                 q.push(0);
  48.                 vis[i][j]=true;
  49.             }
  50.             if(mat[i][j]=='R')
  51.             {
  52.                 r=i;
  53.                 c=j;
  54.             }
  55.             if(mat[i][j]=='Z')
  56.             {
  57.                 r2=i;
  58.                 c2=j;
  59.             }
  60.         }
  61.     }
  62.     for(int i=0;i<a;i++)
  63.     {
  64.         for(int j=0;j<b;j++)
  65.         {
  66.             m[i][j]=0;
  67.         }
  68.     }
  69.     mat[r2][c2]='.';
  70.     mat[r][c] = '.';
  71.     while(!q.empty())
  72.     {
  73.         int ci=q.front();
  74.         q.pop();
  75.         int cj=q.front();
  76.         q.pop();
  77.         int k=q.front();
  78.         q.pop();
  79.         m[ci][cj]=k;
  80.         if(ci+1<a && vis[ci+1][cj]==false && mat[ci+1][cj]=='.')
  81.         {
  82.             q.push(ci+1);
  83.             q.push(cj);
  84.             q.push(k+1);
  85.             vis[ci+1][cj]=true;
  86.         }
  87.         if(ci-1>=0 && vis[ci-1][cj]==false && mat[ci-1][cj]=='.')
  88.         {
  89.             q.push(ci-1);
  90.             q.push(cj);
  91.             q.push(k+1);
  92.             vis[ci-1][cj]=true;
  93.         }
  94.         if(cj+1<b && vis[ci][cj+1]==false && mat[ci][cj+1]=='.')
  95.         {
  96.             q.push(ci);
  97.             q.push(cj+1);
  98.             q.push(k+1);
  99.             vis[ci][cj+1]=true;
  100.         }
  101.         if(cj-1>=0 && vis[ci][cj-1]==false && mat[ci][cj-1]=='.')
  102.         {
  103.             q.push(ci);
  104.             q.push(cj-1);
  105.             q.push(k+1);
  106.             vis[ci][cj-1]=true;
  107.         }
  108.     }
  109.    
  110.     priority_queue<node>Q;
  111.     Q.push(node(r,c,m[r][c]));
  112.     for(int i = 0; i < a; i++) {
  113.         for(int j = 0; j < b; j++) {
  114.             vis[i][j] = false;
  115.         }
  116.     }
  117.     while(!Q.empty())
  118.     {
  119.         node broj=Q.top();
  120.         Q.pop();
  121.         if(broj.i==r2 && broj.j==c2)
  122.         {
  123.             cout<<broj.dis;
  124.             return 0;
  125.         }
  126.         if(broj.i+1<a && vis[broj.i+1][broj.j]==false && mat[broj.i+1][broj.j]=='.')
  127.         {
  128.             Q.push(node(broj.i+1,broj.j,min(broj.dis, m[broj.i+1][broj.j])));
  129.             vis[broj.i + 1][broj.j] = true;
  130.         }
  131.         if(broj.i-1>=0 && vis[broj.i-1][broj.j]==false && mat[broj.i-1][broj.j]=='.')
  132.         {
  133.             Q.push(node(broj.i-1,broj.j,min(broj.dis, m[broj.i-1][broj.j])));            vis[broj.i - 1][broj.j] = true;
  134.  
  135.         }
  136.         if(broj.j+1<b && vis[broj.i][broj.j+1]==false && mat[broj.i][broj.j+1]=='.')
  137.         {
  138.             Q.push(node(broj.i,broj.j+1,min(broj.dis, m[broj.i][broj.j+1])));
  139.             vis[broj.i ][broj.j + 1] = true;
  140.  
  141.         }
  142.         if(broj.j-1>=0 && vis[broj.i][broj.j-1]==false && mat[broj.i][broj.j-1]=='.')
  143.         {
  144.             Q.push(node(broj.i,broj.j-1,min(broj.dis, m[broj.i][broj.j-1])));
  145.             vis[broj.i][broj.j - 1] = true;
  146.  
  147.         }
  148.     }
  149.     return 0;
  150. }
  151.  
  152. /*
  153. 4R2343234
  154. 321232123
  155. 21G121G12
  156. 121232123
  157. G12212234
  158. 1221G1234
  159. 233**2345
  160. 345Z43456
  161. */
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement