Curling 2.0
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26408   Accepted: 10546

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.

vacant square
block
start position
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,遇到1才可以停下来,并且可以撞掉1,前提是前一个是0,走了十次以后也算over
题解:dfs,能往前走就往前走,不能往前走在看自己走了几步
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int m,n,st,en,a[maxn][maxn],ans,flag;
int dir[][]={,,,,,-,-,}; int judge(int &x,int &y,int xx,int yy)
{
int num=;
x=x+xx; y=y+yy;
num++;
while(x>= && x<n && y>= && y<m)
{
if(a[x][y]==)
return ;
else if(a[x][y]==)
{
if(num>)
return ;
return ;
}
x=x+xx;
y=y+yy;
num++;
}
return ;
} void dfs(int x,int y,int k)
{
if(k>)
return ;
for(int i=;i<;i++)
{
int xx=x;
int yy=y;
int num=judge(xx,yy,dir[i][],dir[i][]);
if(num==)
{
ans=min(ans,k+);
flag=;
return ;
}
else if(num==)
{
a[xx][yy]=;
dfs(xx-dir[i][],yy-dir[i][],k+);
a[xx][yy]=;
}
}
}
int main()
{
while(scanf("%d %d",&m,&n) && (m||n))
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==)
{
st=i;
en=j;
}
}
a[st][en]=;
flag=;ans=;
dfs(st,en,);
if(flag && ans<=)
printf("%d\n",ans);
else
printf("-1\n");
}
}

poj-3009 curling2.0(搜索)的更多相关文章

  1. 【POJ 3009 Curling2.0 迷宫寻径 DFS】

    http://poj.org/problem?id=3009 模拟冰壶的移动,给出到达终点的最少投掷次数(不可达时为-1). 具体移动规则如下: 每次选四个方向之一,沿此方向一直前进,直到撞到bloc ...

  2. POJ 3009 Curling 2.0【带回溯DFS】

    POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...

  3. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  4. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  5. destoon6.0搜索页熊掌号页面改造技巧【原创】

    大家都知道,DT官方是封禁搜索页的,是不让百度蜘蛛抓取的,但是搜索页又是大型网站优化的重点,今天来说说关于DT6.0搜索页熊掌号的改造方法,如果您要改造内容页面可以查看我前几期的分享! 首先要开启百度 ...

  6. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  7. poj 3009 Curling 2.0

    题目来源:http://poj.org/problem?id=3009 一道深搜题目,与一般搜索不同的是,目标得一直往一个方向走,直到出界或者遇到阻碍才换方向. 1 #include<iostr ...

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

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

  9. DFS:Curling 2.0(POJ 3009)

      冰壶2.0 题目大意:就是给你一个冰壶和一个地图,地图上有石头,冰壶只能沿着x方向和y方向运动,并且要一直运动直到撞到石头为止,并且沿着此方向撞过来会把挡住的石头撞没,冰壶在停的时候可以扔出去一次 ...

  10. POJ 3009 Curling 2.0 回溯,dfs 难度:0

    http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...

随机推荐

  1. Kestrel服务器

    Kestrel服务器 什么是Kestrel服务器 Kestrel是开源的(GitHub提供的源代码),事件驱动的异步I / O服务器,用于在任何平台上托管ASP.NET应用程序.这是一个监听服务器和一 ...

  2. Function.prototype.apply.call

    我们先从一道简单的题目开始,前几天在git上看到的: 定义log方法,它可以代理console.log的方法.log(1,2,3)  =>  1 2 3 通常,你的答案会是这样的: functi ...

  3. 使用Zeppelin时出现sh interpreter not found错误的解决办法(图文详解)

    不多说,直接上干货! 问题详解 http://192.168.80.145:8099/#/notebook/2CSV2VT5S 相关博客是 Zeppelin的入门使用系列之使用Zeppelin运行sh ...

  4. GDI绘制图形的使用_验证码

    //创建GDI对象 Graphics g = this.CreateGraphics();// new Graphics(); //创建画笔对象 Pen pen = new Pen(Brushes.R ...

  5. 多线程----Thread类,Runnable接口,线程池,Callable接口,线程安全

    1概念 1.1进程 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 任务管理器中: 1.2线程 线程是进程中的一个执行单元 ...

  6. 中介者模式和php实现

    中介者模式: 中介者模式(Mediator Pattern)定义:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互.中介者模 ...

  7. Google Play发布App中遇到"多个APK:版本1未提供给任何设备配置使用。"问题的解决方法

    在google play上发布apk,当上传了apk文件,填写了相关的内容信息和介绍图片.图标后,出现“发布应用”始终灰色无法点击,查看原因显示如下问题: 其中支持的设备数量始终显示为0,怀疑是编译出 ...

  8. 系统有问题基本出在数据库上,web层无状态

    系统有问题基本出在数据库上,web层无状态.

  9. Exchange 用户邮箱导入/导出

    在第2部分中,我将向您介绍如何使用Exchange Server中提供的新cmdlet导入/导出数据,以及如何查看导入和导出的信息统计信息这样做. 走起! 将数据从PST文件导入到邮箱 现在是时候尝试 ...

  10. .NET easyUI tree树状结构

    简单的制作后台制作写一个json(string类型)格式 public partial class goodstype_type : System.Web.UI.Page { public strin ...