hdu 2102 A计划-bfs
阅读原文时间:2025年01月10日阅读:1

**Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11573    Accepted Submission(s): 2823
**

Problem Description


怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃
公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探
所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时
空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移
动只能通过时空传输机,且不需要任何时间。

Input

输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。

Output

如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

Sample Input

1

5 5 14

S*#*.

.#…

…..

****.

…#.

..*.P

#.*..

***..

…*.

*.#..

Sample Output

YES

#include
#include
#include
#include
#include
using namespace std ;
char mat1[][], mat2[][] ;//第一层和第二层
int vis[][][];//标记数组
int dir[][] = { {-,}, {,-},{,},{,} } ;//方向
int N, M, T ;
struct node{
int x, y ;
int f, st ;
}sp, now, nex ;
void _()
{
scanf("%d%d%d",&N,&M,&T) ;
for(int i = ; i < N; ++i) scanf("%s",mat1[i]) ; for(int i = ; i < N; ++i) scanf("%s",mat2[i]) ; } void bfs() { queue que ;
vis[sp.x][sp.y][] = ;//根节点标记
que.push(sp) ;
while(!que.empty())
{
now = que.front() ;
que.pop() ;
if(now.f && mat2[now.x][now.y] == 'P') {
if(now.st <= T)
{printf("YES\n") ; return ;}
else break ;
}
else if(!now.f && mat1[now.x][now.y] == 'P' ) {
if(now.st <= T)
{printf("YES\n") ; return ;}
else break ;
}

    for(int i = ; i < ; ++i)  
    {  
        nex.x = now.x + dir\[i\]\[\] ;  
        nex.y = now.y + dir\[i\]\[\] ;  
        nex.f = now.f ;  
        nex.st = now.st +  ;  
        if(nex.x <  || nex.x >= N || nex.y <  || nex.y >= M) continue ;//要先判断是否越界,再判断是否标记过  
        if(!nex.f && vis\[nex.x\]\[nex.y\]\[\])  continue ;//标记过  
        else if(nex.f && vis\[nex.x\]\[nex.y\]\[\]) continue ;

        if(nex.f && mat2\[nex.x\]\[nex.y\] == '\*') continue ;//是墙  
        else if(!nex.f && mat1\[nex.x\]\[nex.y\] == '\*') continue ;

        if(nex.f && mat2\[nex.x\]\[nex.y\] == '.') { vis\[nex.x\]\[nex.y\]\[\] = ; que.push(nex) ; continue ;}//可以走  
        else if(!nex.f && mat1\[nex.x\]\[nex.y\] == '.') { vis\[nex.x\]\[nex.y\]\[\] = ; que.push(nex) ; continue ;}  
        //以下必不可少,在以前搜索迷宫的时候,我们是以 mat\[i\]\[j\] != '\*',来判断 (i,j)可以走,既若此时的节点为目标节点也同样入队  
        if(nex.f && mat2\[nex.x\]\[nex.y\] == 'P') {//但在此处,我是分开判断,不加如下,目标节点反而会被舍弃  
            if(nex.st <= T)  
        {printf("YES\\n") ; return ;}  
        else break ;  
        }  
        else if(!nex.f && mat1\[nex.x\]\[nex.y\] == 'P') {  
                if(nex.st <= T)  
        {printf("YES\\n") ; return ;}  
        else break ;  
        }

        if(nex.f && mat2\[nex.x\]\[nex.y\] == '#'){  
                vis\[nex.x\]\[nex.y\]\[\] =  ;  
                if(mat1\[nex.x\]\[nex.y\] == '\*') continue ;  
                if(mat1\[nex.x\]\[nex.y\] == '#') continue ;  
                if(mat1\[nex.x\]\[nex.y\] == 'P') {//考虑传送之后直接到达目标节点的情况  
                    if(nex.st <= T)  
            {printf("YES\\n") ; return ;}  
                    else break ;  
                }  
                nex.f =  ;  
                que.push(nex) ;  
        }

        if(!nex.f && mat1\[nex.x\]\[nex.y\] == '#'){  
                vis\[nex.x\]\[nex.y\]\[\] =  ;  
                if(mat2\[nex.x\]\[nex.y\] == '\*') continue ;  
                if(mat2\[nex.x\]\[nex.y\] == '#') continue ;  
                if(mat2\[nex.x\]\[nex.y\] == 'P') {  
                    if(nex.st <= T)  
            {printf("YES\\n") ; return ;}  
                    else break ;  
                }  
                nex.f =  ;  
                que.push(nex) ;  
        }  
    }  
}  
printf("NO\\n") ;  

}
void solve()
{
_() ;
sp.x = ; sp.y = ;
sp.f = ;sp.st = ;
memset(vis, ,sizeof vis) ;
bfs() ;
}
int main()
{
// freopen("in.txt","r",stdin) ;
int t ;
scanf("%d",&t) ;
while(t--) solve() ;
return ;
}

Source

HDU 2007-6 Programming Contest

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章