hdu 5433 Xiao Ming climbing(bfs+三维标记)

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 n∗m 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(H1−H2))/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(T≤10), indicating the number of testcases. 

Then T testcases follow.

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

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

3
4 4 5
2134
2#23
2#22
2221
1 1
3 3
4 4 7
2134
2#23
2#22
2221
1 1
3 3
4 4 50
2#34
2#23
2#22
2#21
1 1
3 3

Sample Output

1.03
0.00
No Answer

Source

BestCoder Round #55 ($)

三维数组标记消耗的体力,如果if(vis[t2.x][t2.y][t2.t]>t2.v) 则继续入队。

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 using namespace std;
 15 #define ll long long
 16 #define eps 1e-10
 17 #define MOD 1000000007
 18 #define N 56
 19 #define inf 1e12
 20 int n,m,k;
 21 char mp[N][N];
 22 int sgn(double e)
 23 {
 24     if(fabs(e)<eps)return 0;
 25     return e>0?1:-1;
 26 }
 27 struct Node{
 28     int x,y;
 29     int t;
 30     double v;
 31 }st,ed;
 32 double vis[N][N][N];
 33 int M[N][N];
 34 int dirx[]={1,0,-1,0};
 35 int diry[]={0,1,0,-1};
 36 void bfs(){
 37     queue<Node>q;
 38     st.v=0;
 39     q.push(st);
 40     Node t1,t2;
 41     vis[st.x][st.y][st.t]=0;
 42     while(!q.empty()){
 43         t1=q.front();
 44         q.pop();
 45         if(t1.t<=0) continue;
 46         for(int i=0;i<4;i++){
 47             t2=t1;
 48             t2.x=t1.x+dirx[i];
 49             t2.y=t1.y+diry[i];
 50             if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue;
 51             if(M[t2.x][t2.y] == -1) continue;
 52
 53
 54             int num1=M[t2.x][t2.y];
 55             int num2=M[t1.x][t1.y];
 56             t2.v+=(abs(num1-num2)*1.0)/(t2.t);
 57             t2.t--;
 58             if(sgn(vis[t2.x][t2.y][t2.t]-t2.v)>0){
 59                 vis[t2.x][t2.y][t2.t]=t2.v;
 60                 q.push(t2);
 61             }
 62         }
 63     }
 64 }
 65 int main()
 66 {
 67     int t;
 68     scanf("%d",&t);
 69     while(t--){
 70         scanf("%d%d%d",&n,&m,&k);
 71         for(int i=0;i<n;i++){
 72             scanf("%s",mp[i]);
 73             for(int j=0; j<m; j++)
 74              if(mp[i][j]==‘#‘)M[i][j]=-1;
 75               else M[i][j]=mp[i][j]-‘0‘;
 76         }
 77
 78
 79         scanf("%d%d%d%d",&st.x,&st.y,&ed.x,&ed.y);
 80         if(k<=0){
 81             printf("No Answer\n"); continue;
 82         }
 83         st.x--;
 84         st.y--;
 85         ed.x--;
 86         ed.y--;
 87         st.t=k;
 88         st.v=0;
 89         //memset(vis,inf,sizeof(vis));
 90         for(int i=0;i<=n;i++){
 91             for(int j=0;j<=m;j++){
 92                 for(int r=0;r<=k;r++){
 93                     vis[i][j][r]=inf;
 94                 }
 95             }
 96         }
 97         bfs();
 98         double ans=vis[ed.x][ed.y][1];
 99         for(int i=1;i<=k; i++)
100          ans=min(ans,vis[ed.x][ed.y][i]);
101
102          if(sgn(ans-inf)>=0){
103              printf("No Answer\n");
104          }
105          else{
106              printf("%.2lf\n",ans);
107          }
108     }
109     return 0;
110 }

时间: 2024-08-05 19:37:35

hdu 5433 Xiao Ming climbing(bfs+三维标记)的相关文章

hdu 5433 Xiao Ming climbing (BFS)

DFS超时,当时头脑混乱不会改成BFS.过了两天,思路清晰,一次AC,真爽. 思路:用dp[x][y][p]表示走到(x,y)且剩余斗志为p的最少体力,bfs的过程中,只有满足边界条件并且下一步算出来的值要小于dp[next.x][next.y][next.p]时才把这个点放入队列. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include &

hdu 5433 Xiao Ming climbing

题意:一张图,给出起点和终点,给一个fighting值,每走一步fighting消耗1,并且消耗(abs(H1−H2))/fighting 的 physical power.求起点到终点消耗最小的physical pow. 解:题目上有一句"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 fai

hdu 5433 Xiao Ming climbing(用dp代替搜索)

题意:有n*m的矩阵,人的初始力量值为k,矩阵上有数字表示高度,相邻点高度差的绝对值除k,为力量消耗量,求给定起点终点消耗力量的最小值: 思路:dp[k][x][y]表示第k步走到(x,y)时的最小力量消耗,建立递推: #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define inf 0x3f3f3f3f int t,

HDU 4349 Xiao Ming&#39;s Hope (Lucas)

题意:给定一个 n,问你在 C(n, 0) - C(n , n) 中有多少个奇数. 析:Lucas定理,C(b[i], a[i]),只要不为0,那么就是奇数,然后b[i],是固定的,也就是说a[i] 只有 b[i]+1种情况.最后乘起来就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

HDU 4349 Xiao Ming&#39;s Hope(数学题,Lucas定理)

解题思路: 深入理解lucas定理. Lucas定理:把n写成p进制a[n]a[n-1]a[n-2]...a[0],把m写成p进制b[n]b[n-1]b[n-2]...b[0],则C(n,m)与C(a[n],b[n])*C(a[n-1],b[n-1])*C(a[n-2],b[-2])*....*C(a[0],b[0])模p同余. 这题p为2,所以a[i]和b[i]为0或者1.又因为C(1,0), C(1,1), 即当n等于1时,结果才等于1. 所以写出n的二进制,m从0遍历到n,每一次遍历把m写

HDU 4349 Xiao Ming&#39;s Hope

很无语的一个题. 反正我后来看题解完全不是一个道上的. 要用什么组合数学的lucas定理. 表示自己就推了前面几个数然后找找规律. C(n, m) 就是 组合n取m: (m!(n-m!)/n!) 如果n==11 : C(11,0):C(11,1):C(11,2):C(11,3):C(11,4):C(11,5): 分别为 (1/1); (1 / 11) ; (11*10 / 2*1)  ;   (11*10*9 / 3*2*1); (11*10*9*8 / 4*3*2*1) ;  (11*10*9

hdu 4349 Xiao Ming&#39;s Hope 规律

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to

HDU 4349 Xiao Ming&#39;s Hope lucas定理

Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2