Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define iFOR(i, a, b) for(int i=(a); i<=(b); i++)
- #define iFORn(i, n) for(int i=1; i<=(n); i++)
- using namespace std;
- //=================declare=================
- struct pos
- {
- int y, x;
- };
- int n, col, row, res, X, Y;
- pos b, e;
- bool a[105][105]={};
- short d[105][105];
- pos t[105][105];
- pos k[8]={{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
- queue<pos> q;
- //=================..end..=================
- //=================function=================
- void print(pos n){
- if(t[n.y][n.x].x!=b.x||t[n.y][n.x].y!=b.y) print(t[n.y][n.x]);
- cout<<n.y<<' '<<n.x<<'\n';
- }
- //=================..end...=================
- int main(){
- // freopen("baimin.inp", "r", stdin);
- // freopen("baimin.out", "w", stdout);
- // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- //=================code=================
- cin>>n;
- iFORn(i, n){
- cin>>col;
- while(col--){
- cin>>row;
- a[i][row]=1;
- }
- a[i][0]=a[i][n+1]=a[0][i]=a[n+1][i]=1;
- }
- cin>>b.y>>b.x>>e.y>>e.x;
- a[b.y][b.x]=a[0][0]=a[0][n+1]=a[n+1][0]=a[n+1][n+1]=1;
- q.push({b.y, b.x});
- while(!q.empty()&&!d[e.y][e.x]){
- Y=q.front().y, X=q.front().x;
- q.pop();
- iFOR(i, 0, 7)
- if(!a[k[i].y+Y][k[i].x+X]&&!d[k[i].y+Y][k[i].x+X]){
- q.push({k[i].y+Y, k[i].x+X});
- d[k[i].y+Y][k[i].x+X]=d[Y][X]+1;
- t[k[i].y+Y][k[i].x+X]={Y, X};
- }
- }
- if(d[e.y][e.x]){
- cout<<d[e.y][e.x]<<'\n';
- print(e);
- }
- else cout<<"NO";
- //=================end.=================
- }
- /*
- 5
- 4 1 2 3 4
- 2 4 5
- 2 1 3
- 3 1 3 4
- 1 5
- 2 3 1 5
- 1 1 1 1 0
- 0 0 0 1 1
- 1 0 1 0 0
- 1 0 1 1 0
- 0 0 0 0 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement