比较简短的dfs,错最后一个点,求大佬进来看一眼
查看原帖
比较简短的dfs,错最后一个点,求大佬进来看一眼
592223
Allorry楼主2023/10/27 20:31
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int N = 12;
int n, w[N][N], ans;
int vis[N][N][N][N];

void dfs(int x1, int x2, int y1, int y2, int val) {
	if (x1 > n || y1 > n || x2 > n || y2 > n) return;

	if (x1 == n && x2 == n && y1 == n && y2 == n) {
		ans = max(ans, val); return;
	}

	val += w[x1][y1];
	if (x1 != x2) val += w[x2][y2];

	if (vis[x1][x2][y1][y2] >= val) return;
	vis[x1][x2][y1][y2] = val;

	dfs(x1 + 1, x2 + 1, y1, y2, val);
	dfs(x1 + 1, x2, y1, y2 + 1, val);
	dfs(x1, x2 + 1, y1 + 1, y2, val);
	dfs(x1, x2, y1 + 1, y2 + 1, val);
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	int t1, t2, t3;
	while(cin >> t1 >> t2 >> t3, t1 || t2 ||t3) 
		w[t1][t2] = t3;
	
	memset(vis, -1, sizeof vis);
	dfs(1, 1, 1, 1, 0);
	cout << ans;
}
2023/10/27 20:31
加载中...