HDU-5433

来源:互联网 发布:蒲公英淘客软件 编辑:程序博客网 时间:2024/06/10 03:53

思路: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;}



0 0
原创粉丝点击