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.

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迭代加深搜索的更多相关文章

  1. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  2. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  3. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  4. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  5. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  8. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...

  9. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

随机推荐

  1. Struts2框架具体解释

    在Struts2的Model-View-Controller模式实现下面五个核心组件: 动作-Actions 拦截器-Interceptors 值栈/OGNL 结果/结果类型 视图技术 Struts ...

  2. [置顶] 【SQL】查询重复人名的次数并列出

    select count(姓名) as 重复次数,姓名from 某表 group by 姓名order by 重复次数 asc 首先,group by 姓名,可以将所有相同姓名的项集合在一起.然后,c ...

  3. 获取Android自己写好了的apk以及反编译

    今天,我们先说一下,获取Android自带的apk以及反编译它们来学习Android工程师是怎样写的,今天我们就以拿到Android自带的短信管理器的apk为例子 你可能有疑问,为什么要那么麻烦,从系 ...

  4. [Angular 2] ngrx/store

    @ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJ ...

  5. [Javascript] Refactoring: Polymorphic Functions

    if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...

  6. [CSS3] CSS Media Queries

    Using CSS media queries allows you to design responsive layout in your web apps and website. We will ...

  7. lamp安装指南(转)

    主要软件包, 1. httpd-2.2.6.tar.gz 2. mysql-5.0.45-linux-i686-glibc23.tar.gz ( 这个版本是已编译好的压缩包,解压后稍做配置即可使用 ) ...

  8. 线段树---HDU2795Billboard

    这道题跟第二个题差不多,求单点的最大值. 题目大意:有个高和宽分别为h, w的广告牌, 这个广告牌分成高为 1 的长条, 每条分别能贴长度为wi长度的广告, 输入的n为广告的条数,广告优先贴在最上边和 ...

  9. EventBus 事件总线 案例

    简介 地址:https://github.com/greenrobot/EventBus EventBus是一个[发布 / 订阅]的事件总线.简单点说,就是两人[约定]好怎么通信,一人发布消息,另外一 ...

  10. CDN的全称是Content Delivery Network,即内容分发网络

    CDN的全称是Content Delivery Network,即内容分发网络 http://baike.baidu.com/link?url=Wd-IGGgslfJemdpuT3Y0BUi88RPQ ...