POJ_3009——冰球,IDS迭代加深搜索
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.
- The stone hits a block (Fig. 2(b), (c)).
- 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.
0 vacant square 1 block 2 start position 3 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
#include <cstdio>
int depth;
int w, h, s_x, s_y,
map[][],
dir[][]={,,,-,,,-,}; bool IDS(int d,int x,int y)
{
if(depth == d) //如果搜索深度达到一定,则停止这条搜索树
{
return false;
} for(int i=;i<;i++)
{
int dx = x + dir[i][];
int dy = y + dir[i][]; if(map[dx][dy] == ) //如果下一步直接就撞墙了,中间没有空格,此路就不通
continue; while(!map[dx][dy]) //如果是通路就一直走下去,直到墙壁或者终点
{
dx += dir[i][];
dy += dir[i][];
}
if(dx>= && dx<h && dy>= && dy<w) //如果没有走出地图范围,走出就不用递归了
{
if(map[dx][dy] == ) //撞到的是墙
{
map[dx][dy] = ; //把墙撞碎
if(IDS(d+, dx-dir[i][], dy-dir[i][]))//深度递增,往回走一步
{
return true;
}
map[dx][dy] = ; //墙恢复
}
else //撞到的是终点
{
return true;
}
}
}
return false;
} int main()
{
while(~scanf("%d%d",&w,&h), (w||h))
{
for(int i=;i<h;i++)
{
for(int j=;j<w;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==)
{
s_x = i;
s_y = j;
}
}
}
map[s_x][s_y]=; //起点无用,直接覆盖掉
depth = ; //记录搜索深度
while(depth<= && !IDS(, s_x, s_y))
{
depth++;
}
printf(depth==?"-1\n":"%d\n",depth);
}
return ;
}
POJ_3009——冰球,IDS迭代加深搜索的更多相关文章
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
随机推荐
- 【移动开发】WIFI热点通信(一)
之前调查过Android中WIFI模块的使用,也写过两篇学习总结的文章(http://smallwoniu.blog.51cto.com/3911954/1334951),后来发现DEMO里面还是有许 ...
- linux下U盘的读取
1.虚拟机vmware右下角,找到大容量存储设备图标,右键->connect(disconect from host):使U盘连接到虚拟机中来. 2.打开终端:fdisk -l [root@lo ...
- 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】
2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...
- MyEclipse设置默认的文档注释
- Dev GridControl 按条件合并相同单元格
Dev 默认的合并方式,只要(垂直方向)相邻两个单元格的值相同都会进行合并,这种方式并不是最优的,所以需要在进行合并的过程中进行判断. 方式如下: 1:先设置需要合并的列为允许合并 OptionsVi ...
- C#Css/Js静态文件压缩--Yui.Compressor.Net
一.Asp.Net 自带静态文件压缩工具包 Microsoft.AspNet.Web.Optimization http://www.nuget.org/packages/Microsoft.AspN ...
- Swift - 41 - swift1.2新特性(1)
更简洁的if-let import UIKit func attack(name: String, enemyName: String, weapon: String) { print("\ ...
- 使用 ADD-ON SDK 开发 基于 Html JQuery 和 CSS 的 firefox 插件入门教程1: 创建一个简单的 Add-on
[本文转载自http://sixpoint.me/942/implementing-simple-addon/] 实现一个简单的插件 教程的这个部分带你使用 SDK 来实现, 运行并打包一个插件. 这 ...
- CURL请求接口
private function http_curl($url,$data=null){ //1.初始化,创建一个新cURL资源 $ch = curl_init(); ...
- 关于 typings install 的使用
typings 用来管理.d.ts的文件,这种文件是js的一种接口描述,原因是有很多js库并没有typescript的版本. 微软给出一种描述文件,让IDE识别各种js库的代码提示以及类型检查等. 写 ...