ZOJ4020--Traffic Light--BFS+思维

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 .

#include<bits/stdc++.h>
using namespace std;
#define maxn 100006
struct point
{
	int x,y;
	int step;
	point()
	{
		step=0;
	}
};
int n,m;
int bx,by,ex,ey;
map<int,int>mmp[maxn];
map<int,int>vis[maxn];
int dx[]= {0,-1,1,0,0};
int dy[]= {0,0,0,-1,1};
queue<point>q;
int bfs(int x,int y)
{
    queue<point>q;
	while(q.size())q.pop();
	point p,b;
	p.x=x;
	p.y=y;
	p.step=0;
	vis[x][y]=1;
	q.push(p);
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        if(p.x==ex&&p.y==ey)
        {
            return p.step;
        }
        int flag=p.step;
        if(flag%2==1)
        {
            flag=!mmp[p.x][p.y];//变
        }
        else
        {
            flag=mmp[p.x][p.y];
        }
        if(flag)
        {
            for(int i=3; i<=4; i++)
            {
                b.x=p.x+dx[i];
                b.y=p.y+dy[i];
                b.step=p.step+1;
                if(b.x==ex&&b.y==ey)
                    return b.step;
                if(b.x>=1&&b.y>=1&&b.y<=m&&b.x<=n&&!vis[b.x][b.y])
                {
                    q.push(b);
                    vis[b.x][b.y]=1;
                }
            }
        }
        else
        {
            //上下
            for(int i=1; i<=2; i++)
            {
                b.x=p.x+dx[i];
                b.y=p.y+dy[i];
                b.step=p.step+1;
                if(b.x==ex&&b.y==ey)
                    return b.step;
                if(b.x>=1&&b.y>=1&&b.y<=m&&b.x<=n&&!vis[b.x][b.y])
                {
                    q.push(b);
                    vis[b.x][b.y]=1;
                }
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<=n; i++)
            mmp[i].clear(),vis[i].clear();
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                int x;
                scanf("%d",&x);//---//
                mmp[i][j]=x;
            }
        }
        scanf("%d%d",&bx,&by);
        scanf("%d%d",&ex,&ey);
        int ans=bfs(bx,by);
        cout<<ans<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值