题目链接:点击打开链接

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.
  • 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

题意:
溜石游戏。在一给定大小的矩形冰面上,散布若干石块,给定石头的初始位置和终点,求从起点到达终点的最小步数,超过10次则视作不可达。其中规则如下,若石头与石块有相邻则不能向该方向滑动;每次溜石只能到达有石块的地方,且将其立即敲碎;若出界则视作失败。
做法:
对每一步到达的终点进行四个方向的搜索,直至搜到3或者搜索结束。
注意:
1,当搜索的步数大于10的时候,要终止搜索。
2,当在滑动过程中搜索到3的话,也要终止。
3,不要对图进行标记,因为搜索的方向是四个方向。标记会出错。
4,在标记完起点的位置后,记得把起点的置置为0;

<span style="font-size:24px;">#include <iostream>
#include<cstring>
#include<cmath>using namespace std;
int mp[30][30];
int w,h;
int sx,sy;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int  minsum;
void dfs(int x,int y,int step)
{if(step>10)return ;//大于10步就返回for(int i=0;i<4;i++){int nx=x+dx[i];int ny=y+dy[i];if(mp[nx][ny]==1)continue;int xx=x;int yy=y;int f=0;while(1)//沿着一个方向走{xx=xx+dx[i];yy=yy+dy[i];if(xx<0||yy<0||xx>=h||yy>=w)//边界结束{f=1;break;}else if(mp[xx][yy]==0)continue;else if(mp[xx][yy]==1)碰墙结束{break;}else if(mp[xx][yy]==3){minsum=min(minsum,step);return;}}if(f){continue;}mp[xx][yy]=0;dfs(xx-dx[i],yy-dy[i],step+1);mp[xx][yy]=1;}
}
int main()
{while(cin>>w>>h){if(w==0&&h==0)break;memset(mp,0,sizeof(mp));int x;for(int i=0;i<h;i++){for(int j=0;j<w;j++){cin>>x;mp[i][j]=x;if(x==2){mp[i][j]=0;sx=i;sy=j;}}}minsum=11;dfs(sx,sy,0);if(minsum<10)cout<<minsum+1<<endl;else cout<<"-1\n";}return 0;
}</span>

poj 3009 Curling 2.0相关推荐

  1. POJ 3009 Curling 2.0(深度优先搜索+剪枝)

    POJ 3009  Curling 2.0 题目大意: 在一块光滑的h*w的矩形平面上,有若干个障碍物,用1表示以及空格用0表示.现在有一个小球在平面上的数字2的地方,通过抛掷这个小球,使其达到数字3 ...

  2. poj 3009 Curling 2.0 (dfs的应用)

    http://poj.org/problem?id=3009 (1)这是一个用球撞石头的游戏,撞到石头,石碎球停.在规定的10次抛球机会下,若求移动到终点就赢,否则算输了(出界直接算输). (2)每一 ...

  3. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  4. POJ 3009 Curling 2.0(简单DFS)

    题意: 每一次碰到障碍则在障碍的旁边停下来,并且障碍被击碎.此时可以重新值掷一次冰球.当掷球次数超过 10 次则输出 -1. 思路: 1. 超过 10 次输出 -1 这个剪枝很关键: 2. 主要是要注 ...

  5. POJ 3009 Curling 2.0-DFS

    Curling 2.0 题意 就像最强大脑里的一个游戏,从当前位置移动,撞到障碍物后才会停止(所以有可能会冲出地图姐界面),障碍物被撞后会消失,求到达终点的最短路径. 但不同的是,如果路径上可以通过终 ...

  6. Curling 2.0{

    题目 Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22417   Accepted: 9100 D ...

  7. Curling 2.0 - POJ 3009

    参考的这个 思路: 枚举冰球下一个可能所在的所有位置然后DFS. 改了一天的BUG,结果发现这个的长宽是反过来输入的,裂开. #include <iostream> #include &l ...

  8. POJ Curling 2.0

    题目 题目真的长,还有很多细节. #include <iostream> #include <string> using namespace std;struct Coordi ...

  9. POJ3009 Curling 2.0

    原题链接:http://poj.org/problem?id=3009 暴力DFS + 回溯.注意行和列不要搞反,并且当搜索深度大于10的时候直接返回. View Code 1 #include &l ...

最新文章

  1. usaco Arithmetic Progressions(看了题解)
  2. MQ日常维护操作手册
  3. Time包详解二-timer和ticket.html
  4. 对Group By 语句的一次优化过程
  5. LeetCode之Sum of Left Leaves
  6. xml 和android脚本之家,Android开发之XML文件解析的使用
  7. NPOI导出Excel2007-xlsx
  8. python3语法学习第四天--字符串
  9. springboot分页展示功能_SpringBoot实战项目(三)用户列表以及分页功能实现
  10. C语言 常用标准库函数 初学者常用
  11. HTTP请求报文和响应报文、http协议状态码分类和常用状态码、Cookie、curl工具、httpd自带的工具、压力测试工具
  12. 入门物联网还得靠嵌入式
  13. BUUCTF刷题记录
  14. sql server 2008新建视图时出现对象名无效
  15. 什么是互联网产品的运营?,互联网营销
  16. hadoop中namenode退出安全模式
  17. 自由下落距离的计算(1019)
  18. 循黑线程序c语言,51单片机舵机循黑线小车程序
  19. Ubuntu下搭建第一台hadoop输入start-dfs.sh出现Permission denied (publickey,password)的问题
  20. 护眼灯真能护眼吗?学习专用的护眼灯推荐

热门文章

  1. C/C++语言100题练习计划 97——素数对
  2. php开发自己的composer包
  3. 04-小键盘字母u输出为4的问题
  4. 2021年,从事数据分析行业前景如何?还能转行数据分析师吗?(上)
  5. 腾讯云服务器备案要多久?腾讯云小程序备案流程
  6. 关于高精度交流恒流源设计是怎样的?
  7. 数学建模竞赛2022美赛
  8. python取出数组大于某值_Python替换NumPy数组中大于某个值的所有元素实例
  9. HDU 3605 Escape【最大流】
  10. html页面设计参考文献英文,网页设计参考文献(国外英文资料).doc