当前位置:首页 > > 充电吧
[导读]题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5433 题面: Xiao Ming climbing Time Limit: 2000/1000

题目链接: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 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


题目大意:

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


解题:

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


坑点:

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


错误代码:


错误原因:

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

#include
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 Answern");
			continue;
		} 
        init();
        dfs(sx,sy,k,0.0);
        if(flag)
        {
            printf("%.2lfn",dp[dx][dy]);
        }
        else
          printf("No Answern");
    }
    return 0;
} 


修改代码:

#include 
#include 
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  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 Answern");
			continue;
		} 
        init();
        bfs();
        if(flag)
        {
       	   ans=dp[dx][dy][0];
           for(int i=1;i<=k;i++)
		   {
   			  if(dp[dx][dy][i]


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: 驱动电源

在工业自动化蓬勃发展的当下,工业电机作为核心动力设备,其驱动电源的性能直接关系到整个系统的稳定性和可靠性。其中,反电动势抑制与过流保护是驱动电源设计中至关重要的两个环节,集成化方案的设计成为提升电机驱动性能的关键。

关键字: 工业电机 驱动电源

LED 驱动电源作为 LED 照明系统的 “心脏”,其稳定性直接决定了整个照明设备的使用寿命。然而,在实际应用中,LED 驱动电源易损坏的问题却十分常见,不仅增加了维护成本,还影响了用户体验。要解决这一问题,需从设计、生...

关键字: 驱动电源 照明系统 散热

根据LED驱动电源的公式,电感内电流波动大小和电感值成反比,输出纹波和输出电容值成反比。所以加大电感值和输出电容值可以减小纹波。

关键字: LED 设计 驱动电源

电动汽车(EV)作为新能源汽车的重要代表,正逐渐成为全球汽车产业的重要发展方向。电动汽车的核心技术之一是电机驱动控制系统,而绝缘栅双极型晶体管(IGBT)作为电机驱动系统中的关键元件,其性能直接影响到电动汽车的动力性能和...

关键字: 电动汽车 新能源 驱动电源

在现代城市建设中,街道及停车场照明作为基础设施的重要组成部分,其质量和效率直接关系到城市的公共安全、居民生活质量和能源利用效率。随着科技的进步,高亮度白光发光二极管(LED)因其独特的优势逐渐取代传统光源,成为大功率区域...

关键字: 发光二极管 驱动电源 LED

LED通用照明设计工程师会遇到许多挑战,如功率密度、功率因数校正(PFC)、空间受限和可靠性等。

关键字: LED 驱动电源 功率因数校正

在LED照明技术日益普及的今天,LED驱动电源的电磁干扰(EMI)问题成为了一个不可忽视的挑战。电磁干扰不仅会影响LED灯具的正常工作,还可能对周围电子设备造成不利影响,甚至引发系统故障。因此,采取有效的硬件措施来解决L...

关键字: LED照明技术 电磁干扰 驱动电源

开关电源具有效率高的特性,而且开关电源的变压器体积比串联稳压型电源的要小得多,电源电路比较整洁,整机重量也有所下降,所以,现在的LED驱动电源

关键字: LED 驱动电源 开关电源

LED驱动电源是把电源供应转换为特定的电压电流以驱动LED发光的电压转换器,通常情况下:LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: LED 隧道灯 驱动电源
关闭