POJ 3009: Curling 2.0

http://poj.org/problem?id=3009

题目大意:

类似于打冰壶球,规则如下

1.推一下球,球就沿着某方向一直移动,直到出界,或者碰到障碍、或者到终点

2.碰到障碍会把障碍撞开(障碍变成空地)

3.推一下就直接撞墙,是不行的

 

思路是深度搜索,比较简单

 

  1 /*
  2 0    vacant square
  3 1    block
  4 2    start position
  5 3    goal position
  6 */
  7 
  8 #include <stdio.h>
  9 
 10 int w;    //列数
 11 int h;    //行数
 12 int maze[20][20];    //地图
 13 int sx, sy;    //起点坐标
 14 
 15 bool success;    //成功标志
 16 int result;        //最短次数
 17 
 18 int go[][2] =
 19 {
 20     0, 1,
 21     0, -1,
 22     1, 0,
 23     -1, 0
 24 };
 25 
 26 void dfs(int x, int y, int times)
 27 {
 28     if (times > 10)    //超过10次返回
 29         return;
 30 
 31     for (int i = 0; i < 4; i++)    //四个方向,深度搜索
 32     {
 33         //临时坐标变量
 34         int tx = x;
 35         int ty = y;
 36 
 37         //坐标的增量
 38         int dx = go[i][0];
 39         int dy = go[i][1];
 40 
 41         //一推直接撞墙,跳过
 42         if (tx + dx < 0 || tx + dx >= h || ty + dy < 0 || ty + dy >= w)    //判断一下边界,防止误读
 43             continue;
 44         if (maze[tx + dx][ty + dy] == 1)
 45             continue;
 46 
 47         while (true)    //不停走(一旦不满足,就break终止)
 48         {
 49             //坐标变化
 50             tx += dx;
 51             ty += dy;
 52 
 53             //越界(先检查,防止误读)
 54             if (tx < 0 || tx >= h || ty < 0 || ty >= w)
 55                 break;
 56 
 57             //是否是终点
 58             if (maze[tx][ty] == 3)
 59             {
 60                 if (success)    //已经有结果了
 61                 {
 62                     if (times < result)
 63                     {
 64                         result = times;
 65                     }
 66                 }
 67                 else   //还没结果
 68                 {
 69                     success = true;
 70                     result = times;
 71                 }
 72                 break;
 73             }
 74 
 75             //直到撞墙
 76             if (maze[tx][ty] == 1)
 77             {
 78                 maze[tx][ty] = 0;    //格子被撞开
 79                 dfs(tx - dx, ty - dy, times + 1);    //前一个位置停止,并且在前一个位置深搜
 80                 maze[tx][ty] = 1;    //不走这条路,格子复原
 81                 break;
 82             }
 83         }
 84     }
 85 }
 86 
 87 int main()
 88 {
 89     while (scanf("%d %d", &w, &h) != EOF)
 90     {
 91         if (w == 0 && h == 0)
 92             break;
 93 
 94         //输入
 95         for (int i = 0; i < h; i++)
 96         {
 97             for (int j = 0; j < w; j++)
 98             {
 99                 scanf("%d", &maze[i][j]);
100                 if (maze[i][j] == 2)    //开始坐标记录
101                 {
102                     sx = i;
103                     sy = j;
104                 }
105             }
106         }
107 
108         //处理
109         success = false;
110         dfs(sx, sy, 1);        //times表示本次循环是第几次操作
111 
112         //结果
113         printf("%d\n", success ? result : -1);
114     }
115 
116     return 0;
117 }

 

posted @ 2015-04-22 20:26  iswoit  阅读(128)  评论(0编辑  收藏  举报