sincerit 1010 Tempter of the Bone 搜索(重要的是怎么剪枝!!!)

1010 Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 150560 Accepted Submission(s): 40146

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

介绍两种剪枝的方法

  1. 奇偶剪枝法–效率高
    把矩阵标记成如下形式:
    0,1,0,1,0
    1,0,1,0,1
    0,1,0,1,0
    1,0,1,0,1
    很明显,如果起点在0 而终点在1 那显然 要经过奇数步才能从起点走到终点,依次类推,奇偶相同的偶数步,奇偶不同的奇数步
    在读入数据的时候就可以判断,并且做剪枝,当然做的时候并不要求把整个矩阵0,1刷一遍,读入的时候起点记为(Si,Sj) 终点记为(Di,Dj) 判断(Si+Sj) 和 (Di+Dj) 的奇偶性就可以了
    abs(beginx-endx) + abs(beginy - endy)表示现在所在的格子到目标格子的距离(不能走对角线)
    t-cnt是剩余能用的步数,将他们做差
    如果temp < 0或者temp为奇数,那就不可能到达!
    int temp = t - abs(beginx-endx) - abs(beginy-endy);
    if (temp < 0 || temp&1) { // 等于0刚好能到达
    printf(“NO\n”);
    continue;
    }
    当小于0时说明不要走那么步也能到,说明没达到需要走的步数,不和要求
    为什么temp为奇数就不能到?
    剩余能用步数 - 到达目的需要走的步数 = 奇数
    偶 - 奇 = 奇 剩余能用的步数为偶数而实际却要走奇数步, 不合要求
    奇 - 偶 = 奇 剩余能用的步数为奇数而实际却要走偶数步, 不合要求

2.路径剪枝:
矩阵的大小是NM 墙的数量记为wall 如果能走的路的数量 NM - wall 小于等于时间T,就是说走完也不能到总的时间的,这显然是错误的,可以直接跳出了
if (n*m - wall <= t) { // 12个格子墙占了9个要求走三步,但第一个开始走的地方算成0,只要两步就能到
printf(“NO\n”);
continue;
}
比如有12个格子,墙占了9个格子,要求走三步,但第一个格子也就是起点是不算走过一秒的,所以只要两步就能到, 12 - 9 <= 3(还剩三个格子)刚好等于三个格子也不能达到,达到目的地的步数小于三更不符合要求

好的方法要分享: http://acm.hdu.edu.cn/forum/read.php?tid=6158

#include <stdio.h>
#include <math.h>
#include <cstring>
int n, m, t, flag;
int beginx, beginy;
int endx, endy; 
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
int vis[20][20];
char map[20][20];
int check(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || map[x][y]=='X') return 0;
  return 1;
}
void DFS(int x, int y, int cnt) {
  if (flag) return; // 找到了就减少搜索,减少时间消耗 
  if (cnt >= t) {  // 递归出口, 这个代码可以当成搜索递归的出口的模板 
    if (cnt == t && map[x][y] == 'D') {
      flag = 1;
      return;
    }
    return;
  }
  // 奇偶剪枝法
  int temp = (t - cnt) - abs(x-endx) - abs(y-endy); // x y是要从当前位置算起 
  if (temp < 0 || temp&1) { // 等于0刚好能到达 
    return;
  } 
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (check(nx, ny)) {
      vis[nx][ny] = 1;
      DFS(nx, ny, cnt+1);
      vis[nx][ny] = 0; // 回溯返回上一层时去除标记 
    }
  }
}
int main() {
  while (scanf("%d%d%d", &n, &m, &t), n||m||t) {
    int wall = 0;
    for (int i = 0; i < n; i++) {
      getchar();
      scanf("%s", &map[i]);
      for (int j = 0; j < m; j++) {
        if (map[i][j] == 'S') {
          beginx = i;
          beginy = j;
        } else if (map[i][j] == 'D') {
          endx = i;
          endy = j;
        } else if (map[i][j] == 'X') 
          ++wall;
      } 
    }
    // 路径剪枝法 
    if (n*m - wall <= t) { // 12个格子墙占了9个要求走三步,但第一个开始走的地方算成0,只要两步就能到 
      printf("NO\n");
      continue;
    }
    flag = 0;
    memset(vis, 0, sizeof(vis));
    vis[beginx][beginy] = 1;
    DFS(beginx, beginy, 0);
    if (flag) printf("YES\n");
    else printf("NO\n");
  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/sincerit/article/details/83473448
今日推荐