Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19734 | Accepted: 8063 |
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.
- The stone hits a block (Fig. 2(b), (c)).
- 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
Source
大致题意:
1.给定一个w*h的网格,其中一个方格为起点S(2),一个方格为终点(3)。其余方格分为两种:1.内有石头的方格(1)2.空方格(0)。
2.冰壶球开始时静止于起点方格内,目标是用最少的步数将其移动到终点方格。
3.冰壶球的移动规则:(1)冰壶球可以向上下左右四个方向扔出。扔出的冰壶球将持续前进知道它碰到石头后停止。冰壶球停止的位置不是石头方格,而是石头旁边的那个方格。(2)冰壶球碰到石头后,会将其撞碎,原石头方格变为空。(3)若冰壶球所在方格紧邻的方格(上、下、左、右)中有石头,冰壶球不能向有石头的方向扔。(距离为1时,冰壶球无法撞碎石头)(4)冰壶球经过终点时会自动停止,视为游戏成功。
4.(1)若冰壶球滑出网格边缘,视为游戏失败。(2)最多移动10次,10次后没有到达终点视为游戏失败。
5.游戏成功时,输出最少的移动次数。游戏失败时,输出-1。
总体思路:
带回溯的深度优先搜索。
解题过程:
看到最少移动次数果断用了广搜。但这道题会在移动过程中改变网格条件,导致不同的情况会相互干扰。因此,此题不宜用广搜,用带回溯的深搜。
#include<cstdio>
#include<queue>
#include<cstring>
#define p(x,y) ( (x-1)*col+y )
int B [411];
int cow,col;
int ans;
void dfs (int de,int S,int E)//参数:de 搜索深度,实际代表“已经扔了多少次”。S 冰壶球当前位置,本次搜索的起点。E 最后的终点
{
if(de>10) return;
int x=(S-1)/col+1,y=(S-1)%col+1;//把传进来的起点位号转化为坐标
int i;
for(i=1;x+i<=cow&&B[p(x+i,y)]!=1;i++)//先向下走
if(B[p(x+i,y)]==3&&de<ans) ans=de;
if(i>1&&x+i<=cow){//i==1 表示石头紧挨着冰壶球,此方向不能走。若x+i>cow,说明冰壶球沿此方向走将滑出边界,不能走。
B[p(x+i,y)]=0;//把碰到的这块石头击碎
dfs(de+1,p(x+i-1,y),E);//下一层搜索
B[p(x+i,y)]=1;//搜索完成后将本次撞碎的石头还原
}
for(i=1;x-i>=1&&B[p(x-i,y)]!=1;i++)//向上走
if(B[p(x-i,y)]==3&&de<ans) ans=de;
if(i>1&&x-i>=1){
B[p(x-i,y)]=0;
dfs(de+1,p(x-i+1,y),E);
B[p(x-i,y)]=1;
}
for(i=1;y-i>=1&&B[p(x,y-i)]!=1;i++)//向左走
if(B[p(x,y-i)]==3&&de<ans) ans=de;
if(i>1&&y-i>=1){
B[p(x,y-i)]=0;
dfs(de+1,p(x,y-i+1),E);
B[p(x,y-i)]=1;
}
for(i=1;y+i<=col&&B[p(x,y+i)]!=1;i++)//向右走
if(B[p(x,y+i)]==3&&de<ans) ans=de;
if(i>1&&y+i<=col){
B[p(x,y+i)]=0;
dfs(de+1,p(x,y+i-1),E);
B[p(x,y+i)]=1;
}
}
int main ()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&col,&cow)&&col!=0){
ans=11;//将ans初始化为11,若搜索失败,此ans值对应输出-1
int tot=col*cow;//实例的格子数
int S,E;
for(int i=1;i<=tot;i++){
scanf("%d",B+i);
if(B[i]==2) S=i;//S记录起点位号
if(B[i]==3) E=i;//E记录终点位号
}
dfs(1,S,E);
if(ans<=10) printf("%d\n",ans);
else printf("-1\n");
}
return 0;;
}