最低通行费 https://www.acwing.com/problem/content/1020/
从NxN的网格的左上角起点出发,右下角终点出来
每穿过一个小方格耗费1各单位时间,必须在2N-1个单位时间内走出来->不能走回头路
每个方格含一个权值,求经过方格权值之和的最小值
类似摘花生,但是求最小值需考虑边界问题
可以先将g数组初始化为无穷大
#include <iostream>
#include <cstring>
using namespace std;
const int N = 105;
int f[N][N], g[N][N], n;
int main()
{
cin >> n;
memset(g, 0x3f, sizeof g);
for ( int i = 1; i <= n; i ++ )
for ( int j = 1; j <= n; j ++ )
cin >> g[i][j];
f[1][1] = g[1][1];
for ( int i = 1; i <= n; i ++ )
for ( int j = 1; j <= n; j ++ )
{
if ( i == 1 ) f[i][j] = f[i][j - 1] + g[i][j];
else if ( j == 1 ) f[i][j] = f[i - 1][j] + g[i][j];
else f[i][j] = min(f[i - 1][j] + g[i][j], f[i][j - 1] + g[i][j]);
}
cout << f[n][n];
return 0;
}