hdu 2102 A计划(bfs)

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

A计划

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


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
HDU 2007-6 Programming Contest
我觉得这题有些问题,首先不是在t时刻救出公主,而是在这段时间之内就出来就可以。其次,“层间的移动只能通过时空传输机,且不需要任何时间"有误导作用,当到达另一层后,时间是花了一秒的,反正我这样改了才过的。还有要注意:如果另一层的相对位置是‘#’或者‘*’时不应该过去,如果时间大于了规定时间及时跳出。

#include <iostream>
#include <cstdio>
#include<cstring>
#include<queue>
using namespace std;
char s[2][15][15];
bool vis[2][15][15];
int n,m,t;
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int change(int a){
    if(a==0)return 1;
    else return 0;
}
typedef struct node{
    int x,y,z,step;
}Node;
int bfs(){
    queue<Node> que;
    Node temp;
    temp.step=0;
    temp.x=temp.y=temp.z=0;
    que.push(temp);
    while(!que.empty()){
         temp=que.front();
         que.pop();
         int xx=temp.x,yy=temp.y,zz=temp.z;
         if(s[zz][xx][yy]=='P'){
             return temp.step;
         }
         for(int i=0;i<4;i++){
             int tx=xx+dir[i][0],ty=yy+dir[i][1],tz=zz;
             if(tx<0||tx>=n||ty<0||ty>=m||s[tz][tx][ty]=='*')continue;
             if(s[tz][tx][ty]=='#'&&!vis[tz][tx][ty]){
                  vis[tz][tx][ty]=1;
                  tz=change(tz);
                  if(s[tz][tx][ty]=='#'|| s[tz][tx][ty]=='*'){  tz=change(tz);  s[tz][tx][ty]='*';  continue ; }
                  Node tmp;
                  tmp.step=temp.step+1;
                  tmp.x=tx;
                  tmp.y=ty;
                  tmp.z=tz;
                  vis[tz][tx][ty]=1;
                  que.push(tmp);
             }
             else if(s[tz][tx][ty]=='.'&&!vis[tz][tx][ty]){
                  vis[tz][tx][ty]=1;
                  Node tmp;
                  tmp.step=temp.step+1;
                  if(tmp.step>t)continue;
                  tmp.x=tx;
                  tmp.y=ty;
                  tmp.z=tz;
                  que.push(tmp);
             }
             else if(s[tz][tx][ty]=='P'){
                  if(temp.step+1>t)continue; 
                  return temp.step+1;
             }
         }
    }
    return -1;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int ca;
    cin>>ca;
    while(ca--){
        scanf("%d%d%d",&n,&m,&t);
        memset(s,0,sizeof(s));
        for(int k=0;k<2;k++)
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>s[k][i][j];
            }
        }
        s[0][0][0]='*';
        memset(vis,0,sizeof(vis));
        vis[0][0][0]=1;
        int mytime=bfs();
        if(mytime!=-1&&mytime<=t)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
原来误导版的T时刻到达(果断TLE):
#include <iostream>
#include <cstdio>
using namespace std;
char s[2][15][15];
int n,m,t;
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
bool flag;
int change(int a){
    if(a==0)return 1;
    else return 0;
}
void dfs(int x,int y,int dex,int mytime){
    if(flag==1||mytime>t)return ;
    for(int i=0;i<4;i++){
        int tx=x+dir[i][0],ty=y+dir[i][1];
        if(tx<0||tx>=n||ty<0||ty>=m)continue;
        if(s[dex][tx][ty]=='P'&&mytime==t){
             flag=1;
             return ;
        }
        else if(s[dex][tx][ty]=='*'){
             continue ;
        }
        else if(s[dex][tx][ty]=='.'){
             s[dex][tx][ty]='*';
             dfs(tx,ty,dex,mytime+1);
             s[dex][tx][ty]='.';
        }
        else if(s[dex][tx][ty]=='#'){
             s[dex][tx][ty]='*';
             dex=change(dex);
             dfs(tx,ty,dex,mytime);
             dex=change(dex);
             s[dex][tx][ty]='#';
        }
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int ca;
    cin>>ca;
    while(ca--){
        scanf("%d%d%d",&n,&m,&t);
        for(int k=0;k<2;k++)
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>s[k][i][j];
            }
        }
        flag=0;
        s[0][0][0]='*';
        dfs(0,0,0,0);
        if(flag)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}





你可能感兴趣的:(HDU,bfs)