Question meaning: n*m matrix a. If a[i][j]==1, you can go left and right, if a[i][j]==0, you can go up and down.
Every second can follow the above rules Move, and flip all the values of the matrix every second.
n*m<=1e5. Ask the shortest time from (sx,sy) to (tx,ty).
First n*m<=1e5 open a one-dimensional array to represent the map.
If the map does not change every second, then it is a bare BFS
Every second can follow the above rules Move, and flip all the values of the matrix every second.
n*m<=1e5. Ask the shortest time from (sx,sy) to (tx,ty).
First n*m<=1e5 open a one-dimensional array to represent the map.
If the map does not change every second, then it is a bare BFS
Now the map flips every second. The initial map is set to A and the flipped one is set to B. Then record the state of each point (x,y,st(A/B),d)
The weight of each step is 1, and you can run BFS.
#include <bits/stdc++.h> using namespace std; const int N=2e5+5; int T, n, m, sx, sy, tx, ty, res = 1e9; bool a[N*2],vis[N][2]; int dx[]={-1,1,0,0}; int dy[]={0,0,-1,1}; struct node{ int x,y,st,d; node(){} node(int xx,int yy,int stt,int dd) { x=xx,y=yy,st=stt,d=dd; } }; bool check(int x,int y) { return (x>=0&&x<n&&y>=0&&y<m); } void bfs() { memset(vis,0,sizeof(vis)); queue<node> q; q.push(node(sx,sy,0,0)); force[sx*m+sy][0]=true; while(!q.empty()) { node u=q.front(); q.pop(); int pos=u.x*m+u.y; int st=(a[pos]+u.d)%2; if(u.x==tx&&u.y==ty) res = min (res, ud); for(int i=0;i<4;i++) { int nx=u.x+dx[i],ny=u.y+dy[i]; int np=nx*m+ny,nst=1-u.st; if(check(nx,ny)==false) continue; if(i<2&&st==0||(i>=2&&i<4&&st)) { if (! vis [np] [nst]) { vis [np] [nst] = true; q.push(node(nx,ny,nst,u.d+1)); } } } } } intmain() { ios::sync_with_stdio(false); cin.tie(0); cin>>T; while(T--) { res=1e9; cin>>n>>m; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>a[i*m+j]; cin>>sx>>sy>>tx>>ty; sx-, sy-, tx-, ty--; bfs(); if(res>=1e9) res=-1; cout<<res<<'\n'; } return 0; }