POJ 3009 Iterative deepening Search Curling 2.0
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 12986 |
|
Accepted: 5460 |
Description
On Planet MM-21, after their Olympic, the year, and curling is getting popular. But the rules is somewhat different from ours. The game is played on the Ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to leads 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 is occupied with blocks. There is special squares namely the start and the goal, which is not occupied with blocks. (These squares is distinct.) Once The stone begins to move, it'll proceed until it hits a block. In order to bring the stone to the goal, your may has 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 is restricted to X and y directions. Diagonal moves is prohibited.
- When the stone stands still, you can make it moving by throwing it. 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 hits.
- 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 to more than times in a game. If The stone does not reach the goal in 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 O F moves required.
With the initial configuration shown in Fig. 1, 4 moves is 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 had 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 to a line containing and a 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 <=, 1 <= h <= 20.
Each line consists of the 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 has a decimal integer indicating the minimum number of moves along a route from the star T to the goal. If There is no such routes, print-1 instead. Each of the line should is not a character other than this number.
Sample Input
2 13 26 61 0 0 2 1 01 1 0 0 0 00 0 0 0 0 30 0 0 0 0 01 0 0 0 0 10 1 1 1 1 16 11 1 2 1 1 36 11 0 2 1 1 312 12 0 1 1 1 1 1 1 1 1 1 313 12 0 1 1 1 1 1 1 1 1 1 1 30 0
Sample Output
14-1410-1
Test instructions
A known starting and ending point, the shortest path to the end of the stone from the starting point, or 1 if it is unreachable. The specific rules for gravel movement are as follows:
1. At the beginning, the stone is at the beginning S
2, the direction of movement can be horizontal or vertical, can not oblique direction of movement
3, at the beginning, you can throw a stone up or down in either direction, except if the point adjacent to it is an obstruction.
4, once the stone began to exercise, there are three possibilities:
A, encounter obstacles, the stone will stop in the front of the obstacle, the obstacles will disappear
B. If out of bounds, the game fails
c, reach the end, the game is over and successful
5, if the number of moves more than 10 times, will think the game is a failure
Idea: The previous shortest-circuit is a lattice to expand, the direct linear expansion, BFS,DFS, iterative deepening can be solved, originally wanted to use BFS, and then found BFS difficult to handle the direction, instead of Dfs, but also did not AC, the next day with an iterative deepening of direct AC, very study on the choice of methods, DFS is not suitable for the shortest path, BFS is difficult to handle the direction, the best practice is of course, the iterative deepened, that is, each limit the number of steps to search, and gradually increase the maximum number of steps. Later thought to be similar to 8 digital question Bfs+hash also very simple
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;Const intmaxn= -;Const intInf= (1<< -);Const intstop=-1;//the direction state is stillintM,n;intMAP[MAXN][MAXN];intSx,sy;//starting pointintans;BOOLFlag;intdx[]={0,0,-1,1};//4 Directionsintdy[]={-1,1,0,0};voidDfsintXintYintDirintNowstep,intSteplimit)//Nowstep is the current number of steps, and steplimit is the limit step for the iterative deepening search{ if(flag)return; if(Nowstep>steplimit)return; if(map[x][y]==3) {flag=1; return; } if(Dir==stop) {//when the direction is stationary for(intI=0;i<4; i++){ intnx=x+dx[i],ny=y+Dy[i]; if(map[nx][ny]==-1|| map[nx][ny]==1)Continue; DFS (Nx,ny,i,nowstep+1, Steplimit); } return; } intNx=x+dx[dir],ny=y+dy[dir];//when the direction is non-stationary if(map[nx][ny]==-1)return; if(map[nx][ny]==1){//when the next step is the wallmap[nx][ny]=0; DFS (X,Y,STOP,NOWSTEP,STEPLIMIT); Map[nx][ny]=1; } ElseDFS (NX,NY,DIR,NOWSTEP,STEPLIMIT);//The next step is not the wall}intMain () { while(cin>>m>>n,m| |N) {memset (map,-1,sizeof(map)); for(intI=1; i<=n;i++){ for(intj=1; j<=m;j++) {scanf ("%d",&Map[i][j]); if(map[i][j]==2) {SX=i,sy=J; } }} flag=0; for(intI=1; i<=Ten; i++) {//Iterative deepened SearchDFS (Sx,sy,stop,0, i); if(flag) {ans=i; Break; } } if(flag) cout<<ans<<Endl; Elsecout<<-1<<Endl; } return 0;}
poj3009_ iterative deepened search
poj3009--Iterative deepened Search