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. 【BZOJ1087】[SCOI2005] 互不侵犯King(状压DP)

    点此看题面 大致题意: 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案(国王能攻击到它周围的8个格子). 状压\(DP\) 一看到这道题我就想到了经典的八皇后问题,但 ...

  2. 用TreeView控件遍历磁盘目录

    实现效果: 知识运用: ListView控件中Items集合的Add方法  TteeView控件中Nodes集合的Add方法 实现代码: private void Form1_Load(object ...

  3. Feign + Hystrix 服务熔断和服务降级

    本机IP为  192.168.1.102 1.    新建 Maven 项目   feign 2.   pom.xml <project xmlns="http://maven.apa ...

  4. 列表与特殊字符,div(新手HTMLL基础)

    1.无序列表 -项目符号:实心圆(disc).方框(square).空心圆(circle) -列表<ul>---- 列表项<li>--- </li></ul& ...

  5. 操作系统(3)_CPU调度_李善平ppt

    不只上面的四种,比如时间片到了也会引起调度. 具体的调度算法: fcfs简单,但是波动很大. 最高相应比算法,执行时间最长就应该等待的长点,比sjf多了一个等待时间的考虑. 硬件定时器和软件计数器共同 ...

  6. 问题010:在Java中,什么是常量,什么是变量?

    Java中常量如何分类? 1.整数常量,所有的整数. 整数又分为 int (integer) 占用4个字节 一个字节占几个二进制位?8个二进制位,一个整型变量占32位二进制位 (内存中开辟出来的存储空 ...

  7. react的redux无状态组件

    Provider功能主要为以下两点: 在原应用组件上包裹一层,使原来整个应用成为Provider的子组件 接收Redux的store作为props,通过context对象传递给子孙组件上的connec ...

  8. DDOS与DOS的区别

    0x01 在说之前,先看一些例子吧 还有众所周知的中美黑客大战,新闻我就不说了 0x02 什么是DOS DoS是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻 ...

  9. Mbps、Kbps、bps、kb、mb区别和换算

    Mbps 即 Milionbit pro second(百万位每秒) Kbps 即 Kilobit pro second(千位每秒) bps 即 bit pro second(位每秒) 速度单位,bi ...

  10. PHP使用CURL_MULTI实现多线程采集

    $connomains = array( "http://www.baidu.com/", "http://www.hao123.com/", "ht ...