hdu 1008 Dogs 广度优先搜索

10 篇文章 0 订阅

Dogs

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

Input

The input is divided into blocks. The first line in each block contains two integers: the length m of the farmland, the width n of the farmland (m, n ≤ 1000). The next lines contain m rows and each row have n letters, with 'X' stands for the lattices of house, and '.' stands for the empty land. The following two lines is the start and end places' coordinates, we guarantee that they are located at 'X'. There will be a blank line between every test case. The block where both two numbers in the first line are equal to zero denotes the end of the input.

Output

For each case you should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig.

Sample Input

6 6
..X...
XXX.X.
....X.
X.....
X.....
X.X...
3 5
6 3

0 0

Sample Output

3

Hint

Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).

Source

2009 Multi-University Training Contest 1 - Host by TJU
这道广度优先搜索的迷宫题,首先因为自己的理解错误以为是八个方向,后来才找到是四个方向
这道题是第一次做迷宫类的题目或者说是BFS一开始没有按优先队列的思想去做,只按一般的队列
来做的不过最后还是不能过只有按这种优先队列来做才AC了,C++的队列函数:

队列:#include<queue>

申请队列:queue<type>q;

判队空:q.empty();

获取队头元素:q.front()

入队:q.push();

出队:q.pop();


还有就是关于方向数组这次明白了他的准确含义
四个方向每个方向只有两种状态他们横坐标是相同的,变的只有纵坐标
dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int x0,y0,x1,y1;
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int map[1003][1003];
int visit[1003][1003];

typedef struct queue1
{
	int x,y,sum;
	bool operator <(const queue1& a)const{
		return sum>a.sum;
	}
};
int bfs()
{
	priority_queue<queue1> q;
	queue1 qq,ding;
	qq.x=0;
	qq.y=0;
	qq.sum=0;
	qq.x = x0;
	qq.y = y0;
	q.push(qq);
	int w;
	while(!q.empty())
	{
		qq=q.top();
		q.pop();
	if(qq.x==x1&&qq.y==y1)
		return qq.sum;

			for(int i=0;i<4;i++)		
			{	
				if(visit[qq.x+dir[i][0]][qq.y+dir[i][1]]==0&&map[qq.x+dir[i][0]][qq.y+dir[i][1]]>=0)
				{
					visit[qq.x+dir[i][0]][qq.y+dir[i][1]]=-1;
					ding.x=qq.x+dir[i][0];
					ding.y=qq.y+dir[i][1];
					ding.sum=qq.sum+map[qq.x+dir[i][0]][qq.y+dir[i][1]];
				
					q.push(ding);
				
				}
			}	
	}
	
}
int main()
{
	memset(map,-1,sizeof(map));
	memset(visit,0,sizeof(visit));
	int C=0,R=0,result;
	char ch1[1009];
	while(scanf("%d%d",&C,&R)&&C!=0&&R!=0)
	{	
		memset(map,-1,sizeof(map));
		memset(visit,0,sizeof(visit));
		x0=0,x1=0,y0=0,y1=0,result=0;
		for(int i=1;i<=C;i++)
		{
			scanf("%s",ch1+1);//这里的技巧可以省去边界的判断
			for(int j=1;j<=R;j++)//memset(&,1,sizeof())初始化的值会是一个相当大得数 {	
				if(ch1[j]=='.')//-1,0都不会
				{
					map[i][j]=1;
				}
				if(ch1[j]=='X')
				{
					map[i][j]=0;
				}
			}	
		}	
		cin>>x0>>y0>>x1>>y1;
		result=bfs();
		if(result==0)
			cout<<result<<endl;
		else
			cout<<result<<endl;		
	}	
	
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值