最小代价

本文介绍了一个寻找矩阵中从左上角到右下角的最小代价路径的问题,通过动态规划算法来解决这一问题,并给出了具体的实现代码。该算法适用于路径规划等应用场景。

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

最小代价问题

Time Limit:1000MS  Memory Limit:65536K
Total Submit:191 Accepted:69

Description

设有一个n×m(小于100)的方格(如图所示),在方格中去掉某些点,方格中的数字代表距离(为小于100的数,如果为0表示去掉的点),试找出一条从A(左上角)到B(右下角)的路径,经过的距离和为最小(此时称为最小代价),从A出发的方向只能向右,或者向下。

Input

Output

Sample Input

4 4  
4 10 7 0
3 2 2 9
0 7 0 4
11 6 12 1

Sample Output

(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)
24


var
 f:array[0..1000,0..1000,1..2] of longint;
 a:array[0..1000,0..1000] of longint;
 i,n,m,k,j:longint;
 x,y:longint;
 procedure dg(x,y:longint);
  begin
   if (x<1) or (y<1) then exit;
   if f[x,y,2]=1 then dg(x,y-1)
                 else dg(x-1,y);
   if (x<>n) or (y<>m) then write('(',x,',',y,')->')
                       else write('(',x,',',y,')')
  end;
begin
 read(n,m);
  for i:=1 to n do
  begin
   for j:=1 to m do
   read(a[i,j]);
  readln;
  end;
  f[1,1,1]:=a[1,1];
  for i:=2 to n do
      if (f[1,i-1,1]<>0) and (a[1,i]<>0)
          then
           begin
            f[1,i,1]:=f[1,i-1,1]+a[1,i];
            f[1,i,2]:=1;
           end;
  for i:=2 to n do
      if (f[i-1,1,1]<>0) and (a[i,1]<>0)
          then begin
                f[i,1,1]:=f[i-1,1,1]+a[i,1];
                f[i,1,2]:=2;
               end;
  for i:=2 to n do
   for j:=2 to m do
       if a[i,j]<>0 then
          begin
              if f[i-1,j,1]=0 then begin
                                   f[i,j,1]:=a[i,j]+f[i,j-1,1];
                                   f[i,j,2]:=1;
                                   end
                            else
              if f[i,j-1,1]=0 then begin
                                   f[i,j,1]:=a[i,j]+f[i-1,j,1];
                                   f[i,j,2]:=2;
                                   end
                            else
              if f[i-1,j,1]>f[i,j-1,1] then begin
                                            f[i,j,1]:=a[i,j]+f[i,j-1,1];
                                            f[i,j,2]:=1;
                                            end
                                   else begin
                                        f[i,j,1]:=a[i,j]+f[i-1,j,1];
                                        f[i,j,2]:=2;
                                        end;
          end;
   //   for i:=1 to n do
   //        begin
   //          for j:=1 to m do write(f[i,j,1],' ');
   //         writeln;
   //     end;


   dg(n,m);
   writeln;
  write(f[n,m,1]-a[n,m]);
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值