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
Sample Output
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
get:数组太大可以用向量来存,记得先push_back(0)使得矩阵列从1开始。以为数组太小段错误,wa了好几次
状态变化也很容易解决,步数为偶数次的时候不变,奇数次的时候相反
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define Max (int)1e5+10
struct node
{
int x,y,d;
};
vector<int> a[Max],vis[Max];
typedef pair<int ,int > p;
int sx, sy, fx, fy,n,m,mi;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool is(int x,int y)
{
if(x>0&&x<=n&&y>0&&y<=m&&vis[x][y]==0)
return 1;
return 0;
}
int bfs()
{
queue<node> que;
vis[sx][sy] = 1;
node start;
start.d=0;
start.x=sx;
start.y=sy;
que.push(start);
while(que.size())
{
start=que.front();
que.pop();
int x=start.x,y=start.y;
int state=a[x][y];
if(start.d%2)
{
if(state)
state=0;
else
state=1;
}
if (state == 0)
{
node t;
for(int i=2;i<4;i++)
{
t.x=x+dx[i];
t.y=y+dy[i];
t.d=start.d+1;
if(t.x== fx&& t.y==fy)
return t.d;
if(is(t.x,t.y))
{
vis[t.x][t.y] = 1;
que.push(t);
}
}
}
else
{
node t;
for(int i=0;i<2;i++)
{
t.x=x+dx[i];
t.y=y+dy[i];
t.d=start.d+1;
if(t.x== fx&& t.y==fy)
return t.d;
if(is(t.x,t.y))
{
vis[t.x][t.y] = 1;
que.push(t);
}
}
}
}
return -1;
}
int main()
{
int t,i,j;
cin >> t;
while (t--)
{
scanf("%d%d", &n, &m);
for(int i=1;i<=n;i++)
{
a[i].clear();
vis[i].clear();
a[i].push_back(0);
vis[i].push_back(0);
}
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
{
int x;
cin>>x;
a[i].push_back(x);
vis[i].push_back(0);
}
scanf("%d%d%d%d", &sx, &sy, &fx, &fy);
if(sx==fx&&sy==fy)
printf("0\n");
else
printf("%d\n", bfs());
}
return 0;
}