[ZOJ] 4020 G- Traffic Light (BFS)

 题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5748

Traffic Light

Time Limit: 1 Second      Memory Limit: 131072 KB

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 .


【题意】

    给定n*m 的图,  给定起点坐标和终点坐标   

    0 代表只能上下走 

    1 代表只能左右走

    并且每走一步。 01 要翻转 即 原先是1 的变成0  是0 的变成1 

    问 到达终点最短时间;

 【思路】

     BFS 搜索一下,  也可以用 SPFA  跑最短路, 但是要根据 两点的距离是奇偶 来建边

 【code】

#include <stdio.h>
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int MAXN=1e5+5;
const int INF=0x3f3f3f3f;
int dir1[3][2]={0,1,0,-1};
int dir2[3][2]={1,0,-1,0};
using namespace std;
int px,py,ex,ey;
int n,m;
struct node{
    int step;
    int x,y;
};
vector<int>mp[MAXN];

int bfs(node st )
{
    int vis[n][m];
    mem(vis,0);
    queue<node>Q;
    Q.push(st);
    int ans=0;
    while(!Q.empty())
    {
        node u=Q.front();
        Q.pop();
        if(u.x==(ex-1)&&u.y==(ey-1))
        {
            ans=u.step;
            return ans;
        }

        if(u.step%2==0)// 本身
        {
            if(mp[u.x][u.y])
            {
                for(int i=0;i<2;i++)
                {
                    node t=u;
                    t.x+=dir1[i][0];
                    t.y+=dir1[i][1];
                     if(!vis[t.x][t.y]&&t.x<n&&t.x>=0&&t.y<m&&t.y>=0)
                    {
                        t.step++;
                        vis[t.x][t.y]=1;
                        Q.push(t);
                    }
                }
            }
            else
            {
                for(int i=0;i<2;i++)
                {
                    node t=u;
                    t.x+=dir2[i][0];
                    t.y+=dir2[i][1];
                    if(!vis[t.x][t.y]&&t.x<n&&t.x>=0&&t.y<m&&t.y>=0)
                    {
                        t.step++;
                        vis[t.x][t.y]=1;
                        Q.push(t);
                    }
                }
            }
        }
        else
        {
            if( !mp[u.x][u.y])
            {
                for(int i=0;i<2;i++)
                {
                    node t=u;
                    t.x+=dir1[i][0];
                    t.y+=dir1[i][1];
                    if(!vis[t.x][t.y]&&t.x<n&&t.x>=0&&t.y<m&&t.y>=0)
                    {

                        t.step++;
                        vis[t.x][t.y]=1;
                        Q.push(t);
                    }
                }
            }
            else
            {
                for(int i=0;i<2;i++)
                {
                    node t=u;
                    t.x+=dir2[i][0];
                    t.y+=dir2[i][1];
                    if(!vis[t.x][t.y]&&t.x<n&&t.x>=0&&t.y<m&&t.y>=0)
                    {
                        t.step++;
                        vis[t.x][t.y]=1;
                        Q.push(t);
                    }
                }
            }
        }
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0;i<MAXN;i++)
        {
            mp[i].clear();
        }
        for(int i=0;i<n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                int x;
                scanf("%d",&x);
                mp[i].push_back(x);
            }
        }
        scanf("%d %d %d %d",&px,&py,&ex,&ey);
        node st;
        st.step=0;
        st.x=px-1;
        st.y=py-1;
        int ans=bfs(st);
        if(ans<0)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

1233

posted @ 2018-04-07 22:09  Sizaif  阅读(203)  评论(0编辑  收藏  举报