您的位置:首页 > 其它

hdu 1728 逃离迷宫

2012-10-12 21:59 399 查看
题目链接:

题目大意:迷宫之类的题大多数都能使用BFS或者DFS吧,不过这道题比较多陷阱和技巧,首先,每一种走法经过的弯可能是不同的,虽然都是经过同一个点,但是到达这个点时经过的弯路可能就不同了。应该有点DP的味道吧,BFS+DP可以算出来。这需要开多一个二维数组来保存这个点最小的转弯数,到达终点后判断这个点的转弯数是否小于或等于给予的限定就可以了。这应该是属于UCS查找吧,是BFS的升级版,就是加了个进队列的条件。

代码:

#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<queue>
using namespace std;
#define INF 100000000
const int maxn=110;
int q[4][2]={-1,0,1,0,0,-1,0,1};
bool flag[maxn][maxn];
int n,m,k,x1,y,x2,y2;
struct node
{
int x,y,step;
};
char s[maxn][maxn];
int bfs()
{
int i,j;
memset(flag,false,sizeof(flag));
queue<node>Q;
node S;
S.x=x1-1;
S.y=y-1;
S.step=-1;
flag[S.x][S.y]=true;
Q.push(S);
while(!Q.empty())
{
node go,st;
go=Q.front();
Q.pop();
if(go.x==x2-1&&go.y==y2-1&&go.step<=k)
return 1;
for(i=0;i<4;i++)
{
st.x=q[i][0]+go.x;
st.y=q[i][1]+go.y;
st.step=go.step+1;
if(st.step>k)
continue;
while(st.x>=0&&st.x<m&&st.y>=0&&st.y<n&&s[st.x][st.y]=='.')
{
if(!flag[st.x][st.y])
{
flag[st.x][st.y]=true;
Q.push(st);
}
st.x=st.x+q[i][0];
st.y=st.y+q[i][1];
if(st.x==x2-1&&st.y==y2-1&&st.step<=k)
return 1;
}
}
}
return 0;
}

int main(void)
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
scanf("%s",s[i]);
scanf("%d%d%d%d%d",&k,&y,&x1,&y2,&x2);
if(x1==x2&&y==y2)
{
printf("yes\n");
continue;
}
bfs();
if(bfs()==1)
printf("yes\n");
else
printf("no\n");
}
system("Pause");
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: