码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2102 BFS

时间:2016-08-05 15:16:44      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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

#include "map"
#include "queue"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "iostream"
#include "algorithm"
#define abs(x) x > 0 ? x : -x
#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b

using namespace std;

int di[6][3] = {{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};
int Map[5][15][15];
bool vis[5][15][15];
int t;

struct Node
{
    int zz,xx,yy;
    int step;
};

void Bfs()
{
    memset(vis,0,sizeof(vis));
    queue<Node>Q;
    Node now,next;
    int l,r;

    now.zz=1;
    now.xx=1;
    now.yy=1;
    now.step=0;
    vis[1][1][1] = 1;

    Q.push(now);

    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();

        if(Map[now.zz][now.xx][now.yy]==3)
        {
            if(now.step<=t)
                printf("YES\n");
            else
                printf("NO\n");
            return;
        }
        if(Map[now.zz][now.xx][now.yy]==2)
            l=4,r=6;
        if(Map[now.zz][now.xx][now.yy]==1)
            l=0,r=4;

            for(int i=l; i<r; i++)
            {
                next.zz = now.zz + di[i][0];
                next.xx = now.xx + di[i][1];
                next.yy = now.yy + di[i][2];
                next.step = now.step + 1;
                if(i==4||i==5)
                    next.step = now.step;

                if(Map[next.zz][next.xx][next.yy]!=0)
                {
                    if(!vis[next.zz][next.xx][next.yy])
                    {
                        vis[next.zz][next.xx][next.yy] = 1;
                        Q.push(next);
                    }
                }
            }
    }
    printf("NO\n");
}

int main()
{
    int c,n,m,i,j,k;
    char s;
    scanf("%d",&c);
    while(c--)
    {
        memset(Map,0,sizeof(Map));
        scanf("%d%d%d",&n,&m,&t);
        for(i=1; i<=2; i++)
        {
            for(j=1; j<=n; j++)
            {
                getchar();
                for(k=1; k<=m; k++)
                {
                    scanf("%c",&s);
                    if(s==S||s==.)
                        Map[i][j][k] = 1;
                    if(s==P)
                        Map[i][j][k] = 3;
                    if(s==#)
                        Map[i][j][k] = 2;
                    if(s==*)
                        Map[i][j][k] = 0;
                }
            }
            getchar();
        }
         //for(i=1; i<=2; i++){for(j=1; j<=n; j++) {for(k=1; k<=m; k++) printf("%d",Map[i][j][k]); printf("\n"); }printf("\n"); }
        Bfs();
    }
    return 0;
}

 

hdu 2102 BFS

标签:

原文地址:http://www.cnblogs.com/max88888888/p/5740949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!