hdoj1010(dfs+剪枝技巧)

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

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38218 Accepted Submission(s): 10304


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


 

Author
ZHANG, Zheng


 

Source


 

Recommend
JGShining
题意:给定起点要求在指定的那一个时间点走到迷宫,走过的路不能走。必须在指定的时间内抵达终点才行,BFS无法达到要求。
这题最大的收获就是学到了剪枝技巧
  1. 把矩阵标记成如下形式:
  2. 0,1,0,1,0
  3. 1,0,1,0,1
  4. 0,1,0,1,0
  5. 1,0,1,0,1
  6. 很明显,如果起点在0 而终点在1 那显然要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
  7. 在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
剪枝方法:奇偶剪枝

把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 需要奇数步

从 0->0 需要偶数步
那么设所在位置 (x,y) 与 目标位置 (dx,dy)

如果abs(x-y)+abs(dx-dy)为偶数,则说明 abs(x-y) 和 abs(dx-dy)的奇偶性相同,需要走偶数步

如果abs(x-y)+abs(dx-dy)为奇数,那么说明 abs(x-y) 和 abs(dx-dy)的奇偶性不同,需要走奇数步

理解为 abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!

而 (ti-setp)表示剩下还需要走的步数,由于题目要求要在 ti时恰好到达,那么 (ti-step) 与 abs(x-y)+abs(dx-dy) 的奇偶性必须相同

因此 temp=ti-step-abs(dx-x)-abs(dy-y) 必然为偶数!

5。最后一点:整个图的可以移动步数应该大于指定的时间

#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int n,m;
char a[7][7];
int sx,sy,dx,dy;
int ti,flag;
void dfs(int x,int y,int step)
{
  int temp;
  temp=ti-step-abs(dx-x)-abs(dy-y);//关键
  if(temp<0||temp%2==1)//利用奇偶行进行剪枝************
    return;
  int tx,ty,i;
  for(i=0;i<4;i++)//四个方向
  {
    tx=x+dir[i][0];//下一步的坐标
    ty=y+dir[i][1];
    if(a[tx][ty]=='D'&&step+1==ti)//还记得吧,起点这一步没有算进来,是终点,并且步数为给定的返回1
    {
      flag=1;
      return;
    }
    if(a[tx][ty]=='.'&&tx>=0&&tx<n&&ty>=0&&ty<m)//可走的路&&边界判断
    {
      a[tx][ty]='X';//走过的标记成不能走
      dfs(tx,ty,step+1);//深搜,步数+1
      a[tx][ty]='.';//清除标记回溯取消标记
      if(flag==1)//在进行一次判断
        return;
    }
  }
}
int main()
{
  while(cin>>n>>m>>ti)
  {
    int i,j;
    if(n==0&&m==0&&ti==0)
      break;
    int bushu=0;
    for(i=0;i<n;i++)
      for(j=0;j<m;j++)
      {
        cin>>a[i][j];
        if(a[i][j]=='S')//记录起点
        {
          sx=i;
          sy=j;
        }
        if(a[i][j]=='D')//记录终点
        {
          dx=i;
          dy=j;
        }
        if(a[i][j]=='.')//计算墙的个数用于后面的剪枝
          bushu++;
      }
    flag=0;
    if(ti>=bushu+1)//时间大于给定能走的路则输出NO,别忘了这里起点没有算一步
    {
      cout<<"NO"<<endl;
      continue;
    }
    dfs(sx,sy,0);//从起点进行深搜,开始步数为0
    if(flag==1)
      cout<<"YES"<<endl;
    else
      cout<<"NO"<<endl;
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值