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
三维数组标记消耗的体力,如果if(vis[t2.x][t2.y][t2.t]>t2.v) 则继续入队。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }