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. 限制TextBox输入,只能输入整数

    public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...

  2. ubuntu常用命令(转)

    1.打开终端的方法 Ubuntu 中按左侧栏的第一个“面板主页(Dash 主页)”(可以按win键调出),在里面输入terminal可以打开终端,另外打开终端的快捷键是Ctrl+Alt+T 2.修改用 ...

  3. docker 容器扩盘

    docker:/root/sbin# cat add_fs.sh #!/bin/bash #This script is dynamic modify docker container disk #A ...

  4. perl 创建包

    <pre name="code" class="python"><pre name="code" class=" ...

  5. NPOI操作EXCEL--设置密码及设置只读

    有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完成,如下图:      那么,在NPOI中有没有办法通过编码的 ...

  6. 泛型、注解、log4j

    泛型.注解.log4j 泛型:将运行阶段的类型错误提前到编译阶段. 声明泛型必须两端的一致,要么左面有,要么右边有,两边都有的两边必须一致. 泛型方法: static 之后 返回类型之前进行声明 泛型 ...

  7. Freedur为什么会免费?

    难道没人看看他们的官方站点吗? Freedur倒闭了...... 一个中国人,Chris Lee,作为Freedur的会计师,窃取了公司的银行帐号.并将Freedur的官方站点指向自己的空间.而且声称 ...

  8. Attempt to call getDuration without a valid mediaplayer

    最近在做一个播放器的小例子,中途遇到 了这个错: Attempt to call getDuration without a valid mediaplayer 解决参考方案如下: 一是如果media ...

  9. cocoa pods出现的错误

    ERROR:  While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/pod 解决办法: sudo gem ...

  10. JS onkeydown控制HTML Input 只录入浮点数值

    // -1) return false; return index == 0 } keychar = String.fromCharCode(keynum) var newVal = oriVal.s ...