TAT
查看原帖
TAT
782718
Litter_Yin楼主2024/5/15 18:12

问一下,这道题不能用两次二维动归解决呢?代码如下,由于我太辣了,所以各位大佬不要生气[doge]

#include<bits/stdc++.h>
using namespace std;

long long n, x, y, z, a[15][15];
struct whole
{
	long long num, fx, fy; 
}f[15][15];
void dfs(long long x, long long y)
{
	if(x <= 0 || y <= 0) return ;
	if(a[x][y] >= 1) a[x][y] = 0;
	if(f[x][y].fx == -1) dfs(x - 1, y);
	else dfs(x, y - 1);
}

int main()
{
	cin >> n;
	while(1)
	{
		cin >> x >> y >> z;
		if(x == 0 && y == 0 && z == 0) break;
		a[x][y] = z;
		f[x][y].num = z;
	}
	for(int i = 2; i <= n; i++)
		for(int j = 2; j <= n; j++)
			if(f[i - 1][j].num > f[i][j - 1].num) f[i][j].num += f[i - 1][j].num, f[i][j].fx = -1;
			else f[i][j].num += f[i][j - 1].num, f[i][j].fy = -1;
	dfs(n, n);
	for(int i = 2; i <= n; i++)
		for(int j = 2; j <= n; j++)
			a[i][j] += max(a[i - 1][j], a[i][j - 1]);
	cout << a[n][n] + f[n][n].num;
	return 0;
}
2024/5/15 18:12
加载中...