hdu2433Xiao Ming climbing

来源:互联网 发布:淘宝新店采集器 编辑:程序博客网 时间:2024/06/09 19:36

Xiao Ming climbing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 593    Accepted Submission(s): 155


Problem Description
Due to the curse made by the devil,Xiao Ming is stranded on a mountain and can hardly escape.

This mountain is pretty strange that its underside is a rectangle which size is nm and every little part has a special coordinate(x,y)and a height H.

In order to escape from this mountain,Ming needs to find out the devil and beat it to clean up the curse.

At the biginning Xiao Ming has a fighting will k,if it turned to 0 Xiao Ming won't be able to fight with the devil,that means failure.

Ming can go to next position(N,E,S,W)from his current position that time every step,(abs(H1H2))/k 's physical power is spent,and then it cost 1 point of will.

Because of the devil's strong,Ming has to find a way cost least physical power to defeat the devil.

Can you help Xiao Ming to calculate the least physical power he need to consume.
 

Input
The first line of the input is a single integer T(T10), indicating the number of testcases.

Then T testcases follow.

The first line contains three integers n,m,k ,meaning as in the title(1n,m50,0k50).

Then the N × M matrix follows.

In matrix , the integer H meaning the height of (i,j),and '#' meaning barrier (Xiao Ming can't come to this) .

Then follow two lines,meaning Xiao Ming's coordinate(x1,y1) and the devil's coordinate(x2,y2),coordinates is not a barrier.
 

Output
For each testcase print a line ,if Xiao Ming can beat devil print the least physical power he need to consume,or output "NoAnswer" otherwise.

(The result should be rounded to 2 decimal places)
 

Sample Input
34 4 521342#232#2222211 13 34 4 721342#232#2222211 13 34 4 502#342#232#222#211 13 3
 

Sample Output
1.030.00No Answer
 

Source
BestCoder Round #55 ($)
 
只需要暴力BFS,去能到达终点的消耗的体力最小的
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;int n,m,k;char s[51][51];double cost[51][51];int dis[51][51];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int si,sj,ei,ej;double ans;int flag;struct node{    int x,y,energy;    double k;};void bfs(){    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++){            cost[i][j]=9999999;        }    ans=999999999;    flag=0;    cost[si][sj]=0;    queue<node>Q;    node b;    b.x=si;    b.y=sj;    b.k=0;    b.energy=k;    Q.push(b);    while(!Q.empty()){        node a=Q.front();        Q.pop();        if(a.x==ei&&a.y==ej&&a.energy>=1){            if(a.k<ans){                ans=a.k;                flag=1;            }            continue;        }        for(int k=0;k<4;k++){            int di=a.x+dir[k][0];            int dj=a.y+dir[k][1];            if(di<=0||dj<=0||di>n||dj>m||s[di][dj]=='#')                continue;            int z=abs(s[a.x][a.y]-s[di][dj]);            double x=a.k+(double)z/(double)a.energy;            if(x>=cost[di][dj]||a.energy<=1){                continue;            }            cost[di][dj]=x;            b.x=di;            b.y=dj;            b.k=x;            b.energy=a.energy-1;            Q.push(b);        }    }}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d%d%d",&n,&m,&k);        for(int i=1;i<=n;i++){            scanf("%s",s[i]+1);        }        scanf("%d%d%d%d",&si,&sj,&ei,&ej);        if(abs(ej-sj)+abs(ei-si)>=k){            printf("No Answer\n");            continue;        }        bfs();        if(flag==1)            printf("%.2lf\n",ans);        else{            printf("No Answer\n");        }    }    return 0;}


0 0
原创粉丝点击