Advertisement
leminhkt

noname22 (baimin)

Mar 8th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define iFOR(i, a, b) for(int i=(a); i<=(b); i++)
  3. #define iFORn(i, n) for(int i=1; i<=(n); i++)
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.         //=================declare=================
  13.     struct pos
  14.     {
  15.         int y, x;
  16.     };
  17.     int n, col, row, res, X, Y;
  18.     pos b, e;
  19.     bool a[105][105]={};
  20.     short d[105][105];
  21.     pos t[105][105];
  22.     pos k[8]={{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
  23.     queue<pos> q;
  24. //=================..end..=================
  25.         //=================function=================
  26.     void print(pos n){
  27.         if(t[n.y][n.x].x!=b.x||t[n.y][n.x].y!=b.y) print(t[n.y][n.x]);
  28.         cout<<n.y<<' '<<n.x<<'\n';
  29.     }
  30. //=================..end...=================
  31. int main(){
  32.     // freopen("baimin.inp", "r", stdin);
  33.     // freopen("baimin.out", "w", stdout);
  34.     // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  35.         //=================code=================
  36.     cin>>n;
  37.     iFORn(i, n){
  38.         cin>>col;
  39.         while(col--){
  40.             cin>>row;
  41.             a[i][row]=1;
  42.         }
  43.         a[i][0]=a[i][n+1]=a[0][i]=a[n+1][i]=1;
  44.     }
  45.     cin>>b.y>>b.x>>e.y>>e.x;
  46.     a[b.y][b.x]=a[0][0]=a[0][n+1]=a[n+1][0]=a[n+1][n+1]=1;
  47.     q.push({b.y, b.x});
  48.     while(!q.empty()&&!d[e.y][e.x]){
  49.         Y=q.front().y, X=q.front().x;
  50.         q.pop();
  51.         iFOR(i, 0, 7)
  52.             if(!a[k[i].y+Y][k[i].x+X]&&!d[k[i].y+Y][k[i].x+X]){
  53.                 q.push({k[i].y+Y, k[i].x+X});
  54.                 d[k[i].y+Y][k[i].x+X]=d[Y][X]+1;
  55.                 t[k[i].y+Y][k[i].x+X]={Y, X};
  56.             }
  57.     }
  58.     if(d[e.y][e.x]){
  59.         cout<<d[e.y][e.x]<<'\n';
  60.         print(e);
  61.     }
  62.     else cout<<"NO";
  63. //=================end.=================
  64. }
  65.  
  66. /*
  67. 5
  68. 4 1 2 3 4
  69. 2 4 5
  70. 2 1 3
  71. 3 1 3 4
  72. 1 5
  73. 2 3 1 5
  74.  
  75.  
  76. 1 1 1 1 0
  77. 0 0 0 1 1
  78. 1 0 1 0 0
  79. 1 0 1 1 0
  80. 0 0 0 0 1
  81. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement