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;
        }
    }
  • 相关阅读:
    BZOJ 3529 [Sdoi2014]数表
    bzoj 3195 [Jxoi2012]奇怪的道路
    BZOJ 4720 [Noip2016]换教室
    BZOJ 2160 拉拉队排练
    BZOJ 1031 [JSOI2007]字符加密Cipher 后缀数组教程
    BZOJ 1002 [FJOI2007]轮状病毒
    欧拉定理、拓展欧拉定理及其应用(欧拉降幂法)
    算术基本定理解析及其应用
    The 15th Zhejiang Provincial Collegiate Programming Contest(部分题解)
    网络流解析及其应用
  • 原文地址:https://www.cnblogs.com/8023spz/p/7247828.html
Copyright © 2011-2022 走看看