【POJ 3009 Curling2.0 迷宫寻径 DFS】
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】的更多相关文章
- POJ 3009 Curling 2.0【带回溯DFS】
POJ 3009 题意: 给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物 ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- POJ 3009:Curling 2.0 推箱子
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14090 Accepted: 5887 Desc ...
- 2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意)
2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意) P1363 幻象迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 幻象迷宫可以认为是无限 ...
- POJ 1979 Red and Black【DFS】
标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...
随机推荐
- JAVA的类加载器,详细解释
JVM规范定义了两种类型的类装载器:启动内装载器(bootstrap)和用户自定义装载器(user-defined class loader). 一. ClassLoader基本概念 1.ClassL ...
- bzoj1632 [Usaco2007 Feb]Lilypad Pond
Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼.这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方 ...
- mysql 存储过程 事务处理
BEGIN ; ; START TRANSACTION; #这边放sql语句,涉及到的表必须都为InnoDB THEN ROLLBACK; ELSE COMMIT; END IF; END
- UVA_Cubic Eight-Puzzle UVA 1604
Let's play a puzzle using eight cubes placed on a 3 x 3 board leaving one empty square.Faces of cube ...
- 剑指offer-面试题.二叉树的镜像
题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像. 二叉树节点定义如下: strcut BinaryTreeNode { int val; strcut BinaryTreeNode* m_ ...
- AutoResetEvent与ManualResetEvent区别
本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...
- scrollTo和scrollTo.js
最近做一个项目前端要用到scrollTo和滚动视觉差.顺便把两个东西拿出来温习一下. HTML DOM里面定义了scrollTo方法,用法:scrollTo(xpos,ypos),把内容滚动到当前的指 ...
- 执行此安装程序之前,必须安装 32 位 Windows 映像处理组件(WIC)解决的方法
我们在Windows Service 2003上安装 Microsoft .NET Framework4.0时常常出现以下的报错 执行此安装程序之前,必须安装 32 位 Windows 映像处理组件( ...
- PHP安装mcrypt.so报错 mcrypt.h not found 的解决办法
报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了很多,包括自带的 yum install libm ...
- Bestcoder HDU5059 Help him 字符串处理
Help him Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...