深搜+回溯 poj3009 Curling 2.0
来源:互联网 发布:网络推广方式方法有哪些 编辑:程序博客网 时间:2024/06/10 06:04
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.
0vacant square1block2start position3goal 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 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
#include<stdio.h>#include<string.h>#include<algorithm>#include<limits.h>#include<math.h>using namespace std;const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};int m,n,start_x,start_y,end_x,end_y,xx,yy,minn,xxx,yyy;int a[25][25];bool panduan(int aa,int bb){ if((aa>=1)&&(aa<=n)&&(bb>=1)&&(bb<=m)) return true; else return false;}bool panduan2(int i,int x,int y)//判断能不能走并给出走到哪里,i是方向,xy是起始位置坐标{ xx=x+dx[i]; yy=y+dy[i]; if(panduan(xx,yy)&&(a[xx][yy]==3))//如果下一步就是结束点,直接return return true; if((!panduan(xx,yy))||(a[xx][yy]==1))//如果下一步越界或者下一步是障碍不能走 return false; while(panduan(xx,yy)&&(a[xx][yy]!=1))//走到这里,说明下一步既不是终点,而且能走,找出最后的的停止点 { xx=xx+dx[i]; yy=yy+dy[i]; if((a[xx][yy]==3)) return true; } if(a[xx][yy]!=1)//假如xx,yy此时越界,而且这一格子正好是1,那就完蛋了。所以要赋初值 return false; xxx=xx;//保存这时候障碍物的位置xxx,yyy yyy=yy; xx-=dx[i]; yy-=dy[i]; return true;}void dfs(int step,int x,int y){ int temp_xxx,temp_yyy; //printf("(%d,%d),step=%d\n",x,y,step); int temp_x,temp_y; if((step>=10)||((x==end_x)&&(y==end_y))) return ; for(int i=0;i<4;i++) { if(panduan2(i,x,y)) { temp_x=xx; temp_y=yy; if((xx==end_x)&&(yy==end_y)) { minn=min(step+1,minn); //printf("++\n"); } a[xxx][yyy]=0;//更改状态 temp_xxx=xxx; temp_yyy=yyy;//将这次dfs对应的block位置保存在这层递归中,防止被破坏 dfs(step+1,xx,yy); a[temp_xxx][temp_yyy]=1;//回溯 } }}void duru(void){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&a[i][j]); if(a[i][j]==2) { start_x=i; start_y=j; a[i][j]=0; } if(a[i][j]==3) { end_x=i; end_y=j; } }}int main(void){ while(1) { scanf("%d%d",&m,&n); memset(a,-1,sizeof(a)); if(m*n==0) goto myk; duru(); minn=INT_MAX; dfs(0,start_x,start_y); if(minn==INT_MAX)//走不到的情况 minn=-1; printf("%d\n",minn); } myk:return 0;}
- 深搜+回溯 poj3009 Curling 2.0
- poj3009 Curling 2.0(DFS回溯)
- POJ3009 Curling 2.0 DFS 深搜
- 【POJ3009】Curling 2.0(深搜)
- POJ3009:Curling 2.0(DFS)
- POJ3009--Curling 2.0
- poj3009 Curling 2.0
- poj3009 Curling 2.0---bfs
- poj3009 Curling 2.0
- poj3009 Curling 2.0
- POJ3009 Curling 2.0(DFS)
- POJ3009 Curling 2.0
- POJ3009---Curling 2.0
- POJ3009 Curling 2.0
- POJ3009 Curling 2.0 DFS
- POJ3009 Curling 2.0【DFS】
- poj3009 Curling 2.0
- POJ3009 Curling 2.0(DFS)
- Lua中的loadfile、dofile、require详解
- rt3070sta GNU / Linux 64 bit driver working
- Verify Preorder Serialization of a Binary Tree
- 如何防止重复发送Ajax请求
- 在线免费生成IntelliJ IDEA 15.0注册码
- 深搜+回溯 poj3009 Curling 2.0
- Java23种设计模式简介与原则
- C++面试题
- 关于join执行时where的执行时间
- CAS Server 4.0 + RESTfull + Services调用 完全使用文档
- 浏览器插件之ActiveX开发(一)
- Android 65k 问题
- Java并发编程:volatile关键字解析
- ./ggsci: error while loading shared libraries: libnnz11.so: cannot open shared object file: No such