最小代价问题
来源:互联网 发布:mac装win7 集成显卡 编辑:程序博客网 时间:2024/06/06 19:35
最小代价问题
Description
设有一个n×m(小于100)的方格(如图所示),在方格中去掉某些点,方格中的数字代表距离(为小于100的数,如果为0表示去掉的点),试找出一条从A(左上角)到B(右下角)的路径,经过的距离和为最小(此时称为最小代价),从A出发的方向只能向右,或者向下。
Input
Output
Sample Input
4 4 4 10 7 03 2 2 90 7 0 411 6 12 1
Sample Output
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)
var map,dis,g:array[0..101,0..101]of longint; y,x,i,j,t:longint;procedure dg(n,m:longint);begin if (n<1) or (m<1) then exit; if g[n,m]=2 then dg(n-1,m) else dg(n,m-1); if (n<>x) or (m<>y) then write('(',n,',',m,')->') else write('(',n,',',m,')');end;begin read(x,y); for i:=1 to x do begin for j:=1 to y do read(map[i,j]); readln; end; dis[1,1]:=map[1,1]; for i:=1 to y do if (dis[1,i-1]<>0) and (map[1,i]<>0) then begin dis[1,i]:=map[1,i]+dis[1,i-1]; g[1,i]:=1;//标记路径 end; for i:=1 to x do if (dis[i-1,1]<>0) and (map[i,1]<>0) then begin dis[i,1]:=map[i,1]+dis[i-1,1]; g[i,1]:=2;//标记路径 end; for i:=2 to x do begin for j:=2 to y do begin if (map[i,j]<>0) then begin if (dis[i-1,j]=0) or ((dis[i,j-1]<>0) and (dis[i-1,j]>dis[i,j-1])) then begin dis[i,j]:=map[i,j]+dis[i,j-1]; g[i,j]:=1;//标记路径 end else if (dis[i,j-1]=0) or ((dis[i-1,j]<>0) and (dis[i,j-1]>dis[i-1,j])) then begin dis[i,j]:=map[i,j]+dis[i-1,j]; g[i,j]:=2;//标记路径 end; end; end; end; dg(x,y); writeln; write(dis[x,y]-map[x,y]);end.
0 0
- 最小代价问题
- 最小代价问题
- 最小代价问题
- [DP]最小代价问题
- 最小代价问题
- 最小代价问题
- 买一送一的最小代价问题
- SSL 1460——最小代价问题
- 最小代价
- 最小代价;;;;
- 最小代价
- 最小代价
- POJ1251(基础的最小代价生成树问题)
- 动态规划系列问题-最小编辑代价
- 霍夫曼树之切割木板最小总代价问题
- 最小代价子母树
- 最小代价树
- 最小代价树
- dvm_lock_sample 解析
- boost的函数:all_of、all_of_equal、any_of、any_of_equal、none_of、none_of_equal、one_of、one_of_equal
- 线性回归与梯度下降法——原理与实现
- js时间比较函数
- C语言练习 (典型递归问题)汉诺塔问题
- 最小代价问题
- 234. Palindrome Linked List | 判断一个单链表是否是回文
- a标签传值及接收
- 服务治理
- torch篇之一
- 【SAP HANA】新建表以及操作数据(3)
- Java jdbc数据库连接池总结!
- 轮廓提取cvDrawContours 函数和cvDrawContours函数的使用
- iptables之四表五链