A计划 HDU

A计划

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34211    Accepted Submission(s): 8412

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

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
 
Source
 
 
 
Recommend
xhd   |   We have carefully selected several similar problems for you:  1253 1548 1372 1728 1175 
 
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

const int maxn=10+3;
char map[2][maxn][maxn];
int book[2][maxn][maxn];
int n,m,c,t,stc,stx,sty,dec,dex,dey,flag;
int nex[][2]={{1,0},{0,1},{-1,0},{0,-1}};

void dfs(int c,int x,int y,int time){
    if(c==dec&&x==dex&&y==dey){
        if(time<=t){
            flag=1;
        }
        return;
    }
    if(time>t||flag==1){
        return;
    }
    for(int i=0;i<4;i++){
        int tx=x+nex[i][0];
        int ty=y+nex[i][1];
        if(tx<1||tx>n||ty<1||ty>m){
            continue;
        }else if(book[c][tx][ty]==2){
            if( (c==0&&(book[1][tx][ty]==1||book[1][tx][ty]==2)) || (c==1&&(book[0][tx][ty]==1||book[0][tx][ty]==2)) ){
                continue;
            }else{
                if(c==0){
                    book[1][tx][ty]=1;
                    dfs(1,tx,ty,time+1);
                    book[1][tx][ty]=0;
                }else{
                    book[0][tx][ty]=1;
                    dfs(0,tx,ty,time+1);
                    book[0][tx][ty]=0;
                }
            }
        }else if(book[c][tx][ty]==0){
            book[c][tx][ty]=1;
            dfs(c,tx,ty,time+1);
            book[c][tx][ty]=0;
        }
    }
}

int main(){
    scanf("%d",&c);
    while(c--){
        scanf("%d%d%d",&n,&m,&t);
        memset(book,0,sizeof(book));
        for(int i=0;i<=1;i++){
            for(int j=1;j<=n;j++){
                getchar();
                for(int k=1;k<=m;k++){
                    scanf("%c",&map[i][j][k]);
                    if(map[i][j][k]=='P'){
                        dec=i,dex=j,dey=k;
                    }else if(map[i][j][k]=='*'){
                        book[i][j][k]=1;
                    }else if(map[i][j][k]=='#'){
                        book[i][j][k]=2;
                    }else if(map[i][j][k]=='S'){
                        stc=i,stx=j,sty=k;
                    }
                }
            }
            getchar();
        }
        flag=0;
        book[stc][stx][sty]=1;
        dfs(stc,stx,sty,0);
        if(flag==1){
            printf("YES
");
        }else{
            printf("NO
");
        }
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/qqshiacm/p/10702890.html