最小代价问题【DP】

> 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


> 解题思路
这一道题用动态规划来做。
这一道题本来hin简单,只用判断上面和左边的数哪一个小就加上哪一个就行了,但是它偏偏要用一个0的障碍物,所以呢所以那就算了吧让我们共享时间……,就要在判断这里多很多判断。然后还要输出过程,我是用了一个bool来记录,false(0)是右边,true(1)是上面。


> 代码

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=105;
int n,m,a[maxn][maxn]={0},f[maxn][maxn]={0};
bool c[maxn][maxn];

int ooo(int s,int t)
{
	if(s==0) return t;
	//如果上面越界或是0就返回右边
	 else if(t==0) return s;
	//如上
	if(s<t) return s;
	return t;
	//哪个数小就返回哪一个
}

bool ooo1(int s,int t)
{
	if(s==0) return 0;
	//如果上面越界或是0就返回右边
	 else if(t==0) return 1;
	//如上
	if(s<t) return 1;
	return 0;
	//哪个数小就返回哪一个
}

void aka(int s,int t)
{
	if(s==1&&t==1) return;
	if(c[s][t]==1) aka(s-1,t);
	else aka(s,t-1);
	printf("->(%d,%d)",s,t);
}

int main()
{
	int x,y;
	scanf("%d%d",&n,&m);
	for(x=1;x<=n;x++)
	 for(y=1;y<=m;y++)
	  scanf("%d",&a[x][y]);
	
	for(int x=1;x<=n;x++)
	 for(int y=1;y<=m;y++)
	   if(a[x][y]!=0)
	   //如果是0就不用计算了
	   {
		 f[x][y]=ooo(f[x-1][y],f[x][y-1])+a[x][y];
	 	 c[x][y]=ooo1(f[x-1][y],f[x][y-1]);
	   }
	printf("(1,1)");
    aka(n,m);
    //递归输出过程
    printf("\n");
	printf("%d",f[n][m]-a[n][m]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43010386/article/details/82791298