zoukankan      html  css  js  c++  java
  • hdu 1010 Tempter of the Bone(bfs)

    hdu 1010 Tempter of the Bone

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

    Problem Description
    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

    The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
     

    Input
    The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

    'X': a block of wall, which the doggie cannot enter; 
    'S': the start point of the doggie; 
    'D': the Door; or
    '.': an empty block.

    The input is terminated with three 0's. This test case is not to be processed.
     

    Output
    For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
     

    Sample Input
    4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
     

    Sample Output
    NO YES

    //剪枝深度搜索

    #include<iostream>
    #include<cmath>
    using namespacestd;
    int
    m,n,t;
    char
    map[10][10];
    bool
    mark[10][10];
    int
    ex,ey,flag;
    int
    cx[]={0,0,1,-1};
    int
    cy[]={-1,1,0,0};
    void
    dfs(intsx,intsy,intstep)
    {

        int
    i;
        if
    (flag)  return;
        if
    (sx==ex&&sy==ey&&step==t)
        {

            flag=1;
            return
    ;
        }

        int
    temp=(t-step)-abs(sx-ex)-abs(sy-ey);
        if
    (temp<0||temp%2!=0)
            return
    ;
        for
    (i=0;i<4;i++)
        {

            int
    dx=cx[i]+sx;
            int
    dy=cy[i]+sy;
            if
    (dx>=0&&dx<m&&dy>=0&&dy<n&&!mark[dx][dy]&&map[dx][dy]!='X')
            {

                mark[dx][dy]=1;
                dfs(dx,dy,step+1);
                if
    (flag)return;
                mark[dx][dy]=0;
            }
        }
    }

    int
    main()
    {

        while
    (~scanf("%d%d%d",&m,&n,&t))
        {

            if
    (n==0&&m==0&&t==0)
                break
    ;
            int
    i,j,sx,sy;
            for
    (i=0;i<m;i++)
            {
        
                getchar();
                for
    (j=0;j<n;j++)
                {

                    scanf("%c",&map[i][j]);
                    if
    (map[i][j]=='S')
                    {

                        sx=i;
                        sy=j;
                    }

                    else if
    (map[i][j]=='D')
                    {

                        ex=i;
                        ey=j;
                    }
                }
            }

            getchar();
            flag=0;
            memset(mark,0,sizeof(mark));
            mark[sx][sy]=1;
            dfs(sx,sy,0);
            if
    (flag)
                puts("YES");
            else

                puts("NO");
        }

        return
    0;
    }

  • 相关阅读:
    各地电信运营商插广告赚钱,北京联通也不甘落后
    也谈Server Limit DOS的解决方案
    Still Believe
    无奈小虫何
    好朋有也有类别
    无为而治
    青鸟随想
    落寞时分
    网站开发学习路线和资料
    C++实例 添加快捷键表
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999472.html
Copyright © 2011-2022 走看看