DreamGrid City is a city with intersections arranged into a grid of rows and columns. The intersection on the -th row and the -th column can be described as , and two intersections and are connected by a road if .
At each intersection stands a traffic light. A traffic light can only be in one of the two states: 0 and 1. If the traffic light at the intersection is in state 0, one can only move from to or ; If the traffic light is in state 1, one can only move from to or (of course, the destination must be another intersection in the city).
BaoBao lives at the intersection , and he wants to visit his best friend DreamGrid living at the intersection . After his departure, in each minute the following things will happen in order:
- BaoBao moves from his current intersection to another neighboring intersection along a road. As a law-abiding citizen, BaoBao has to obey the traffic light rules when moving.
- Every traffic light changes its state. If a traffic light is in state 0, it will switch to state 1; If a traffic light is in state 1, it will switch to state 0.
As an energetic young man, BaoBao doesn't want to wait for the traffic lights, and he must move in each minute until he arrives at DreamGrid's house. Please tell BaoBao the shortest possible time he can move from to to meet his friend, or tell him that this is impossible.
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers and (), indicating the size of the city.
For the following lines, the -th line contains integers (), where indicates the initial state of the traffic light at intersection .
The next line contains four integers , , and (, ), indicating the starting intersection and the destination intersection.
It's guaranteed that the sum of over all test cases will not exceed .
Output
For each test case output one line containing one integer, indicating the shortest possible time (in minute) BaoBao can move from to without stopping. If it is impossible for BaoBao to arrive at DreamGrid's house, print "-1" (without quotes) instead.
Sample Input
4 2 3 1 1 0 0 1 0 1 3 2 1 2 3 1 0 0 1 1 0 1 3 1 2 2 2 1 0 1 0 1 1 2 2 1 2 0 1 1 1 1 1
Sample Output
3 5 -1 0
Hint
For the first sample test case, BaoBao can follow this path: .
For the second sample test case, due to the traffic light rules, BaoBao can't go from to directly. Instead, he should follow this path: .
For the third sample test case, it's easy to discover that BaoBao can only go back and forth between and .
题意:给出起始点和末尾点,再给出红绿灯路径(只有0 1),路径上是1的点可以左右走,路径上是2的点可以前后走,每隔一秒红绿灯会发生变化,之前是0的变为1,1的变为0。
思路:BFS,对于红绿灯的变化问题,因为只有01态,所以我用了2个数字来存贮
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100005;
int map1[N];
int map2[N];
int vis[N];
int m,n;
int d0[2][2]= {-1,0,1,0}; //1 0
int d1[2][2]= {0,-1,0,1};
int sx,sy,ex,ey;
struct node
{
int x,y;
int step;
friend bool operator<(const node &a,const node &b)//运算符重载对优先队列里的小于号进行的重载
{
return a.step>b.step;//时间小的先出队
}
};
int bfs(int x,int y)
{
node st,nt;
priority_queue<node> q;
st.x=x;
st.y=y;
st.step=0;
q.push(st);
int pos=(x-1)*m+y;
vis[pos]=1;
while(!q.empty())
{
int pos1,pos2;
st=q.top();
q.pop();
// cout<<st.step<<endl;
if(st.x==ex&&st.y==ey)
return st.step;
pos1=(st.x-1)*m+st.y;
if(st.step%2==0){
if(map1[pos1]==1)
for(int i=0; i<2; i++)
{
nt.x=st.x+d1[i][0];
nt.y=st.y+d1[i][1];
pos2=(nt.x-1)*m+nt.y;
if(!vis[pos2] && nt.x>=1 && nt.x<=n && nt.y>=1 && nt.y<=m)
{
//cout<<nt.x<<" "<<nt.y<<" "<<nt.step<<endl;
vis[pos2]=1;
nt.step=st.step+1;
q.push(nt);
}
}
else
for(int i=0; i<2; i++)
{
nt.x=st.x+d0[i][0];
nt.y=st.y+d0[i][1];
pos2=(nt.x-1)*m+nt.y;
if(!vis[pos2] && nt.x>=1 && nt.x<=n && nt.y>=1 && nt.y<=m)
{
// cout<<nt.x<<" "<<nt.y<<" "<<nt.step<<endl;
vis[pos2]=1;
nt.step=st.step+1;
q.push(nt);
}
}
}
else{
if(map2[pos1]==1)
for(int i=0; i<2; i++)
{
nt.x=st.x+d1[i][0];
nt.y=st.y+d1[i][1];
pos2=(nt.x-1)*m+nt.y;
if(!vis[pos2] && nt.x>=1 && nt.x<=n && nt.y>=1 && nt.y<=m)
{
//cout<<nt.x<<" "<<nt.y<<" "<<nt.step<<endl;
vis[pos2]=1;
nt.step=st.step+1;
q.push(nt);
}
}
else
for(int i=0; i<2; i++)
{
nt.x=st.x+d0[i][0];
nt.y=st.y+d0[i][1];
pos2=(nt.x-1)*m+nt.y;
if(!vis[pos2] && nt.x>=1 && nt.x<=n && nt.y>=1 && nt.y<=m)
{
// cout<<nt.x<<" "<<nt.y<<" "<<nt.step<<endl;
vis[pos2]=1;
nt.step=st.step+1;
q.push(nt);
}
}
}
}
return -1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int cnt=-1;
memset(vis,0,sizeof(vis));
cin>>n>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
int pos=(i-1)*m+j;
scanf("%d",&map1[pos]);
if(map1[pos]==0) map2[pos]=1;
else map2[pos]=0;
}
cin>>sx>>sy>>ex>>ey;
cnt=bfs(sx,sy);
if(cnt==-1)
printf("-1\n");
else
printf("%d\n",cnt);
}
return 0;
}