zoukankan      html  css  js  c++  java
  • HDU 2102 A计划 bfs

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=2102

    题目大意:

    这个就是简单的BFS, 需要注意的地方就是你进入传送阵后被传送到的地方是什么, 要是还是传送阵的话人就会被传送回来,此点要注意一下。

    其他的基本没什么坑的地方,代码可以参考一下, 弱有疑问欢迎留言,随时解决。

    #include <iostream>
    #include <vector>
    #include <stack>
    #include <cstdlib>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <algorithm>
    using namespace std;
    #define maxn 15
    #define INF 0xfffffff
    #define min(a,b) (a<b?a:b)
    #define max(a,b) (a>b?a:b)
    struct Point
    {
        int x, y, z;
        int step;
    } Ps, Pe;
    
    char maps[2][maxn][maxn];
    bool vis[2][maxn][maxn];
    int dir[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
    int m, n, step;
    bool OK(Point P)
    {
        return P.x >= 0 && P.x < m && P.y >= 0 && P.y < n && maps[P.z][P.x][P.y] != '*' && !vis[P.z][P.x][P.y];
    }
    bool BFS()
    {
        Point P, Pn;
        queue<Point> Q;
        Q.push(Ps);
    
        while( !Q.empty() )
        {
            P = Q.front();
            Q.pop();
            if(P.step > step)
                return false;
            if(P.x == Pe.x && P.y == Pe.y && P.z == Pe.z)
                return true;
    
            for(int i=0; i<4; i++)
            {
                Pn.z = P.z;
                Pn.x = P.x + dir[i][0];
                Pn.y = P.y + dir[i][1];
                Pn.step = P.step + 1;
    
                if( OK(Pn) )
                {
                    vis[Pn.z][Pn.x][Pn.y] = true;
                    if(maps[Pn.z][Pn.x][Pn.y] == '#' && maps[Pn.z^1][Pn.x][Pn.y] == '.' &&  !vis[Pn.z^1][Pn.x][Pn.y] )
                    {
                        Pn.z = Pn.z^1;
                        vis[Pn.z][Pn.x][Pn.y] = true;
                        Q.push(Pn);
                    }
                    else if(maps[Pn.z][Pn.x][Pn.y] == '.')
                    {
                        Q.push(Pn);
                    }
                }
            }
        }
        return false;
    }
    
    int main()
    {
        int T;
        cin >> T;
    
        while(T--)
        {
            cin >> m >> n >> step;
            memset(vis,0,sizeof(vis));
            for(int k=0; k<2; k++)
            {
                for(int i=0; i<m; i++)
                {
                    cin >> maps[k][i];
                    for(int j=0; j<n; j++)
                    {
                        if(maps[k][i][j] == 'S')
                            Ps.x = i, Ps.y = j, Ps.z = k, Ps.step = 0, maps[k][i][j] = '.';
                        if(maps[k][i][j] == 'P')
                            Pe.x = i, Pe.y = j, Pe.z = k, maps[k][i][j] = '.';
                    }
                }
            }
    
            if( BFS() )
                cout << "YES" <<endl;
            else
                cout << "NO" << endl;
        }
        return 0;
    }
  • 相关阅读:
    HDU:2767-Proving Equivalences(添边形成连通图)
    POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)
    HDU:1269-迷宫城堡(tarjan模板)
    Xml 丶json丶 C/S KVO 数据库 SQL 数据持久化 复杂对象 集合视图综合
    项目穿越记
    SDP (Session Description Protocol)
    shell脚本实现查找文件夹下重复的文件,并提供删除功能
    HDU 2896 病毒侵袭 (AC自动机)
    项目启动那些事儿
    C++ 完美破解九宫格(数独)游戏
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4109015.html
Copyright © 2011-2022 走看看