标签:
#include "stdio.h" #include "stdlib.h" #include "time.h" void main() { int m,n,i,j,t,min,a[30][30],b[30][30]; char stw,stm[30][30]; printf("Row:n:"); scanf("%d",&n); printf("line m:"); scanf("%d",&m); t=time(0)%1000; srand(t); printf("\n"); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { a[i][j]=rand()%40+5; b[i][j]=a[i][j]; printf("%4d",a[i][j]); } printf("\n"); } for(j=m-1;j>=1;j--) { b[n][j]=b[n][j]+b[n][j+1]; stm[n][j]=‘R‘; } for(i=n-1;i>=1;i--) { b[i][m]=b[i][m]+b[i+1][m]; stm[i][m]=‘D‘; } for(i=n-1;i>=1;i--) for(j=m-1;j>=1;j--) { min=b[i+1][j+1]; stm[i][j]=‘O‘; if(min>b[i+1][j]) { min=b[i+1][j]; stm[i][j]=‘D‘; } if(min>b[i][j+1]) { min=b[i][j+1]; stm[i][j]=‘R‘; } b[i][j]=b[i][j]+min; } printf("\n%d",b[1][1]); printf("\n %2d",a[1][1]); i=1;j=1; while(i<n || j<m) { stw=stm[i][j]; if(stw==‘R‘) { j++; printf("-R-%d",a[i][j]); } else if(stw==‘D‘) { i++; printf("-D-%d",a[i][j]); } else { j++; i++; printf("-O-%d",a[i][j]); } } }
标签:
原文地址:http://www.cnblogs.com/tinaluo/p/5132194.html