hdu1728-逃离迷宫

本文记录了解决杭电HDU1728题目的过程,主要介绍了如何运用广度优先搜索(BFS)策略来解决迷宫问题,通过实例详细阐述了解题思路和步骤。
摘要由CSDN通过智能技术生成

逃离迷宫

// File Name: hdu1728bfs.cpp
// Author: rudolf
// Created Time: 2013年04月23日 星期二 13时18分54秒
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
int k,x1,x2,y1,y2,m,n,mark[105][105];
struct node
{
	int x,y;
	node operator + (node  a)
	{
		node t;
		t.x=x+a.x;
		t.y=y+a.y;
		return t;
	}
}p[5],cur,que[1000000];
bool fix(node cur)
{
	if ((cur.x>=0)&&(cur.x<n)&&(cur.y>=0)&&(cur.y<m)) 
		return 1;
	return 0;
}
char g[105][105];
int bfs()
{
	int i;
	queue <node>Q;
	node next;
	cur.x=x1;
	cur.y=y1;
	Q.push(cur);
	while (!Q.empty())
	{
		cur=Q.front();
		Q.pop();
		for (i=0;i<4;++i)
		{
			next=cur+p[i];
			while (fix(next)&&(g[next.y][next.x]!='*'))
			{
				if (mark[next.y][next.x]==-1)
				{
					mark[next.y][next.x]=mark[cur.y][cur.x]+1;
					if ( next.x==x2 && next.y==y2 && mark[next.y][next.x]<=k ) 
						return 1;
					Q.push(next);
				}
				next=next+p[i];
			}
		}
	}
    return 0;
}
int main()
{
    int cas,i;
	p[1].x=1;p[1].y=0;
	p[2].x=-1;p[2].y=0;
	p[3].x=0;p[3].y=1;
	p[0].x=0;p[0].y=-1;
    scanf("%d",&cas);
    while (cas--)
    {
		memset(mark,-1,sizeof(mark));
        scanf("%d%d",&m,&n);
        gets(g[1]);
        for (i=0;i<m;i++)
            gets(g[i]);
        scanf("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2);
		x1--;x2--;y1--;y2--;
		if ((x1==x2)&&(y1==y2)) 
			printf("yes\n");
		else
			if(bfs()) 
				printf("yes\n");
			else 
				printf("no\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值