http://poj.org/problem?id=3009

一个搜索的题目:

大意就是一个冰球,在冰面上滑动,你打击一次,就沿一个反向滑动,知道碰到墙就会停下,而墙则会破碎。

求从起点到终点的最短的击打次数。

题目中 2代表起点,3是终点,1是墙,0则是光滑的。

 #include <stdio.h>

 int h,w,a[][],step,xs,ys,xe,ye,steps;

 int dic[][]={-,,,-,,,,};
void dfs(int x,int y)
{
if(step>) return;
for(int i=;i<;i++)
{
int n=x+dic[i][],m=y+dic[i][];
int stop=;
while(n<=h&&n>&&m<=w&&m>&&a[n][m]!=)
{
stop=;
if(n==xe&&m==ye)
if(step<steps) steps=step; //求最短的步数。
n+=dic[i][];
m+=dic[i][];
}
if(a[n][m]==&&stop)
{
step++;
a[n][m]=;
dfs(n-dic[i][],m-dic[i][]); //回溯。
step--;
a[n][m]=;
}
}
}
int main()
{
while(scanf("%d%d",&w,&h)&&w!=&&h!=)
{
for(int i=;i<=h;i++)
for(int j=;j<=w;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==)
{
xs=i;
ys=j;
}
if(a[i][j]==)
{
xe=i;
ye=j;
}
}
steps=;
step=;
dfs(xs,ys);
if(steps<=)printf("%d\n",steps);
else printf("-1\n");
}
return ;
}

POJ 3009的更多相关文章

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

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

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

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

  3. poj 3009 Curling 2.0

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

  4. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

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

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

  6. 【原创】poj ----- 3009 curling 2 解题报告

    题目地址: http://poj.org/problem?id=3009 题目内容: Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Tot ...

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

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

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

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

  9. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

随机推荐

  1. tamper参数

    "tamper/apostrophemask.py","tamper/equaltolike.py","tamper/greatest.py" ...

  2. Security Test Cases

    https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents Username Enumeration Vulner ...

  3. Hello World(本博客启程篇)

    Hello World 作为本博客第一篇日志,作为程序员,无论走到哪里,做什么事,必须先输出这句话. 一个想法 从今天3月份到现在一直在学技术,过程中坑的解决.知识的总结以及想法等都写到了" ...

  4. SQL Server 2012不支持从SQL Server 2000的备份进行还原

    错误: dbbackup failed: Unable to restore database 'ppt'Not valid backupThe database was backed up on a ...

  5. 包介绍 - UriTemplates (用于处理格式化Uri模板)

    UriTemplates 用于处理格式化Uri模板 PM> Install-Package Tavis.UriTemplates 设置Uri Path Segment [Fact] public ...

  6. 使用LIBSVM工具实现样本分类预测——MatLab

    准备工作: https://www.csie.ntu.edu.tw/~cjlin/libsvm/,下载LIBSVM:(LIBSVM工具相较于MATLAB自带的工具:1).支持多分类及回归(‘-s 0’ ...

  7. AlwaysOn可用性组测试环境安装与配置(二)--AlwaysOn配置(界面与T-SQL)

    四.AlwaysOn配置 1.开启AlwaysOn高可用性功能. 1.1.开启Server01的可用性组 1.2.需要重启服务:属于SQL server群集节点的服务,需要通过故障转移界面重启 1.3 ...

  8. Ruby中Block, Proc, 和Lambda

    Block Blocks就是存放一些可以被执行的代码的块,通常用do...end 或者 {}表示 例如: [1, 2, 3].each do |num| puts num end [1, 2, 3]. ...

  9. SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)

    SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...

  10. hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))

    Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...