A计划 HDU

来源:互联网 发布:淘宝商城童装女童鞋子 编辑:程序博客网 时间:2024/05/16 13:49

A计划

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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
15 5 14S*#*..#........****....#...*.P#.*..***.....*.*.#..
Sample Output
YES
思路:这题我用深搜,不过剪枝的时候需要注意很多的东西,代码中的思路也比较清晰,

#include<stdio.h>#include<string.h>char map[5][15][15];int book[5][15][15];int dis[4][2]={0,1,0,-1,1,0,-1,0};int n,m,time,flag;void dfs(int x,int y,int s,int step){    if(map[s][x][y]=='P'&&step<=time)    {        flag=1;        return ;    }    if(flag||step>time)        return ;    for(int i=0;i<4;i++)    {        int tx=x+dis[i][0];        int ty=y+dis[i][1];        if(tx<0||ty<0||tx>=n||ty>=m)            continue;        if(map[s][tx][ty]=='#')        {            if(map[1-s][tx][ty]!='*'&&map[1-s][tx][ty]!='#'&&!book[1-s][tx][ty])            {                book[s][tx][ty]=1;                dfs(tx,ty,1-s,step+1);                book[s][tx][ty]=0;            }        }        else if(map[s][tx][ty]!='*'&&!book[s][tx][ty]&&map[s][tx][ty]!='#')        {                book[s][tx][ty]=1;                dfs(tx,ty,s,step+1);                book[s][tx][ty]=0;        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        flag=0;        scanf("%d %d %d",&n,&m,&time);        memset(book,0,sizeof(book));        for(int i=0; i<2; i++)            for(int j=0; j<n; j++)                scanf("%s",map[i][j]);        book[0][0][0]=1;        dfs(0,0,0,0);        if(!flag)            printf("NO\n");        else  printf("YES\n");    }    return 0;}

这个题以前刚开始做的时候,错了很多遍,做到崩溃,现在再做这个题,发现,只要理清思路,也很容易,所以不要害怕不会做,不要害怕会做错,现在不会,以后就会了,现在错了很多,以后就会错的很少(在奉上刚敲的广搜)。

#include <string.h>#include <stdio.h>#include  <queue>#include <algorithm>using namespace std;struct node{int x;int y;int z;int step;};char map[3][20][20];int book[3][20][20];int dis[4][2]={0,1,0,-1,1,0,-1,0};int n,m,T;int bfs(){node p,q;queue<node>Q;p.x=0;p.y=0;p.z=0;p.step=0;book[0][0][0]=1;Q.push(p);while(!Q.empty()){p=Q.front();Q.pop();    if(map[p.x][p.y][p.z]=='P')    return p.step;    for(int i=0;i<4;i++)    {    int tx=p.x;    int ty=p.y+dis[i][0];    int tz=p.z+dis[i][1];    if(ty>=n||tz>=m||ty<0||tz<0||p.step>=T)continue;    if(book[tx][ty][tz]||map[tx][ty][tz]=='*') continue;    book[tx][ty][tz]=1;    if(map[tx][ty][tz]=='#')    {    if(book[1-tx][ty][tz]==0&&map[1-tx][ty][tz]!='#'&&map[1-tx][ty][tz]!='*')    {    book[1-tx][ty][tz]=1;    q.x=1-tx;    q.y=ty;    q.z=tz;    q.step=p.step+1;    Q.push(q);}}else {q.x=tx;q.y=ty;q.z=tz;q.step=p.step+1;Q.push(q);}}}return 0;}int main(){  int t ;  scanf("%d",&t);  while(t--)  {  scanf("%d %d %d",&n,&m,&T);  memset(book,0,sizeof(book));  for(int i=0;i<2;i++)  for(int j=0;j<n;j++)  scanf("%s",map[i][j]);  if(bfs())  printf("YES\n");  else printf("NO\n");}return 0;} 





原创粉丝点击