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 <p>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.</p><p>Each dataset is formatted as follows.</p><blockquote><p><i>the width(=w) and the height(=h) of the board</i> <br><i>First row of the board</i> <br>... <br><i>h-th row of the board</i></p></blockquote><p>The width and the height of the board satisfy: 2 <= <i>w</i> <= 20, 1 <= <i>h</i> <= 20.</p><p>Each line consists of <i>w</i> decimal numbers delimited by a space. The number describes the status of the corresponding square.</p><blockquote><table id="table1"><tr><td>0 </td><td>vacant square</td></tr><tr><td>1 </td><td>block</td></tr><tr><td>2 </td><td>start position</td></tr><tr><td>3 </td><td>goal position</td></tr></table></blockquote><p>The dataset for Fig. D-1 is as follows:</p><blockquote><p>6 6 <br>1 0 0 2 1 0 <br>1 1 0 0 0 0 <br>0 0 0 0 0 3 <br>0 0 0 0 0 0 <br>1 0 0 0 0 1 <br>0 1 1 1 1 1</p></blockquote> Output <p>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.</p> 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 题目大意:给出一张地图,有一个起点2和终点3,1不能走,0可行 但是运动的规则改变了下,不是一次一格耗费一体力了,每次选择方向后,在没有碰到障碍1或者出街前是不能改变方向的,就和象棋里的“車”一样,也就是一次之内不能拐,走直线,在碰到障碍1之后,障碍物1变为0,碰到3便会停下来,现在才可以改变方向,问你出去的小的移动次数,如果到不了输出-1,根据案例三可以判断出121是移动不了的,也就是得有缓冲才能移动,ans十步之内 四为例 1 0 2 1 1 3 2 0 0 1 1 3 0 0 2 0 1 3 0 0 0 2 0 3 0 0 0 0 0 2 思路:bfs dfs都可以解决 最主要的一点就是撞击1之后地图的更新 Source Code#include<iostream> #include<stdio.h> #include<string.h> #include<queue> using namespace std; int n,m; int ans; int flag,map[1001][1001]; int vis[1001][1001]; int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; int sx,sy,ex,ey; void dfs(int xx,int yy,int step) { if(step>10) return ; for(int i=0;i<4;i++) { int x,y; int j=0; x=xx+dir[i][0]; y=yy+dir[i][1]; while(x>=0&&x<m&&y>=0&&y<n&&map[x][y]==0) { x+=dir[i][0]; y+=dir[i][1]; j++; } if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]==3) { if(ans>step+1) ans=step+1; if(ans<=10) flag=1; return ; } if(j>0) { if(x>=0&&x<m&&y>=0&&y<n) { int newx=x-dir[i][0]; int newy=y-dir[i][1]; map[x][y]=0; dfs(newx,newy,step+1); map[x][y]=1; } } } } int main() { int i,j; while(cin>>n>>m) { if(n==0&&m==0) break; memset(map,0,sizeof(map)); for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>map[i][j]; if(map[i][j]==2) { sx=i; sy=j; map[i][j]=0; } if(map[i][j]==3) { ex=i; ey=j; } } } ans=11; flag=0; dfs(sx,sy,0); if(flag) cout<<ans<<endl; else cout<<"-1"<<endl; } return 0; }