汽车加油行驶问题(car)

本文介绍使用动态规划(DP)算法解决油库路径问题,通过构建工作矩阵和枚举走法,优化路径选择并考虑油量消耗,最终求解最小花费路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

强烈的鄙视一下《算法设计与实验题解》这本书,书上的题解根本就是另一道题,根本对应不上答案。

算法:DP

分析:用work[i,j,k]表示到(i,j)这个点剩余油量为k的最小花费。

program car;

const
 maxn=100;
 maxoil=12;

var
 n,go,cost,gocost,build:longint;
 map:array [0..maxn,0..maxn] of longint;{map[i,j]表示当前是否为油库。}
 work:array [0..maxn,0..maxn,0..maxoil] of longint;{}
 s:array [0..4,0..3] of longint;{s虽然是变量,但是相当于一个常量数组,里面记录了四个方向的走法和花费。}

procedure init;
var
 i,j:longint;
begin
 readln(n,go,cost,gocost,build);
 for i:=0 to n-1 do
  begin
   for j:=0 to n-1 do read(map[i,j]);
   readln;
  end;
 s[0,0]:=-1;
 s[0,1]:=0;
 s[0,2]:=0;
 s[1,0]:=0;
 s[1,1]:=-1;
 s[1,2]:=0;
 s[2,0]:=1;
 s[2,1]:=0;
 s[2,2]:=gocost;
 s[3,0]:=0;
 s[3,1]:=1;
 s[3,2]:=gocost;
end;

procedure main;
var
 i,y,j,p,q,min,x:longint;
begin
 fillchar(work,sizeof(work),100);{求最小值赋值为最大值。}
 for i:=0 to go do work[0,0,i]:=0;
 y:=1;
 while y<>0 do
  begin
   y:=0;
   for i:=0 to n-1 do
    begin
     for j:=0 to n-1 do
      begin
       if (i<>0) or (j<>0) then{不能为起点。}
        begin
         for p:=0 to go do{枚举能走的距离。}
          begin
           min:=1000000;{初始化最小值。}
           for q:=0 to 3 do
            begin
 	     {判断不能出了边界。}
             if (i=0) and (q=0) then continue;
             if (j=0) and (q=1) then continue;
             if (i=n-1) and (q=2) then continue;
             if (j=n-1) and (q=3) then continue;
             if work[i+s[q,0],j+s[q,1],p+1]+s[q,2]<min then min:=work[i+s[q,0],j+s[q,1],p+1]+s[q,2];{先计算正规的走法。}
            end;
           if min<1000000 then
            begin
             if work[i,j,p]>min+cost*map[i,j] then inc(y);
             work[i,j,p]:=min;
             if map[i,j]=1 then
              begin
               inc(work[i,j,0],cost);
               for x:=1 to go do work[i,j,x]:=work[i,j,0];
               break;
              end;
            end
           else
            begin
             work[i,j,p]:=work[i,j,0]+cost+build;
             for x:=p+1 to go do work[i,j,x]:=work[i,j,p];
             break;
            end;
          end;
        end;
      end;
    end;
  end;
end;

begin
 assign(input,'car.in'); reset(input);
 assign(output,'car.out'); rewrite(output);

 init;
 main;
 writeln(work[n-1,n-1,0]);

 close(input); close(output);
end.



 

1.问题描述 给定一个N*N 的方形网格,设其左上角为起点,坐标为(1,1),X 轴向右为正,Y 轴 向下为正,每个方格边长为1。一辆汽车从起点出发驶向右下角终点,其坐标为(N,N)。 在若干个网格交叉点处,设置了油库,可供汽车行驶途中加油汽车行驶过程中应遵守 如下规则: (1)汽车只能沿网格边行驶,装满油后能行驶K 条网格边。出发时汽车已装满油,在 起点与终点处不设油库。 (2)当汽车行驶经过一条网格边时,若其X 坐标或Y 坐标减小,则应付费用B,否则 免付费用。 (3)汽车行驶过程中遇油库则应加满油并付加油费用A。 (4)在需要时可在网格点处增设油库,并付增设油库费用C(不含加油费用A)。 (5)(1)(4)中的各数N、K、A、B、C均为正整数。 算法设计: 求汽车从起点出发到达终点的一条所付费用最少的行驶路线。 数据输入: 输入数据。第一行是N,K,A,B,C的值,2 <= N <= 100, 2 <= K <= 10。第二行起是一个N*N 的0-1方阵,每行N 个值,至N+1行结束。方阵的第i 行第j 列处的值为1 表示在网格交叉点(i,j)处设置了一个油库,为0 时表示未设油库。 各行相邻的2 个数以空格分隔。 结果输出: 将找到的最优行驶路线所需的费用,即最小费用输出. Sample input 9 3 2 3 6 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 Sample output 12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值