Curling 2.0
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 90 Accepted Submission(s) : 27
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
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
1 4 -1 4 10 -1题目大意:给出一张地图,有一个起点2和终点3,1不能走,0可行但是运动的规则改变了下,不是一次一格耗费一体力了,每次选择方向后,在没有碰到障碍1或者出街前是不能改变方向的,就和象棋里的“車”一样,也就是一次之内不能拐,走直线,在碰到障碍1之后,障碍物1变为0,碰到3便会停下来,现在才可以改变方向,问你出去的小的移动次数,如果到不了输出-1,根据案例三可以判断出121是移动不了的,也就是得有缓冲才能移动,ans十步之内四为例1 0 2 1 1 32 0 0 1 1 30 0 2 0 1 30 0 0 2 0 30 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; }