hdu2822(会双搜后再写次)

/*
分析:
    跑的太慢了,虽然1a了。
    等会了双搜后再写一次。


                           2012-07-19
*/

    一直都很好奇,为什么这篇文章点击量这么高?明明只是运气好,用一个bfs水过了的。。

下面是随便写的一点儿对双搜的很基础的解释、属牛的请主动忽略囧~~。双搜的话,对于这

个题、可以写两个bfs同时进行么,一个从s(源点)开始、另一个从e(终点)开始,两个

bfs域的第一次交汇的时候,就得到了一个最佳ans。

    举个例子:a、b两点间距为d1,如果以a为圆心、开始让一个圆变大(这里就说成圆吧,

因为一般的、n*n的矩阵中、每一步的距离为1的bfs(既边为1的最短路)中,bfs是以曼哈

顿距离扩展的,这里说成圆只是为了更容易理解)、直到圆的边蹭到了b点结束,这个圆的

面积为PI*d1*d1;

    但是,如果同时以a点、b点为中心,以同样的速度同时扩展俩圆,那么当两个圆蹭着的

时候,两个圆的面积总和为2*PI*(d1/2)*(d1/2);

    如果只是这样看的话,那么后一个式子是前一个的大小的一半,所以就省了时间。。。

    随手写的简单解释,写的不好了随意砸砖囧。。。










#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;

struct node
{
	int x,y;
	int step;
	friend bool operator<(node n1,node n2)
	{
		return n2.step<n1.step;
	}
};
int map[1011][1011];
int dir[4][2]={0,1, 0,-1, 1,0, -1,0};
int row,lie;
int x_s,y_s;
int x_e,y_e;

int judge(int x,int y)
{
	if(x<0 || x>=row || y<0 || y>=lie)	return 1;
	if(map[x][y]==1)	return 1;
	return 0;
}
int BFS()
{
	priority_queue<node>q;
	node cur,next;
	int i;

	cur.x=x_s;
	cur.y=y_s;
	cur.step=0;
	map[cur.x][cur.y]=1;

	q.push(cur);
	while(!q.empty())
	{
		cur=q.top();
		q.pop();
		if(cur.x==x_e && cur.y==y_e)	return cur.step;
		for(i=0;i<4;i++)
		{
			next.x=cur.x+dir[i][0];
			next.y=cur.y+dir[i][1];


			if(judge(next.x,next.y))	continue;
			if(map[next.x][next.y]==0)	next.step=cur.step;
			else						next.step=cur.step+1;
			map[next.x][next.y]=1;
			q.push(next);
		}
	}
	return -1;
}

int main()
{
	int i,l;
	int ans;
	char str[1011];
	while(scanf("%d%d",&row,&lie),row||lie)
	{
		if(row==0 && lie==0)	break;
		if(row==0 || lie==0)	{printf("0\n");continue;}

		for(i=0;i<row;i++)
		{
			scanf("%s",str);
			for(l=0;str[l];l++)
			{
				if(str[l]=='X')	map[i][l]=0;
				else			map[i][l]=-1;
			}
		}

		scanf("%d%d",&x_s,&y_s);
		x_s--;
		y_s--;
		scanf("%d%d",&x_e,&y_e);
		x_e--;
		y_e--;

		ans=BFS();
		printf("%d\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值