152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )...

http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738

题意 给你一个map 每个格子里有一个红绿灯,用0,1表示状态。当所在格子为0时只能上下移动,为1时左右移动。人一秒动一次,并且每一秒必须移动,灯每秒改变依次状态。问从起点到终点最短时间。

题解:   就看成一道墙壁会按时间周期改变的走迷宫。

    只是墙壁的作用是限制走的方向而不是不能通过。

    关于如何判定迷宫无法走通,按套路设了一个vis数组,试了一发就ac了,莫名奇妙。

    关于处理状态随时间改变,在node里加一个时间参数,然后对它mod2。

我用的BFS代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<cstring>
#include<set>
#include<algorithm>
#include<stack>
#include<string>
#include<cstdio>
#include<list>
#include<cstdlib>
#include<queue>
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
using namespace std;
const int N = 1e5 + 5;
const int INF = 1e6;

int t, n, m,x;
vector<int> map[N], vis[N];
int dir[4][2] = { 1,0 ,-1,0, 0,1, 0,-1 };
struct node {
    int x, y, t;
    node(int x, int y, int t) :x(x), y(y), t(t) {}
};
int main()                                  
{
    
    cin >> t;
    while (t--) {
        
        scanf("%d%d", &n, &m);
        _for(i, 0, n) map[i].clear(), vis[i].clear();
        _for(i, 0, n)_for(j, 0, m) { scanf("%d", &x); map[i].push_back(x); vis[i].push_back(0); }
        int sr, sc, fr, fc;
        scanf("%d%d%d%d", &sr, &sc, &fr, &fc);
        fr--, fc--,sr--,sc--;
        //_for(i, 0, n)_for(j, 0, m)cout << map[i][j];
        queue<node>Q;
        Q.push(node(sr, sc,0));
        vis[sr][sc] = 0;
        int ok = 0;
        while (!Q.empty()) {
            if (ok) break;
            node now = Q.front();
            Q.pop();
            if (now.x == fr&&now.y == fc) { ok = 1; cout << 0 << endl; break; }
            int state = (now.t+map[now.x][now.y]) % 2;
            if (state) {
                _for(i, 0, 2) {
                    int dx = now.x;
                    int dy;
                    dy = i ? now.y + 1 : now.y - 1;
                    if (dx < 0 || dx >= n || dy < 0 || dy >= m||vis[    dx][dy])continue;
                    if (dx == fr&&dy == fc) {
                        cout << now.t + 1 << endl; ok = 1; break;
                    }
                    Q.push(node(dx, dy, now.t + 1)); vis[dx][dy] = 1;
                }
            }
            else {
                _for(i, 0,2) {
                    int dx;
                    dx = i ? now.x + 1 : now.x - 1;
                    int dy = now.y ;
                    if (dx < 0 || dx >= n || dy < 0 || dy >= m||vis[dx][dy])continue;
                    if (dx == fr&&dy == fc) {
                        cout << now.t + 1 << endl; ok = 1; break;
                    }
                    Q.push(node(dx, dy, now.t + 1)); vis[dx][dy] = 1;
                }
            }

        }
        if (!ok)cout << -1 << endl;
    }
    //system("pause");
    return 0;
} 

 

转载于:https://www.cnblogs.com/SuuT/p/8734130.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值