Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8810 | Accepted: 3703 |
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
一道搜索题,最初看到是求最快的方式,就用BFS宽搜,结果总是超时,后来改用了DFS+剪枝,终于过了这题
思路就是从起点开始DFS搜索路径,用一个step记录当前已搜出的最短路径,这样在后面的搜索中只要发现超过了step这么多步就不要继续搜了
由于题目要求不超过10步,所以step初始化为11
另外DFS过程中棋盘变化,我的记录方法是用一个map[x][y]记录初始棋盘,然后用一个vector,记录棋子走动导致的棋盘变化(由于棋盘的变化无非就是由1变为0,所以只要记下变化的位置就可以了)。
1 #include<iostream> 2 #include<vector> 3 #include<cstdio> 4 5 using namespace std; 6 7 typedef struct 8 { 9 int x; 10 int y; 11 } POINT; 12 13 int w,h,step; 14 int map[50][50]; 15 vector<POINT> path; 16 17 void dfs(int x,int y) 18 { 19 int g[50][50]; 20 if(path.size()<step) 21 { 22 for(int i=0;i<=h+1;i++) 23 for(int j=0;j<=w+1;j++) 24 g[i][j]=map[i][j]; 25 for(int i=0;i<path.size();i++) 26 g[path[i].x][path[i].y]=0; 27 if(g[x-1][y]==3||g[x+1][y]==3||g[x][y-1]==3||g[x][y+1]==3) 28 { 29 step=path.size()+1; 30 return ; 31 } 32 if(g[x-1][y]!=-1&&g[x-1][y]!=1) 33 { 34 for(int i=x-2;i>=0;i--) 35 if(g[i][y]==3) 36 { 37 step=path.size()+1; 38 return ; 39 } 40 else if(g[i][y]==1) 41 { 42 if(path.size()<step-1) 43 { 44 POINT temp; 45 temp.x=i; 46 temp.y=y; 47 path.push_back(temp); 48 dfs(i+1,y); 49 path.pop_back(); 50 } 51 break; 52 } 53 } 54 if(g[x+1][y]!=-1&&g[x+1][y]!=1) 55 { 56 for(int i=x+2;i<=h+1;i++) 57 if(g[i][y]==3) 58 { 59 step=path.size()+1; 60 return ; 61 } 62 else if(g[i][y]==1) 63 { 64 if(path.size()<step-1) 65 { 66 POINT temp; 67 temp.x=i; 68 temp.y=y; 69 path.push_back(temp); 70 dfs(i-1,y); 71 path.pop_back(); 72 } 73 break; 74 } 75 } 76 if(g[x][y-1]!=-1&&g[x][y-1]!=1) 77 { 78 for(int j=y-2;j>=0;j--) 79 if(g[x][j]==3) 80 { 81 step=path.size()+1; 82 return ; 83 } 84 else if(g[x][j]==1) 85 { 86 if(path.size()<step-1) 87 { 88 POINT temp; 89 temp.x=x; 90 temp.y=j; 91 path.push_back(temp); 92 dfs(x,j+1); 93 path.pop_back(); 94 } 95 break; 96 } 97 } 98 if(g[x][y+1]!=-1&&g[x][y+1]!=1) 99 { 100 for(int j=y+2;j<=w+1;j++) 101 if(g[x][j]==3) 102 { 103 step=path.size()+1; 104 return ; 105 } 106 else if(g[x][j]==1) 107 { 108 if(path.size()<step-1) 109 { 110 POINT temp; 111 temp.x=x; 112 temp.y=j; 113 path.push_back(temp); 114 dfs(x,j-1); 115 path.pop_back(); 116 } 117 break; 118 } 119 } 120 } 121 } 122 123 int main() 124 { 125 while(scanf("%d %d",&w,&h)==2) 126 { 127 path.clear(); 128 step=11; 129 if(w==0&&h==0) 130 break; 131 for(int i=1;i<=h;i++) 132 for(int j=1;j<=w;j++) 133 scanf("%d",&map[i][j]); 134 for(int i=0;i<=h+1;i++) 135 map[i][0]=map[i][w+1]=-1; 136 for(int j=0;j<=w+1;j++) 137 map[0][j]=map[h+1][j]=-1; 138 for(int i=1;i<=h;i++) 139 for(int j=1;j<=w;j++) 140 if(map[i][j]==2) 141 { 142 map[i][j]=0; 143 dfs(i,j); 144 } 145 printf("%d ",step>10?-1:step); 146 } 147 148 return 0; 149 }