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. 关系型数据库---MySQL---数据表

    1.在创建一个新的MySQL数据表时,可以为它设置一个类型: 2.MySQL支持多种数据表类型,有各自的特点和属性,最重要的3种类型: 1.1 MyISAM 1.2 InnoDB 1.1 可以把Inn ...

  2. Corn 表达式

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...

  3. c# 视频播放

    发表于: 2003-10-15 20:39:21 搞定了,嘿嘿!首先非常感谢zoujiaming在邮件中给我指了条路:用C#调用API搞定!!!使用的是mciSendString API函数主要参考了 ...

  4. webpack.config.js====CSS相关:postcss-loader加载器,自动添加前缀

    1. 在webpack中加载css需要先安装style-loader 和 css-loader cnpm install --save-dev style-loader css-loader 2. 在 ...

  5. 一、Postgresql的基本操作

    ---------------------------------------------------------------------------------------------------- ...

  6. js获取当前的年月日时分秒周期

    function timeNow(){ var date = new Date(); this.year = date.getFullYear(); this.month = date.getMont ...

  7. Linux下软件安装的四种方式

    一.源码安装 步骤: 下载,解压源码(常见的源码打包格式:.tar.gz/.tar.bz2); 可以直接下载源码再上传至linux服务器,或者在联网状态下,直接通过wget等命令获取源码安装包;源码解 ...

  8. Android RecyclerView使用GridLayoutManager导致间隙变大的问题

    我用recyclerView的时候设置LayoutManager为Grid,添加decoration为Grid,作为二级列表时,多次点击一级列表来跳转的时候,两张图之间的间隙在逐渐变大,后来发现是因为 ...

  9. jenkins只能同时构建2个Job怎么办?

    在jenkins 构建任务时,同时只能构建2个,如果两个没有job没有结束,构建第3个就会不执行: 提示: pending—Waiting for next available executor on ...

  10. elasticsearch查询方式

    1.query string a).GET /index/type/_search ===>>查询所有 b).GET /index/type/_search?q=filed:value&a ...