Curling 2.0

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

题目大意:

    就是要求把一个冰壶从起点“2”用最少的步数移动到终点“3”

    其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动)

    除非冰壶撞到石头1 或者 到达终点 3

    撞到石头后,石头会碎掉,并且冰壶停在石头前面一格。

解题思路:

    DFS。

Code:

 /*************************************************************************
> File Name: poj3009.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月26日 星期日 21时37分15秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
//#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#define MAXN 50
using namespace std;
struct Point
{
int x,y;
} begin,end,dir[];
int map[MAXN][MAXN];
int N,M;
int cnt=;
bool dfs(Point begin,Point end,int step)
{
if (step>=) return false;
Point tmp;
for (int i=; i<=; i++)
{
tmp=begin;
if (map[tmp.x+dir[i].x][tmp.y+dir[i].y]!=)
{
while ()
{
tmp.x+=dir[i].x;
tmp.y+=dir[i].y;
if (tmp.x==end.x&&tmp.y==end.y)
{
cnt=cnt>step+?step+:cnt;
return true;
}
if (map[tmp.x+dir[i].x][tmp.y+dir[i].y]==)
break;
}
if (tmp.x+dir[i].x<=M&&tmp.x+dir[i].x>=
&&tmp.y+dir[i].y<=N&&tmp.y+dir[i].y>=)
{
map[tmp.x+dir[i].x][tmp.y+dir[i].y]=;
dfs(tmp,end,step+);
map[tmp.x+dir[i].x][tmp.y+dir[i].y]=;
}
}
}
return false;
}
void init()
{
dir[].x=,dir[].y=;
dir[].x=,dir[].y=-;
dir[].x=,dir[].y=;
dir[].x=-,dir[].y=;
}
int main()
{
init();
while (cin>>N>>M)
{
if (!N&&!M) break;
for (int i=; i<=; i++)
for (int j=; j<=; j++)
map[i][j]=;
for (int i=; i<=M; i++)
for (int j=; j<=N; j++)
{
scanf("%d",&map[i][j]);
if (map[i][j]==)
{
begin.x=i;
begin.y=j;
map[i][j]=;
}
if (map[i][j]==)
{
end.x=i;
end.y=j;
}
}
int step=;
cnt=;
dfs(begin,end,step);
if (cnt!=) cout<<cnt<<endl;
else cout<<"-1"<<endl;
}
return ;
}

POJ3009——Curling 2.0(DFS)的更多相关文章

  1. POJ3009 Curling 2.0(DFS)

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

  2. POJ-3009 Curling 2.0 (DFS)

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

  3. poj3009 Curling 2.0 (DFS按直线算步骤)

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

  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. WCF全面解析第二章 地址(Adress)

    2.1 统一资源标识(URL) 2.1.1 Http/Https 2.1.2 Net.TCP 2.1.3 Net.Pipe WCF只将命名管道专门用于同一台机器的跨进程通信. 2.1.4 Net.Ms ...

  2. Nginx+Tomcat动静分离

    需求:nginx处理用户请求的静态页面,tomcat处理用户请求jsp页面,来实现动态分离,nginx处理静态页面效率远高于tomcat,这样一来就能更好的提高并发,处理性能. 准备软件: 下载jdk ...

  3. 用python实现哈希表

    哈哈,这是我第一篇博客园的博客.尝试了一下用python实现的哈希表,首先处理冲突的方法是开放地址法,冲突表达式为Hi=(H(key)+1)mod m,m为表长. #! /usr/bin/env py ...

  4. 第一个leapmotion的小游戏

    自从看过leapmotion的宣传视频,就被吸引住了.觉得这东西迟早要替代鼠标,然后关注了一年多leapmotion的动态,终于在今年8月份入手了一只.//675大洋啊,心疼~ 一直想写份评测,一直想 ...

  5. 在iOS App的图标上显示版本信息

    最近读到一篇文章(http://www.merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/)介绍了一种非 ...

  6. iOS的SandBox的结构研究

    在模拟器中运行iOS程序,都会为该程序创建一个沙盒(SandBox).首先声明,我用的系统是Max OS X 10.7.3,编译器是Xcode 4.3.2.想要找到沙盒目录,先运行Finder,然后在 ...

  7. 十一、 BOOL类型、分支结构和关系运算符

    BOOL类型:表示非真即假.只有两个值:YES和NO,而二进制只识别二进制数,所以,将YES替换为“1”,NO替换为“0” BOOL数据类型占一字节的空间内存 BOOL数据类型输出为:%lu:输入为: ...

  8. MonoBehaviour

    所有的Unity脚本都继承自MonoBehaviour这个类,它没有Main函数入口,采用了事件触发的模式,根据不同的事件响应不同的函数. void Start(): void Update():每一 ...

  9. 史上最全的Excel数据编辑处理技巧(转)

    史上最全的数据编辑处理技巧,让你在日常数据分析处理的疯魔状态中解放出来. 一.隐藏行列 “不得了了,Excel出现灵异事件,部分区域消失不见了!”办公室里的一个MM跑过来大声喊叫着,着实吓了俺一跳.待 ...

  10. android禁止ScrollView自动滚动

    当Scrollview嵌套listview,或者子View的内容过多时,当内容加载完成后,ScrollView中内容的长度会发生改变,ScrollView会自动往下滚动,解决办法:在ScollView ...