The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G【BFS】

本文介绍了一种在网格城市中,根据交通灯状态变化寻找从起点到终点最短时间路径的算法实现。通过BFS结合状态判断,在每分钟内根据交通灯规则移动,解决了无法等待的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738

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 .

LATEX,由于显示不全,所以请看原题面。

问题:
从fst到end,要一直走,不能在红绿灯面前等待,要注意奇数秒和偶数秒红绿灯的判断。
WA点:没有WA点,因为任意两点之间的奇偶性取决于他们的曼哈顿距离,所以绕道不会改变奇偶性。但是如果这是一个非矩阵的图,那么我们就需要记录两种状态,就会卡一下。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 3e5 + 10;
int a[maxn];
int vis[maxn];
int dx[4] = { 0,0,-1,1 };
int dy[4] = { 1,-1,0,0 };
struct node {
    int x, y;
    int stp;
};
int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        memset(a, 0, sizeof a);
        memset(vis, 0, sizeof vis);
        int n, m; int tmp;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                scanf("%d", &tmp);
                a[i*m + j] = tmp;
            }
        }
        int sx, sy, fx, fy;
        scanf("%d%d%d%d", &sx, &sy, &fx, &fy);
        node fst;
        fst.x = sx;
        fst.y = sy;
        vis[sx*m + sy] = 1;
        fst.stp = 0;
        queue<node> q;
        q.push(fst);
        bool flag = 1; 
        while (!q.empty())
        {
            node nw = q.front();
            q.pop();
            if (nw.x == fx && nw.y == fy) {
                printf("%d\n", nw.stp);
                flag = 0;
                break;
            }
            node cs;
            for (int i = 0; i < 4; i++) {
                cs.x = nw.x + dx[i];
                cs.y = nw.y + dy[i];
                cs.stp = nw.stp + 1;
                if (cs.x >= 1 && cs.x <= n && cs.y >= 1 && cs.y <= m&&vis[cs.x*m+cs.y]==0) {
                    if (a[nw.x*m + nw.y] == 1) {
                        if (cs.stp % 2 == 0) {
                            if (dx[i] != 0) {
                                vis[cs.x*m + cs.y] = 1;
                                q.push(cs);
                            }
                        }
                        else {
                            if (dx[i] == 0) {
                                vis[cs.x*m + cs.y] = 1;
                                q.push(cs);
                            }
                        }
                    }
                    else {
                        if (cs.stp % 2 == 0) {
                            if (dx[i] == 0) {
                                vis[cs.x*m + cs.y] = 1;
                                q.push(cs);
                            }
                        }
                        else {
                            if (dx[i] != 0) {
                                vis[cs.x*m + cs.y] = 1;
                                q.push(cs);
                            }
                        }
                    }
                }
            }
        }
        if (flag) {
            printf("-1\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值