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. Linux上常用命令整理(一)—— cat

    近几个月刚从windows上转过来,开始慢慢熟悉linux,先不撕比到底哪个更好,首先要怀着相互借鉴的精神去了解各个平台. Linux上做开发,除去使用文本编辑器做开发的大神之外,大家(包括我这种菜鸟 ...

  2. go实现set

    package main import ( "fmt" "sync" ) type object interface{} type Set struct { m ...

  3. 【转】说说Runnable与Callable

    说说Runnable与Callable   Callable接口:   Runnable接口: 相同点: 两者都是接口:(废话) 两者都可用来编写多线程程序: 两者都需要调用Thread.start( ...

  4. Java基础语法(自定义类、ArrayList集合)

    Java基础语法 今日内容介绍 u 自定义类 u ArrayList集合 第1章 引用数据类型(类) 1.1 引用数据类型分类 提到引用数据类型(类),其实我们对它并不陌生,如使用过的Scanner类 ...

  5. git从无到有建立一个仓库并上传文件

    第一步,创建仓库 登录自己的码云  第二步,本地操作 1.到你所要上传的文件夹中右键 选择git bash here 2.初始化项目 git init 3.连接远程仓库 刚才我们建立的时候的远程地址就 ...

  6. Android Doze模式源码分析

    科技的仿生学无处不在,给予我们启发.为了延长电池是使用寿命,google从蛇的冬眠中得到体会,那就是在某种情况下也让手机进入类冬眠的情况,从而引入了今天的主题,Doze模式,Doze中文是打盹儿,打盹 ...

  7. Android商城开发系列(一)——开篇

    最近在看尚硅谷的硅谷商城视频,想系统学习一下Android的商城开发流程,打算跟着视频的一步步做出一个商城,然后写博客总结记录一下整个商城的开发过程以及使用到的技术知识点,这个商城的最终效果如下图所示 ...

  8. Java VS Python 应该先学哪个?

    http://blog.segmentfault.com/hlcfan/1190000000361407 http://www.tuicool.com/articles/fqAzqi Java 和 P ...

  9. BZOJ 2539: [Ctsc2000]丘比特的烦恼

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 695  Solved: 260[Submit][Status][Discuss] Description ...

  10. 从照片网站pexels批量爬取照片

    调试中,未成功. from bs4 import BeautifulSoup import requests headers={ #'User-Agent':'Nokia6600/1.0 (3.42. ...