HDU-5433

思路:BFS+优先队列,用一个50*50*50的double数组判重。


注意!!!

题目里的x和y是反着的,也就是说,x对应n,y对应m。

例: n = 3,m = 6 时地图为:

**?***

******

******

“?”为(x=1,y=3)

浪费了一下午的时间。。。


#include <iostream>
#include <iomanip>
#include <queue>
using namespace std;

double ABS(double a){
	return a>0?a:(a*-1);
}

struct node{
	int x,y,k;
	double cost;
	bool operator < (const node &a) const{
		if(ABS(cost-a.cost)<0.0000001) return k>a.k;
		else return cost>a.cost;
	}
};

int n,m,k,sx,sy,ex,ey;
int value[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
char map[55][55];
double flag[55][55][55];

int ABS(int a){
	return a>0?a:(a*-1);
}

double BFS(){
	priority_queue<node> q;
	node temp1,temp2;
	temp1.x = sx;
	temp1.y = sy;
	temp1.k = 0;
	temp1.cost = 0;
	q.push(temp1);
	while(!q.empty()){
		temp1 = q.top();
		q.pop();
		if(temp1.x == ex && temp1.y == ey) return temp1.cost;
		for(int i=0;i<4;i++){
			temp2.x = temp1.x + value[i][0];
			temp2.y = temp1.y + value[i][1];
			temp2.k = temp1.k+1;
			if(temp2.k>=k) continue;
			if(temp2.x>=0 && temp2.x<n && temp2.y>=0 && temp2.y<m && map[temp2.x][temp2.y]!='#'){
				temp2.cost = temp1.cost+(double)ABS(int(map[temp2.x][temp2.y])-int(map[temp1.x][temp1.y]))/double(k-temp2.k+1);
				if(flag[temp2.x][temp2.y][temp2.k]>temp2.cost){
					flag[temp2.x][temp2.y][temp2.k] = temp2.cost;
					q.push(temp2);
				}
			}
		}
	}
	return -10;
}

int main(){
	int t,i;
	double temp;
	cin>>t;
	while(t--){
		cin>>n>>m>>k;
		for(i=0;i<n;i++) cin>>map[i];
		cin>>sx>>sy>>ex>>ey;
		if(k == 0){
			cout<<"No Answer"<<endl;
			continue;
		}
		sx--;
		sy--;
		ex--;
		ey--;
		for(i=0;i<=n;i++){
			for(int j=0;j<=m;j++){
				for(int l=0;l<=k;l++) flag[i][j][l] = 999999999;
			}
		}
		temp = BFS();
		if(temp<-1) cout<<"No Answer"<<endl;
		else cout<<fixed<<setprecision(2)<<temp<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值