#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]);
}
}
233225
最新推荐文章于 2024-10-08 06:04:53 发布