1. 程式人生 > >sincerit 1010 Tempter of the Bone 搜尋(重要的是怎麼剪枝!!!)

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;
}