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

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

Source

 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int map[][];
int n,m;
int sx,sy,ex,ey;
int nex[][]={,,,,,-,-,};
int ans,step; bool judge(int x,int y){
if(x>=&&x<n&&y>=&&y<m&&map[x][y]!=)
return true;
return false;
} void dfs(int x,int y){
if(step>)
return;
for(int i=;i<;i++){
int tx=x+nex[i][];
int ty=y+nex[i][];
bool flag=false;
while(judge(tx,ty)){
flag=true;
if(tx==ex&&ty==ey&&step<ans)
ans=step;
tx+=nex[i][];
ty+=nex[i][];
}
if(map[tx][ty]==&&flag){
step++;
map[tx][ty]=;
dfs(tx-nex[i][],ty-nex[i][]);
step--;
map[tx][ty]=;
} } } int main(){
while(scanf("%d%d",&m,&n)!=EOF){
memset(map,,sizeof(map));
if(n==&&m==)
break;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==)
sx=i,sy=j;
if(map[i][j]==)
ex=i,ey=j;
}
}
ans=;
step=;
dfs(sx,sy);
if(ans>)
printf("-1\n");
else
printf("%d\n",ans); }
return ;
}

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. 修改CAS实现控制某个用户在定义的时间内登录次数

    思想: 在数据库增加字段  1.登录次数 2.登录失败时间(类型TimeStamp) 当一个用户进来认证的时候当登录失败的时候更新登录次数 和最后登录失败的时间. 主要是在登录成功或者失败的时候判断时 ...

  2. UVA 1642 Magical GCD(gcd的性质,递推)

    分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...

  3. SPOJ - MATSUM Matrix Summation---二维树状数组

    题目链接: https://vjudge.net/problem/SPOJ-MATSUM 题目大意: 二维数组,两种操作 SET 将某点设置成x SUM 求某个区域之和 解题思路: 这里用二维树状数组 ...

  4. 【洛谷2519】[HAOI2011] problem a(动态规划)

    点此看题面 大致题意: 一次考试共有\(n\)个人参加,第\(i\)个人说有\(a_i\)个人分数比他高,\(b_i\)个人分数比他低.求最少有几个人说谎. 动态规划 刚看完题目可以说是一头雾水. 仔 ...

  5. 题解 P4613 【[COCI2017-2018#5] Olivander】

    话说这道题,作为一个哈迷,是不能错过的 我很惊讶本蒟蒻竟然看得懂题面 好了,闲话少说,这道题,虽说是入门难度,但凭着良心说,它还是一道普及 - 的吧 看到标签,“高性能”,大脑的第一反应是快读. 是不 ...

  6. github不能加载css、js解决办法

    很奇怪,上午在公司还能正常访问github,晚点访问却有问题,页面样式明显错乱. 在FireFox下用F12开发者工具一看,有2条css和2条js 404 了,猜想应该是github的DNS被GFW污 ...

  7. Nginx学习记录(一)

    1. 什么是nginx Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支 ...

  8. shell脚本,利用awk计算指定范围内的和。

    期望得到结果如下: vivi 42800Tom 32500John 104500 解题方法如下: 1.利用数组来进行解题.

  9. for in 和 for of的区别详解

    for in 和 for of 相对于大家肯定都不陌生,都是用来遍历属性的没错.那么先看下面的一个例子: 例1 const obj = { a: 1, b: 2, c: 3 } for (let i ...

  10. win 系统下制作U盘安装 linux系统

    win 系统制作U盘安装硬盘镜像用ultraiso_v9.5.3.2901将Centos.iso写进U盘.安装过程全程区分大小写.过低的ultraiso不能正确读取文件.本文所有资料均能在网上免费下载 ...