Curling 2.0
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14563   Accepted: 6080

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

  1. 2 1
  2. 3 2
  3. 6 6
  4. 1 0 0 2 1 0
  5. 1 1 0 0 0 0
  6. 0 0 0 0 0 3
  7. 0 0 0 0 0 0
  8. 1 0 0 0 0 1
  9. 0 1 1 1 1 1
  10. 6 1
  11. 1 1 2 1 1 3
  12. 6 1
  13. 1 0 2 1 1 3
  14. 12 1
  15. 2 0 1 1 1 1 1 1 1 1 1 3
  16. 13 1
  17. 2 0 1 1 1 1 1 1 1 1 1 1 3
  18. 0 0

Sample Output

  1. 1
  2. 4
  3. -1
  4. 4
  5. 10
  6. -1

Source

 
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. int map[][];
  7. int n,m;
  8. int sx,sy,ex,ey;
  9. int nex[][]={,,,,,-,-,};
  10. int ans,step;
  11.  
  12. bool judge(int x,int y){
  13. if(x>=&&x<n&&y>=&&y<m&&map[x][y]!=)
  14. return true;
  15. return false;
  16. }
  17.  
  18. void dfs(int x,int y){
  19. if(step>)
  20. return;
  21. for(int i=;i<;i++){
  22. int tx=x+nex[i][];
  23. int ty=y+nex[i][];
  24. bool flag=false;
  25. while(judge(tx,ty)){
  26. flag=true;
  27. if(tx==ex&&ty==ey&&step<ans)
  28. ans=step;
  29. tx+=nex[i][];
  30. ty+=nex[i][];
  31. }
  32. if(map[tx][ty]==&&flag){
  33. step++;
  34. map[tx][ty]=;
  35. dfs(tx-nex[i][],ty-nex[i][]);
  36. step--;
  37. map[tx][ty]=;
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. int main(){
  45. while(scanf("%d%d",&m,&n)!=EOF){
  46. memset(map,,sizeof(map));
  47. if(n==&&m==)
  48. break;
  49. for(int i=;i<n;i++){
  50. for(int j=;j<m;j++){
  51. scanf("%d",&map[i][j]);
  52. if(map[i][j]==)
  53. sx=i,sy=j;
  54. if(map[i][j]==)
  55. ex=i,ey=j;
  56. }
  57. }
  58. ans=;
  59. step=;
  60. dfs(sx,sy);
  61. if(ans>)
  62. printf("-1\n");
  63. else
  64. printf("%d\n",ans);
  65.  
  66. }
  67. return ;
  68. }

poj3009 Curling 2.0 (DFS按直线算步骤)的更多相关文章

  1. POJ3009——Curling 2.0(DFS)

    Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...

  2. POJ3009 Curling 2.0(DFS)

    迷宫问题求最短路. 略有不同的是假设不碰到石头的话会沿着一个方向一直前进,出界就算输了.碰到石头,前方石头会消失,冰壶停在原地. 把这个当作状态的转移. DFS能够求出其最小操作数. #include ...

  3. POJ-3009 Curling 2.0 (DFS)

    Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...

  4. 【POJ】3009 Curling 2.0 ——DFS

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Desc ...

  5. Curling 2.0(dfs回溯)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15567   Accepted: 6434 Desc ...

  6. poj3009 Curling 2.0(很好的题 DFS)

    https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs ...

  7. POJ3009 Curling 2.0(DFS)

    题目链接. 分析: 本题BFS A不了. 00100 00001 01020 00000 00010 00010 00010 00010 00030 对于这样的数据,本来应当是 5 步,但bfs却 4 ...

  8. Curling 2.0(dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8795   Accepted: 3692 Description On Pl ...

  9. POJ3009 Curling 2.0

    正式做POJ的第一题,做出来后又看了别人的代码,就又完善了一下,也通过了.参考 http://blog.sina.com.cn/s/blog_4abcd9bc0100phzb.html 改了之后觉得写 ...

随机推荐

  1. Netweaver和CloudFoundry里的trace开关

    Netweaver 事务码SU01的参数设置里,有很多关于trace(跟踪)的开关. FPTRACELEVEL: 设成04后,能将ADS生成PDF的trace信息写入生成的PDF. 详细原理参考我的b ...

  2. 【BZOJ2006】[NOI2010] 超级钢琴(堆+RMQ)

    点此看题面 大致题意: 要你求出区间和前\(k\)大的区间的区间和之和,其中每个区间的大小在\(L\)与\(R\)之间. 堆+\(RMQ\) 这道题目,我们可以先对\(1\sim n\)中的每一个\( ...

  3. 浅谈KD-Tree

    前言 \(KD-Tree\)是一个十分神奇的东西,其实本质上类似于一个\(K\)维的二叉搜索树. 核心思想 \(KD-Tree\)的核心思想与\(BST\)是差不多的(插入等操作也都基本上一样). 唯 ...

  4. python_12_continue

    for i in range(9): if i<3: print("loop",i) else: continue#跳出本次循环,继续到下一循环 print('hehe... ...

  5. 对mysql快速批量修改,查重

    更新UPDATE mytable SET myfield = CASE id WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' E ...

  6. runtime运行时,类、对象、isa指针

    先查看一段OC源码,关于类的定义: /// An opaque type that represents an Objective-C class. typedef struct objc_class ...

  7. caller、callee的用法及区别

    1 :caller 返回一个调用当前函数的引用 如果是由顶层调用的话 则返回null (举个栗子哈 caller给你打电话的人  谁给你打电话了 谁调用了你 很显然是下面a函数的执行 只有在打电话的时 ...

  8. bootstrap validation submit

    表单提交校验功能 前端样式用bootstrap,依赖jquery,应用jquery自带的validation插件. 其实校验是一个小功能,做了还几天主要是因为碰到了两个问题,一个是对于提示信息样式添加 ...

  9. 学习笔记(六): Regularization for Simplicity

    目录 Overcrossing? L₂ Regularization Lambda Examining L2 regularization Check Understanding Glossay Ov ...

  10. 5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)

    使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...