poj 3009 深搜
来源:互联网 发布:软件后续技术支持方案 编辑:程序博客网 时间:2024/05/20 19:29
如题:http://poj.org/problem?id=3009
题目给出一个迷宫,2是开始,3代表结束,规定了走法。可以沿一个方向一直走,碰到石头(1)后停下,石头消失,然后再选择一个方向走。要注意的是,如果下一步可以走的格子是石头,是不可以走那个方向,并且移除石头的。
让求出最短的步数。
一开始看到最短步数想到广搜,但是每搜索一个状态时,地图会发生泰国变化,很难去处理,题目还说明超过10步就算输,所以直接深搜也不会超时了。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 23
int map[N][N];
int w,h;
int dir[][2]={0,1,0,-1,1,0,-1,0};
int s;
void dfs(int x,int y,int step)
{
int i;
if(step>10)
return;
for(i=0;i<4;i++)
{
int ok=0;
int tx=x+dir[i][0];
int ty=y+dir[i][1];
while(map[tx][ty]!=1&&tx>=0&&ty>=0&&tx<h&&ty<w)
{
ok=1; //可以搜索
if(map[tx][ty]==3)
{
if(step<s)
s=step;
}
tx+=dir[i][0];
ty+=dir[i][1];
}
if(map[tx][ty]==1&&ok)
{
map[tx][ty]=0;
dfs(tx-dir[i][0],ty-dir[i][1],step+1);
//printf("%d %d %d\n",tx,ty,step);
map[tx][ty]=1;
}
}
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
while(scanf("%d%d",&w,&h)&&w&&h)
{
s=10000000;
int i,j;
int xs,ys;
memset(map,0,sizeof(map));
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
{ xs=i;ys=j;}
}
dfs(xs,ys,1);
if(s<=10)
printf("%d\n",s);
else
printf("-1\n");
}
return 0;
}
- poj 3009 深搜
- POJ 3009 深搜
- poj 3009 深搜
- poj 3009 深搜
- POJ 3009 深搜+回溯
- [Poj 3009] Curling 2.0 深搜
- POJ 3009
- poj 3009
- POJ 3009
- POJ 3009
- POJ-3009
- POJ 3009
- POJ 3009
- poj-3009
- poj--3009
- poj 3009
- POJ 3009
- poj-3009
- java基础---JAVA中线程的作用
- 系统学习数字图像处理之频域滤波
- oracle11g默认创建用户的密码有效期为180天的解决方法
- onethink 的基类 Controller.class.php
- JavaScript中一般变量、函数、函数参数重名时的覆盖优先级
- poj 3009 深搜
- 图像缩放--OpenCV cvResize函数--最近邻插值---双线性插值--基本原理
- 链表各种接口的递归写法
- 封装jQuery Validate扩展验证方法
- java基础---java中的IO流
- Q2:关于数组的解包错误 fatal error: Can't unwrap Optional
- 两道和Tarjan有关的图论题
- uva11542 Square(异或方程组)
- 第一部分 基础篇 第一章 走进MongoDB