• 152


    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;
    } 
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    ubuntu下pip的安装和使用
    跨域总结
    本地存储小结
    SVN
    appium整理文档
    appium python andiroid自动化文档整理笔记
    Python 接口测试(二)
    Python 接口测试(一)
    Python 接口测试(四)
    Python 接口测试(三)
  • 原文地址:https://www.cnblogs.com/SuuT/p/8734130.html
Copyright © 2020-2023  润新知