HDU 5433 Xiao Ming climbing

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5433


题面:

Xiao Ming climbing

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


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
  
  
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

题目大意:

    给定起点和终点。有一个意念值k,其实就是可以走k步。每走一步都会产生体力消耗abs(a1-a2)/x,a1,a2分别为两个位置的高度,x为当前剩余意念值。求在限定步数内,从起点到终点到的最小体力消耗。


解题:

    之前的代码是错误的,多谢一抹忧伤|指出。深搜理论上不是不可以,但是还需要加一维代表步数,复杂度可能会挺高。故正解是,步数少,且代价低的优先队列配合BFS。


坑点:

    当k=0的时候,不管起点和终点是否重合,要直接输出No Answer,好像题面有提及,意念值为0,就意味着失败。


错误代码:


错误原因:

    并不是新到达一点时,代价低,就更新该点,可能存在代价低,步数多不能到达的情况,而代价高,步数少,却能到达。

#include<iostream>
using namespace std;
char map[55][55];
double dp[55][55];
int n,m,k,t,sx,sy,dx,dy,dir[4][2]={-1,0,0,1,1,0,0,-1};
bool flag;
void dfs(int x,int y,int step,double cost)
{
    int tx,ty;
    double tcost;
    if(cost>=dp[x][y])return;
    else
    {
        dp[x][y]=cost;
        if(x==dx&&y==dy)
        {
            flag=true;
            return;
        }
        if(step==0)return;
        for(int i=0;i<4;i++)
        {
            tx=x+dir[i][0];
            ty=y+dir[i][1];
            if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&map[tx][ty]!='#')
            {
                tcost=cost+1.0*abs(map[x][y]-map[tx][ty])/step;
                dfs(tx,ty,step-1,tcost);
            }
        }        
    }
}
void init()
{
    flag=false;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            dp[i][j]=1e9;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=m;j++)
              scanf("%c",&map[i][j]);
        }
        scanf("%d%d%d%d",&sx,&sy,&dx,&dy);
        if(k==0)
		{
			printf("No Answer\n");
			continue;
		} 
        init();
        dfs(sx,sy,k,0.0);
        if(flag)
        {
            printf("%.2lf\n",dp[dx][dy]);
        }
        else
          printf("No Answer\n");
    }
    return 0;
} 


修改代码:

#include <iostream>
#include <queue>
using namespace std;
char map[55][55];
double dp[55][55][55];
int n,m,k,t,sx,sy,dx,dy,dir[4][2]={-1,0,0,1,1,0,0,-1};
bool flag;
struct node
{
	int x,y,step;
	double cost;
	bool operator<(const node &a)const
	{
       if(step!=a.step)
		   return step>a.step;
	   else
		   return cost>a.cost;
	}
};
priority_queue <node> qe;
void bfs()
{
   while(!qe.empty())
      qe.pop();
   int tx,ty,tstep;
   node tmp,cur;
   tmp.x=sx,tmp.y=sy,tmp.cost=0.0,tmp.step=0;
   qe.push(tmp);
   while(!qe.empty())
   {
	   cur=qe.top();
	   qe.pop();
	   if(cur.cost>=dp[cur.x][cur.y][cur.step])
		   continue;
	   else
	   {
		   dp[cur.x][cur.y][cur.step]=cur.cost;
		   if(cur.x==dx&&cur.y==dy)
		   {
			   flag=true;
			   continue;
		   }
		   if(cur.step==k)
			   continue;
	   for(int i=0;i<4;i++)
	   {
		   tx=cur.x+dir[i][0];
		   ty=cur.y+dir[i][1];
		   if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&map[tx][ty]!='#')
		   {
              tmp.x=tx,tmp.y=ty,tmp.step=cur.step+1;
			  tmp.cost=cur.cost+(1.0*abs(map[cur.x][cur.y]-map[tx][ty])/(k-cur.step));
			  qe.push(tmp);
		   }
	   }
	  }
   }
}
void init()
{
    flag=false;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
			for(int x=0;x<=k;x++)
            dp[i][j][x]=1e9;
}
int main()
{
	double ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=m;j++)
              scanf("%c",&map[i][j]);
        }
        scanf("%d%d%d%d",&sx,&sy,&dx,&dy);
        if(k==0)
		{
			printf("No Answer\n");
			continue;
		} 
        init();
        bfs();
        if(flag)
        {
       	   ans=dp[dx][dy][0];
           for(int i=1;i<=k;i++)
		   {
   			  if(dp[dx][dy][i]<ans)
   			    ans=dp[dx][dy][i];
	       }   
	       printf("%.2lf\n",ans);
		}
        else
          printf("No Answer\n");
    }
    return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值