233225

#include <stdio.h>
void main()
{
 int v[4][5] = {{0,0,0,0,0}, {2,2,1,2,4}, {3,4,1,2,3}, {2,1,2,2,3}};
 int h[4][5] = {{0,1,3,2,3}, {0,2,2,1,4}, {0,2,3,4,5}, {0,2,3,1,2}};
 int cost[4][5] = {0};
 int i, j, temp1, temp2, count = 0, row[8] = {0}, column[8] = {0};
/*************
 求最短代价
*************/
 for (i = 1; i <= 3; i++)
 {
  cost[i][0] = cost[i-1][0] + v[i][0];
 }
 for (j = 1; j <= 4; j++)
 {
  cost[0][j] = cost[0][j-1] + h[0][j];
 }
 for (i = 1; i <= 3 ; i++)
  for (j = 1; j <=4; j++)
  {
   temp1 = cost[i-1][j] + v[i][j];
   temp2 = cost[i][j-1] + h[i][j];
   cost[i][j] = temp1 > temp2 ? temp2 : temp1;
  }
 printf("最小代价是:%d\n", cost[3][4]);
/*************
 求最短路径
*************/
 i = 3;
 j = 4;
 row[0] = i;
 column[0] = j;
 while (i!=0 && j!=0)
 {
  temp1 = cost[i-1][j] + v[i][j];
  temp2 = cost[i][j-1] + v[i][j];
  temp1 < temp2 ? i-- : j--;
  count++;
  row[count] = i;
  column[count] = j;
 }
 printf("最短路径为:\n");
 for (i = 7; i >= 0 ; i--)
 {
  printf("(%d, %d)\n", row[i], column[i]);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值