求助!最优性剪枝20分,其它全 WA
查看原帖
求助!最优性剪枝20分,其它全 WA
804607
rainygame楼主2023/5/27 07:12
#include <bits/stdc++.h>
using namespace std;
#define MAXN 102
const int movex[] = {0, 1};
const int movey[] = {1, 0};

int n, m, x, y, c;
int gr[MAXN][MAXN], f[MAXN][MAXN];

void dfs(int x, int y, int now, bool g=1){
	if (f[x][y] <= now) return;
	f[x][y] = now;
	
	if (!gr[x+1][y]){
		if (g){
			gr[x+1][y] = 1;
			dfs(x+1, y, now+2, 0);
			gr[x+1][y] = 2;
			dfs(x+1, y, now+2, 0);
			gr[x+1][y] = 0;
		}
	}else if (gr[x+1][y] == gr[x][y]) dfs(x+1, y, now);
	else dfs(x+1, y, now+1);
	
	if (!gr[x][y+1]){
		if (g){
			gr[x][y+1] = 1;
			dfs(x, y+1, now+2, 0);
			gr[x][y+1] = 2;
			dfs(x, y+1, now+2, 0);
			gr[x][y+1] = 0;
		}
	}else if (gr[x][y+1] == gr[x][y]) dfs(x, y+1, now);
	else dfs(x, y+1, now+1);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n >> m;
	while (m--){
		cin >> x >> y >> c;
		gr[x][y] = c+1;
	}
	
	memset(f, 0x3f, sizeof(f));
	dfs(1, 1, 0);
	
	cout << (f[n][n] == 0x3f3f3f3f ? -1 : f[n][n]);
	
	return 0;
}

2023/5/27 07:12
加载中...