ZOJ4020 bfs

题意:一个网格图 从起点走到终点 若当前位置是0 只能走上下
若当前位置是1 只能走左右
随着时间变化 地图也会时刻取反
问你最近多久可以到终点。
思路:广搜 但是判断某点能不能入队有条件
奇数时间和偶数时间只能各自入队一次!这是条件!
下面是ac代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
const int maxn=1e5+10;
int n,m;
//cnt记录这个点去了多少次 
//0代表没去过 -1代表奇数时间去过 1代表偶数时间去过
//-1只能再入一个偶数时间 1只能再入一个奇数时间
//2代表奇偶试过  -2代表偶奇试过
int g[maxn],cnt[maxn];
int dir0[2][2]={-1,0,1,0};//只能上下
int dir1[2][2]={0,-1,0,1};//只能左右
struct node{
    int i,j,t;
}st,en;
queue<node> q;
inline int id(int i,int j){
    return m*(i-1)+(j-1);
}
inline bool judge(int i,int j,int t){
    if(i<1||j<1||i>n||j>m) return false;
    if(abs(cnt[id(i,j)])>1) return false;
    if(cnt[id(i,j)]==-1) {//奇数时间探访过这里
        if(t&1) return false;
        else{
            cnt[id(i,j)]=2;
            return true;
        }
    }
    if(cnt[id(i,j)]==1){
        if(t&1) return true;
        else{
            cnt[id(i,j)]=-2;
            return false;
        }
    }
    //cnt=0的时候
    cnt[id(i,j)]=(t&1)?-1:1;
    return true;
}
int main() {
	int T;sd(T);
    while(T--){
        while(!q.empty()) q.pop();
        sdd(n,m);
        rep(i,1,n){
            rep(j,1,m){
                int tp;
                sd(tp);
                g[id(i,j)]=tp;
                cnt[id(i,j)]=0;
            }
        }
        sdd(st.i,st.j);st.t=0;
        sdd(en.i,en.j);
        q.push(st);
        cnt[id(st.i,st.j)]=1;
        bool flag=false;
        node tp;
        while(!q.empty()){
            tp=q.front();
            q.pop();
            if(tp.i==en.i&&tp.j==en.j){
                flag=true;
                break;
            }
            if((g[id(tp.i,tp.j)]+tp.t)%2==0){
                rep(i,0,1){
                    node tp1;
                    tp1.i=tp.i+dir0[i][0];
                    tp1.j=tp.j+dir0[i][1];
                    tp1.t=tp.t+1;
                    if(!judge(tp1.i,tp1.j,tp1.t)) continue;
                    q.push(tp1);
                }
            }else if((g[id(tp.i,tp.j)]+tp.t)%2==1){
                rep(i,0,1){
                    node tp1;
                    tp1.i=tp.i+dir1[i][0];
                    tp1.j=tp.j+dir1[i][1];
                    tp1.t=tp.t+1;
                    if(!judge(tp1.i,tp1.j,tp1.t)) continue;
                    q.push(tp1);
                }
            }
        }
        if(flag){
            printf("%d\n",tp.t);
        }else{
            printf("-1\n");
        }
    }
	return 0;
}

DreamGrid City is a city with (n \times m) intersections arranged into a grid of (n) rows and (m) columns. The intersection on the (i)-th row and the (j)-th column can be described as ((i, j)), and two intersections ((i_1, j_1)) and ((i_2, j_2)) are connected by a road if (|i_1-i_2| + |j_1-j_2| = 1).

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 ((i, j)) is in state 0, one can only move from ((i, j)) to ((i+1, j)) or ((i-1, j)); If the traffic light is in state 1, one can only move from ((i, j)) to ((i, j+1)) or ((i, j-1)) (of course, the destination must be another intersection in the city).

BaoBao lives at the intersection ((s_i, s_j)), and he wants to visit his best friend DreamGrid living at the intersection ((f_i, f_j)). 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 ((s_i, s_j)) to ((f_i, f_j)) 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 (T), indicating the number of test cases. For each test case:

The first line contains two integers (n) and (m) ((1 \le n \times m \le 10^5)), indicating the size of the city.

For the following (n) lines, the (i)-th line contains (m) integers (t_{i,1}, t_{i,2}, \dots, t_{i,m}) ((0 \le t_{i,j} \le 1)), where (t_{i,j}) indicates the initial state of the traffic light at intersection ((i, j)).

The next line contains four integers (s_i), (s_j), (f_i) and (f_j) ((1 \le s_i, f_i \le n), (1 \le s_j, f_j \le m)), indicating the starting intersection and the destination intersection.

It’s guaranteed that the sum of (n \times m) over all test cases will not exceed (3 \times 10^5).
Output

For each test case output one line containing one integer, indicating the shortest possible time (in minute) BaoBao can move from ((s_i, s_j)) to ((f_i, f_j)) 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: ((1, 3) \to (2, 3) \to (2, 2) \to (2, 1)).

For the second sample test case, due to the traffic light rules, BaoBao can’t go from ((1, 3)) to ((1, 2)) directly. Instead, he should follow this path: ((1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2)).

For the third sample test case, it’s easy to discover that BaoBao can only go back and forth between ((1, 1)) and ((1, 2)).

发布了120 篇原创文章 · 获赞 12 · 访问量 5253

猜你喜欢

转载自blog.csdn.net/weixin_43735161/article/details/105263438
BFS