深度搜索(2)

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.

/*剪枝很重要,可走的格数小于时间则减去,然后就是奇偶性剪枝
可以把map看成这样:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子
从为 1 的格子走一步,必然走向为 0 的格子
即:
 0 ->1或1->0 必然是奇数步
 0->0 走1->1 必然是偶数步
所以当遇到从 0 走向 0 但是要求时间是奇数的,或者, 从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!
*/

#include <iostream>
#include <stdlib.h>
#include <cstdio>
using namespace std;
char map[8][8];
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int n,m,t,step,remain,destx,desty;
bool found;

void dfs(int ic, int jc, int tc)
{
    if(tc==t&&ic==destx&&jc==desty)
        found=true;
    if(found)return;
    int v=t-tc-abs(destx-ic)-abs(desty-jc);           //奇偶性剪枝
    if(v<0||v&1)return;                                       //if((abs(destx-ic)+abs(desty-jc))%2!=(t-tc)%2)return;

for(int i=0;i<4;i++)
    {
        int ni=ic+dir[i][0];
        int nj=jc+dir[i][1];
        if(ni>=0&&ni<n&&nj>=0&&nj<m&&map[ni][nj]!=‘X‘)
        {
            map[ni][nj]=‘X‘;
            dfs(ni,nj,tc+1);
            map[ni][nj]=‘.‘;
        }
    }
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&t)==3&&(n+m+t))
    {
        int icur,jcur;
        step=0; remain=0; found=false;
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
           cin>>map[i][j];
           if(map[i][j]==‘S‘)
           icur=i,jcur=j;
           else if(map[i][j]==‘D‘)
          {
              remain++;
              destx=i;desty=j;
          }
           else if(map[i][j]==‘.‘) remain++;
        }
        map[icur][jcur]=‘X‘;
        if(remain>=t)
            dfs(icur,jcur,0);
        if(found)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

时间: 2024-08-03 18:03:31

深度搜索(2)的相关文章

[LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

DFS深度搜索的一般思想

对于无向图来说DFS深度搜索 递归思想 //深度优先搜索DFS的一般实现 void DFS(MGraph G,int i)//DFS递归思想 { int j; visited[i]=TRUE;//设置Node已经被访问 printf("%c",G.vexs[i]); for(j=0;j<numVertexes;j++)//对此Node跟Node2(j)遍历 如果arc[][]==1则表明当前DFS的Node与Node2(j)连通,且满足Node2未被访问的条件下 则对Node2进

创建二叉树 树的深度搜索 广度搜索

树的深度搜索 与树的前序遍历同理 根节点->左孩子->右孩子  树的广度搜索 与树的层次遍历同理 一层一层遍历内容 深度搜索 采用stack的适配器 先进后出原则  而广度搜索采用的queue适配器 先进先出原则 二者正好满足 搜索需求 简要代码如下: #include <iostream> #include <stack> #include <queue> #include <malloc.h> using namespace std; typ

[LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Hide Tags Depth-first Search Linked List 这题是将链表变成二叉树,比较麻烦的遍历过程,因为链表的限制,所以深度搜索的顺序恰巧是链表的顺序,通过设置好递归函数的参数,可以在深度搜索时候便可以遍历了. TreeNode * help_f(Lis

[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Hide Tags Tree Depth-first Search 简单的深度搜索 #include <iostream> using namespace std; /** *

[LeetCode] Path Sum II 深度搜索

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] Hide Tags Tree Depth-first

深度搜索应用之黑白图像(非递归)

深度搜索应用之黑白图像(非递归) 前言: 使用深度搜索,有两个方法:递归,栈.本质是栈. 递归有一个缺陷,栈溢出.栈有一个缺陷,程序相对递归更复杂. 练习题: 输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.(题意是让求连在一起的块有几个,图见书本)   使用递归求解:点这里 使用栈求解: 1 #include <iostream> 2 #include <memory.h> 3 #inc

一些项目——深度搜索

Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can mo

hdu--1978--记忆化深度搜索||递推

这题 我开始的做法是 记忆化搜索  但是tm地竟然tle了...很想不通 因为数据很小啊 就100 虽然方案的总数可能会很大.. 然后 我就去百度 记忆化搜索 看下是不是我用错方法了 事实证明 我虽然没有用错 但还是 学到了很多=-=. 其实  我很早以前 也看过关于 记忆化搜索的一些介绍 但是 并没有太多感觉 并不是那些作者写的不好 而是我的水平没到 感受不了.. 很多东西会在不知不觉中提升~ 我想 将我读的这篇介绍 传到这篇博客里来 似乎只能上传照片和视频=-=   那我给个链接吧  传送

[LeetCode] Populating Next Right Pointers in Each Node 深度搜索

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, al