现在的位置: 首页 > 综合 > 正文

POJ3009Curling 2.0解题报告

2013年01月18日 ⁄ 综合 ⁄ 共 5414字 ⁄ 字号 评论关闭
分类:DFS,迷宫
作者:ACShiryu
时间:2011-7-23
Curling 2.0
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5417 Accepted: 2230

Description

On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.


Fig. 1: Example of board (S: start, G: goal)

The movement of the stone obeys the following rules:

  • At the beginning, the stone stands still at the start square.
  • The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
  • When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)).
  • Once thrown, the stone keeps moving to the same direction until one of the following occurs:
    • The stone hits a block (Fig. 2(b), (c)).

      • The stone stops at the square next to the block it hit.
      • The block disappears.
    • The stone gets out of the board.
      • The game ends in failure.
    • The stone reaches the goal square.
      • The stone stops there and the game ends in success.
  • You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.


Fig. 2: Stone movements

Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.

With the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).


Fig. 3: The solution for Fig. D-1 and the final board configuration

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.

Each dataset is formatted as follows.

the width(=w) and the height(=h) of the board 
First row of the board 
... 
h-th row of the board

The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.

Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.

0 vacant square
1 block
2 start position
3 goal position

The dataset for Fig. D-1 is as follows:

6 6 
1 0 0 2 1 0 
1 1 0 0 0 0 
0 0 0 0 0 3 
0 0 0 0 0 0 
1 0 0 0 0 1 
0 1 1 1 1 1

Output

For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.

Sample Input

2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0

Sample Output

1
4
-1
4
10
-1
题目大意就是给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物也会随之消失,如果行动时超出方格的界限或行动次数超过了10则会game over .如果行动时经过3则会win,记下此时行动次数(不是行动的方格数),求最小的行动次数
对于此时
数据1: 3 2 可知起点的旁边是终点,故可以只需一步就到达终点
数据2:
1 0 0 2 1 0                 1 0 0 0 1 0      1 0 0 0 1 0      1 0 0 0 1 0     1 0 0 0 1 0
1 1 0 0 0 0                 1 1 0 0 0 0      1 1 0 0 0 0      1 0 0 0 0 0     1 0 0 0 0 0
0 0 0 0 0 3                 0 0 0 0 0 3      0 0 0 0 0 3      0 2 0 0 0 3     0 0 0 0 0 3/2
0 0 0 0 0 0                 0 0 0 0 0 0      0 0 0 0 0 0      0 0 0 0 0 0     0 0 0 0 0 0
1 0 0 0 0 1 可以这样移动    1 0 0 2 0 1 - >  0 2 0 0 0 1  ->  0 0 0 0 0 1 ->  0 0 0 0 0 1 
0 1 1 1 1 1                 0 1 1 0 1 1      0 1 1 0 1 1      0 1 1 0 1 1     0 1 1 0 1 1
故可以最小四步到达3
数据3:1 1 2 1 1 3 因为2的旁边都是障碍物,而能移动的条件是该方向附近一格无障碍物,所以2无路可走,故无法到达3,无解
其他数据的分析类似
由于题目给出要在10步内找到最优解,又每次可以向四个方向进行搜索,时间复杂度是O(4^10)=O((2^10)^2)=O(1000^2)=O(1000000)
在搜索时如果发现此时搜索的层次已经大于最优解,则可以回溯,因为继续向下搜也不会再出现更优解。
该开始的时候题目没看清,把输入长高高反了,WA了两次,最后终于发现了,下面是做题情况

参考代码:

  1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 int w,h;
9 struct prog{
10 int map[25][25];//迷宫地图
11 int x ; int y ;//此时的坐标
12 };
13 int step; //最优移动次数,初始时为11
14 void DFS ( prog tmp , int k )
15 {
16 if(k>=step) //如果递归层数不小于最优移动次数,就没必要继续搜索
17 return ;
18 int i , j ;
19
20 if(tmp.x+1<h&&tmp.map[tmp.x+1][tmp.y]!=1)
21 {//如果该处下面没越界,并且不是障碍物
22 for ( i = 1 ; tmp.x + i < h; i ++ )
23 {//从该处深度+1开始,一步步寻找
24 if(tmp.map[tmp.x+i][tmp.y]==1)
25 {//如果找到某处有障碍物,则从该处开始继续搜索
26 prog tmp2=tmp; //定义一个临时结构体
27 tmp2.map[tmp.x+i][tmp.y]=0; //将该处的障碍物消失
28 tmp2.x=tmp.x+i-1; //位置应为障碍物的上面
29 DFS(tmp2,k+1); //继续搜索
30 break;
31 }
32
33 if(tmp.map[tmp.x+i][tmp.y]==3)
34 {//如果经过了3,即终点
35 if(step>k+1) //看搜索的结果是否比最优解优,如果优,更新step
36 step=k+1;
37 return ;
38 }
39 }
40 }
41
42 //下面的搜索原理和上面类似,注释略
43
44 if(tmp.x-1>=0&&tmp.map[tmp.x-1][tmp.y]!=1)
45 {//向上
46 for ( i = 1 ; tmp.x - i >=0; i ++ )
47 {
48 if(tmp.map[tmp.x-i][tmp.y]==1)
49 {
50 prog tmp2=tmp;
51 tmp2.map[tmp.x-i][tmp.y]=0;
52 tmp2.x=tmp.x-i+1;
53 DFS(tmp2,k+1);
54 break;
55 }
56
57 if(tmp.map[tmp.x-i][tmp.y]==3)
58 {
59 if(step>k+1)
60 step=k+1;
61 return ;
62 }
63 }
64 }
65
66 if(tmp.y+1<w&&tmp.map[tmp.x][tmp.y+1]!=1)
67 {//向右
68 for ( i = 1 ; tmp.y + i < w; i ++ )
69 {
70
71 if(tmp.map[tmp.x][tmp.y+i]==1)
72 {
73 prog tmp2=tmp;
74 tmp2.map[tmp.x][tmp.y+i]=0;
75 tmp2.y=tmp.y+i-1;
76 DFS(tmp2,k+1);
77 break;
78 }
79
80 if(tmp.map[tmp.x][tmp.y+i]==3)
81 {
82 if(step>k+1)
83 step=k+1;
84 return ;
85 }
86 }
87 }
88
89 if(tmp.y-1>=0&&tmp.map[tmp.x][tmp.y-1]!=1)
90 {//向左
91 for ( i = 1 ; tmp.y - i >=0; i ++ )
92 {
93 if(tmp.map[tmp.x][tmp.y-i]==1)
94 {
95 prog tmp2=tmp;
96 tmp2.map[tmp.x][tmp.y-i]=0;
97 tmp2.y=tmp.y-i+1;
98 DFS(tmp2,k+1);
99 break;
100 }
101 if(tmp.map[tmp.x][tmp.y-i]==3)
102 {
103 if(step>k+1)
104 step=k+1;
105 return ;
106 }
107
108 }
109 }
110
111 }
112 int main()
113 {
114 while(cin>>w>>h,w&&h)
115 {//输入长高
116 prog cur;
117 int i , j ;
118 memset(cur.map,0,sizeof(cur.map));
119 for ( i = 0 ; i < h ; i ++ )
120 {
121 for ( j = 0 ; j < w ; j ++ )
122 {
123 cin >> cur.map[i][j];
124 if(cur.map[i][j]==2)
125 {//记录起点坐标
126 cur.x=i;
127 cur.y=j;
128 }
129 }
130 }
131 step=11;//初始化为11
132 DFS( cur , 0 );
133 if(step==11)//说明没找到解
134 cout<<-1<<endl;
135 else
136 cout<<step<<endl;
137 }
138 return 0;
139 }

  

抱歉!评论已关闭.