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

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

 
题解:题目看起来很复杂,其实很简单,我就不翻译了。由于深度较小,直接上DFS就可以了。
 
AC代码:
 
 #include <cstdio>
#include <cstring>
#define MAX(a, b) (a>b?a:b) const int LEN = ; const int vec[][] = { {, -, , , }, {, , -, , }}; //方向向量 int map[LEN][LEN];
int ans;
int r, c; void dfs(int x, int y, int dir, int res)
{
if (res > ){ //超过十步 return
return;
}
int rx = ;
int ry = ;
if (dir != ){
for(int i = ; i < MAX(r, c); i++){
x += vec[][dir];
y += vec[][dir];
if (x < || x > c || y < || y > r){
return;
}
if (map[y][x] == ){ //到达终点,与ans比较大小
if (res < ans)
ans = res;
return;
}
if (map[y][x] == ){
map[y][x] = ;
rx = x;
ry = y;
x -= vec[][dir];
y -= vec[][dir];
break;
}
}
}
if (x- >= && map[y][x-] != )
dfs(x, y, , res+);
if (x+ <= c && map[y][x+] != )
dfs(x, y, , res+);
if (y- >= && map[y-][x] != )
dfs(x, y, , res+);
if (y+ <= r && map[y+][x] != )
dfs(x, y, , res+);
map[ry][rx] = ;
} int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d %d", &c, &r) != EOF && r+c){
int x, y;
for(int i = ; i <= r; i++){
for(int j = ; j <= c; j++){
scanf("%d", &map[i][j]);
if (map[i][j] == ){
y = i;
x = j;
}
}
}
ans = 0x7fffffff;
dfs(x, y, , );
if (ans != 0x7fffffff)
printf("%d\n", ans);
else
printf("-1\n");
}
return ;
}

【POJ】3009 Curling 2.0 ——DFS的更多相关文章

  1. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  2. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

  3. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  4. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  5. 【翻译】Flume 1.8.0 User Guide(用户指南) Processors

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  6. 【翻译】Flume 1.8.0 User Guide(用户指南) Channel

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  7. 【翻译】Flume 1.8.0 User Guide(用户指南) Sink

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  8. 【翻译】Flume 1.8.0 User Guide(用户指南) source

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  9. 【翻译】Flume 1.8.0 User Guide(用户指南)

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

随机推荐

  1. javascript 数据结构和算法读书笔记 > 第二章 数组

    这章主要讲解了数组的工作原理和其适用场景. 定义: 一个存储元素的线性集合,元素可以通过索引来任意存取,索引通常是数字,用来计算元素之间存储位置的偏移量. javascript数组的特殊之处: jav ...

  2. Messenger类的使用

    一.Messenger类 作用:类似Message类,但是是跨进程使用的. 解析:它的底层是由AIDL实现的,从构造方法可以看出 //Service使用 public Messenger(Handle ...

  3. jquery.post方法回调函数

    1 function(data){} 此post请求成功后调用之,data是请求成功后服务器返回的东西.如果在servlet中有response.getWriter().println("s ...

  4. 【第一篇章-android平台buffer播放探索】native media

    在android平台,从4.0开始,提出了openmax架构,所以在DNK的R7版本中有了openmax AL层播放的DEMO即native media,这个DEMO就是读本地文件,然后把所读buff ...

  5. Centos6.5升级gcc for qt5.3.1

    1.升级GCC CentOS6.5内置的GCC版本为4.4,而Qt5.2.1则需要4.8.2的支持(支持C++ 11特性),因此,必须先升级GCC wget http://ftp.tsukuba.wi ...

  6. 刚子扯谈:源于Chanel的图片描述

    文/刚子 2013年8月9日 北京晴 猴晒猴晒 开片语:真心不知道今天该分享点啥?先扯几句牢骚,我个人认为对朋友也够意思,虽然他们有的时候挺操蛋的,虽然还简称哥们儿,虽然还一起交流,但已经变成了无谓的 ...

  7. SSH登陆服务器的简单命令

    SSh 用户名@目标Ip: 回车输入密码:

  8. 在自定义的js验证规则中调用magento的VarienForm方法验证表单

    js部分<script type="text/javascript"> //<![CDATA[ var loginForm = new VarienForm('l ...

  9. 传纸条(一)(双线程dp)

    传纸条(一) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...

  10. HDU Today(dijskra)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...