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

模拟冰壶的移动,给出到达终点的最少投掷次数(不可达时为-1)。

具体移动规则如下:

每次选四个方向之一,沿此方向一直前进,直到撞到block或出界或抵达目标位置。

  如果撞到block,冰壶停在block的前一个位置,block消失,此时可改变冰壶的移动方向(重新投掷一次);

  如果出界,则这条移动路径以失败结束;

  如果抵达目标位置,则记录这条移动路径一共投掷的次数。

投掷次数不超过10次的为成功路径。

如果存在成功路径,输出最少的投掷次数;不存在则输出-1。

代码如下,由于数据量小,可用DFS得到所有解然后取最小值;注意回溯时还原修改过的block:

 #include <cstdio>
#include <cstring>
using namespace std; const int MAX_N = ; int n, m;
int G[MAX_N][MAX_N];
int dx[]={, , ,-}, dy[]={, -, , };
int sx, sy, gx, gy;
int min_ans; bool inside(int x, int y){
if(x< || x>=n || y< || y>=m) return false;
return true;
} void dfs(int x, int y, int cnt){
if(cnt > ) return ;
for(int i=; i<; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(!inside(x, y)) continue; //出界
if(G[nx][ny] == ) continue; //block
else{
while(inside(nx, ny) && G[nx][ny] != && G[nx][ny] != ){
nx += dx[i];
ny += dy[i]; //沿此方向走到block为止
}
if(!inside(nx, ny)) continue; //此方向最终出界
if(G[nx][ny] == ){
min_ans = cnt<min_ans ? cnt : min_ans;
continue; //此方向中途命中
} G[nx][ny] = ; //block消失
nx -= dx[i];
ny -= dy[i]; //抵达此方向最后一个合法位置,block的前一个
//printf("%d %d\n", dx[i], dy[i]);
dfs(nx, ny, cnt+);
nx += dx[i];
ny += dy[i];
G[nx][ny] = ; //还原,回溯
}
}
return ;
} int main()
{
freopen("3009.txt", "r", stdin);
while(scanf("%d%d", &m, &n) != EOF){
if(m== && n==) break;
for(int i=; i<n; i++){
for(int j=; j<m; j++){
scanf("%d", &G[i][j]);
if(G[i][j] == ){
sx = i;
sy = j;
}else if(G[i][j] == ){
gx = i;
gy = j;
}
}
} min_ans = ; //任意大于10的值
dfs(sx, sy, ); //投掷第一次
if(min_ans > ) min_ans = -;
printf("%d\n", min_ans);
}
return ;
}

注意记这一类搜索的框架,以及联系算法课学过的回溯法的基本概念,如约束条件、活结点等。

【POJ 3009 Curling2.0 迷宫寻径 DFS】的更多相关文章

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

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

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

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

  3. POJ 3009:Curling 2.0 推箱子

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14090   Accepted: 5887 Desc ...

  4. 2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意)

    2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意) P1363 幻象迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 幻象迷宫可以认为是无限 ...

  5. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  6. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

  7. POJ 1745 【0/1 背包】

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

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

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

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

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

随机推荐

  1. 【Xamarin挖墙脚系列:开始使用Xamari4.0系列产品开发IOS】

    一直沉默在Xamarin3.0系列版本上,升级到4.0之后,感觉有些变化.还得适应下. 1 build.host  代理消失了,成了SSH客户端登录.所以,Mac设备需要打开运行远程登录. 2 在Wi ...

  2. ubuntu下编译源码级QT

    注意必须好好看官方文档: http://qt-project.org/doc/qt-5/linux.html 包括编译Qt库依赖的包等等. 编译过程中发现以下错误: All the OpenGL fu ...

  3. 怎样在Github参与一个开源项目

    转载:http://www.csdn.net/article/2014-04-14/2819293-Contributing-to-Open-Source-on-GitHub 最近一年开源项目特别的热 ...

  4. 杭电1010(dfs + 奇偶剪枝)

    题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...

  5. 【转】android 电容屏(三):驱动调试之驱动程序分析篇

    关键词:android  电容屏 tp 工作队列 中断 坐点计算  电容屏主要参数平台信息:内核:linux2.6/linux3.0系统:android/android4.0  平台:S5PV310( ...

  6. jQuery支持移动Mobile的DOM元素移动和缩放插件

    jQuery Panzoom是一款很有用的HTML DOM元素平移和缩放jQuery和CSS3插件. Panzoom利用CSS transforms 和 matrix函数来为浏览器进行硬件(GPU)加 ...

  7. 银行综合储蓄业务系统,水平为学了一年C语言

    银行综合储蓄业务系统 #include <stdio.h> #include<string.h> int acccunt = 0; char name[10],pw[10]; ...

  8. Ubuntu下嵌入式Qt开发环境配置全攻略

    http://qpcwth.blog.163.com/blog/static/20993024620139151424822/ 在安装的过称中,出现一些问题,注意试想: 1.本次开发环境的配置,是基于 ...

  9. UVA 12125 March of the Penguins

    题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...

  10. 部署sharepointform验证

    1  iis sharepoint v4 提供程序2  web service 验证提供程序3 创建web应用程序(选择基于身份验证模式,配置提供程序)4 创建网站集(空)5 创建网站集