【The 18th Zhejiang University Programming Contest Sponsored by TuSimple 】G.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 .

题意:给一个由0,1组成的二维矩阵,若某位置是0,只能上下走,若是1,只能左右走(都在矩阵内部,不能出界)。
给定出发点坐标(sx,sy),终点坐标(tx,ty),问能否到达?所用最少的步数是多少?

dfs的话需要考虑两个方向,不好处理,而且求的是最少步数。
钻什么牛角,非要用dfs。。
这个居然都没写出来。。 菜

【bfs】

#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,n) for(int i=a;i<n;i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define pb push_back
#define IO ios::sync_with_stdio(false);
typedef long long ll;
const int N=1e5+5;
const double eps=1e-4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1}; ///上下左右

struct Node
{
    int x,y;
    int cnt;
    Node(int x,int y,int cnt):x(x),y(y),cnt(cnt) {}
};
vector<bool>g[N];
vector<bool>vis[N];
int n,m,ans;
int sx,sy,tx,ty;
bool in(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m) return true;
    return false;
}
bool bfs()
{
    queue<Node>que;
    //printf("%d %d %d %d\n",sx,sy,tx,ty);
    que.push({sx,sy,0});
    vis[sx][sy]=1;
    while(!que.empty())
    {
        Node now=que.front();
        que.pop();
        int x=now.x,y=now.y;
        int cnt=now.cnt;
        int state=(g[x][y]+cnt)%2==0?0:1;
        if(x==tx&&y==ty)
        {
            ans=cnt;
            return true;
        }
        //printf("state=%d\n",state);
        int ss,tt;
        if(!state) ss=0,tt=2;
        else ss=2,tt=4;
        for(int i=ss; i<tt; i++)
        {
            int nx=x+dir[i][0];
            int ny=y+dir[i][1];
            if(in(nx,ny)&&!vis[nx][ny])
            {
                que.push({nx,ny,cnt+1});
                vis[nx][ny]=1;
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<=n; i++)
        {
            g[i].clear();
            vis[i].clear();
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                int x;
                scanf("%d",&x);
                g[i].pb(x);
                vis[i].pb(0);
            }
        }
        scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
        sx--,sy--,tx--,ty--;
        bool flag=bfs();
        printf("%d\n",flag?ans:-1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值