zoukankan      html  css  js  c++  java
  • Tempter of the Bone dfs+剪枝

    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.

    InputThe 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.
    OutputFor 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
    题意是问能不能在规定时间到达终点。
    dfs,剪枝很重要。避免做一些不必要的判断,还有起点不可以再走了。


    代码:


    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int flag,n,m,t,sx,sy,ex=-1,ey=-1,dir[4][2]={0,1,1,0,0,-1,-1,0},vis[10][10];
    char ar[10][10];
    void dfs(int x,int y,int time)
    {
        char ch;
        if(ar[x][y]=='D')
        {
            if(time==t)flag=1;
            return;
        }
        if((t-time-x-y+ex+ey)&1)return;///奇偶剪枝 以坐标和判断奇偶数
        if(flag||time>t)return;
        int tx,ty;
        for(int i=0;i<4;i++)
        {
            if(flag)return;
            tx=x+dir[i][0],ty=y+dir[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m||ar[tx][ty]=='X'||vis[tx][ty])continue;
            ch=ar[tx][ty];
            vis[tx][ty]=1;
            dfs(tx,ty,time+1);
            vis[tx][ty]=0;
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        while(cin>>n>>m>>t)
        {
            if(!n&&!m&&!t)break;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin>>ar[i][j];
                    if(ar[i][j]=='S')sx=i,sy=j;
                    else if(ar[i][j]=='D')ex=i,ey=j;
                }
            }
            flag=0;
            ar[sx][sy]='X';//起点一定要标记 不可以再走了!!!!!!!!!
            dfs(sx,sy,0);
            if(flag)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
  • 相关阅读:
    fiddler工具窗口功能介绍
    Fiddler导出jmx格式实现方法
    Fiddler抓包时一直请求:http://clients1.google.com:443
    内置函数的使用
    python中操作excel、ddt、config、logging方法
    Pycharm 将代码上传到GitHub
    unittest框架-优化一【变量参数化】
    excel的读取
    ddt数据驱动
    selenium中的js和jquery的相关用法
  • 原文地址:https://www.cnblogs.com/8023spz/p/7247828.html
Copyright © 2011-2022 走看看