• A计划 hdu2102(bfs一般题)


    A计划

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

    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
     
     纠结了好久,一开始就定义为只是bfs的简单拓展的,,,可是错了好几次,然后又参考了网上的解题报告,可是大家思路毕竟是不一样的,
    只是大致的方向是一样的,然后只好调试了好几次,最后才发现自己错的可悲啊,
    大致只要注意:1、遇到‘#’一定要传过去,另一边不能是‘#’和‘*’
                         2、传过去只后记得要记录时间,即,传过去之前的时间;(我就是死在这里)
     
    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    #define maxn 15
    char a[2][maxn][maxn];
    int n,m,T,count;
    int vis[2][maxn][maxn];
    int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    
    int chear(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m)
            return 1;
        else
            return 0;
    }
    
    struct node
    {
        int x;
        int y;
        int layer;//
        int step;
    };
    
    queue<node>Q;
    
    void bfs()
    {
        
        node now,next;
        now.x=0;
        now.y=0;
        now.layer=0;
        now.step=0;
        vis[now.layer][now.x][now.y]=1;
        Q.push(now);
        while(!Q.empty())
        {
            now=Q.front();
            Q.pop();
            if(now.step>T)
                break ;
            if(a[now.layer][now.x][now.y]=='P'&&now.step<=T)
            {
                printf("YES
    ");
                return ;
            }
            for(int i=0;i<4;i++)
            {
                next.x=now.x+dir[i][0];
                next.y=now.y+dir[i][1];
                next.layer=now.layer;     
                if(chear(next.x,next.y)||a[next.layer][next.x][next.y]=='*'||vis[next.layer][next.x][next.y])
                    continue;
                next.step=now.step+1;
                vis[next.layer][next.x][next.y]=1;
                if(a[next.layer][next.x][next.y]=='#'&&a[!next.layer][next.x][next.y]!='*'&&a[!next.layer][next.x][next.y]!='#')
                {
                    next.layer=1-next.layer;
                    next.step=now.step+1;//传输过去记得也要记录时间,不然下一层时间不对,WA了好几次,,,,
                    vis[next.layer][next.x][next.y]=1;
                    Q.push(next);
                }
                else if(a[next.layer][next.x][next.y]=='.'||a[next.layer][next.x][next.y]=='P')
                    Q.push(next);
            }
        }
        printf("NO
    "); 
    }
    int main()
    {
        int C,i,j;
        scanf("%d",&C);
        while(C--)
        {
            scanf("%d%d%d",&n,&m,&T);
    //        getchar();
            for(i=0;i<n;i++)
                cin>>a[0][i];
    //        printf("%c
    ",a[0][0][0]);
    //        for(i=0;i<n;i++)
    //            cout<<a[0][i]<<endl;
            getchar();
            for(i=0;i<n;i++)
                cin>>a[1][i];
    //        for(i=0;i<n;i++)
    //            cout<<a[1][i]<<endl;
            memset(vis,0,sizeof(vis));
            while(!Q.empty())//Q必须得被清空        勿忘!!!
                Q.pop();
            bfs();
        }
    }
    /*
    5
    5 5 14
    S*#*.
    .#...
    .....
    ****.
    ...#.
    
    ..*.P
    #.*..
    ***..
    ...*.
    *.#..
    5 5 14
    S*#*.
    .#...
    .....
    ****.
    ...#.
    
    ..*.P
    #.*..
    ***..
    ...**
    *.#..
    5 5 14
    S*#*.
    .#...
    .....
    ****.
    ...#.
    
    ..*.P
    #.*.*
    ****.
    ...*.
    *.#..
    5 5 14
    S*#*.
    .#...
    .....
    ****.
    ...#.
    
    ..*..
    #.*..
    ***..
    ...*.
    *.#..
    5 5 14
    S*#*.
    .#...
    ....#
    ***..
    ...#.
    
    ..*.P
    #.*.*
    ***.#
    ...*.
    *.#..
    
    */

    貌似没什么好说的了,,,一起努力,一起加油!      就这样。

  • 相关阅读:
    安装chrome driver(14)
    爬虫-selenium实现验证码自动登录(14)
    爬虫-反爬与反反爬(12)
    爬虫-模拟登录(13)
    爬虫-GIL与线程同步问题(11)
    爬虫-多进程(10)
    爬取csdn的数据与解析存储(9)
    Exchange Server 2016邮件系统建设方案
    Exchange 2016高可用及容灾架构选型参考
    Installing Exchange 2016 on Windows Server 2016 Step by Step
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3260620.html
Copyright © 2020-2023  润新知