题解:G. Game on Chessboard

 

#include <bits/stdc++.h>

using namespace std;
#define OST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define endl "\n"
const ll mod = 1e9 + 7, N = (1 << 24) + 5;
string a[13];
ll w[13][13];
ll dp[N];
void solve() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i ++) cin >> a[i];
	for (int i = 0; i < n; i ++)
		for (int j = 0; j < n; j++) cin >> w[i][j];
	ll sta = (1 << n) - 1;
	ll end = sta << n;
	for (int i = 0; i < N; i ++) dp[i] = mod;
	dp[end] = 0;
	for (int s = end; s >= sta; s --) {
		if (dp[s] == mod) continue;
		// 0表示向左走,1表示向右走
		// 从左上走到右下应该要n个1, n个0
		int cnt = 0;
		for (int i = 0; i <= 2 * n; i ++) {
			if (s >> i & 1) cnt ++;
		}
		if (cnt != n) continue;
		int x = - 1, y = 0;
		for (int i = 2 * n - 1; i >= 1; i --) {
			if (s >> i & 1) {
				x ++;
			} else {
				y ++;
			}
			if (s >> i & 1 && !(s >> (i - 1) & 1)) {
				//直接拿掉
				if (a[x][y] == '.')
					dp[s - (1 << (i - 1))] = min(dp[s - (1 << (i - 1))], dp[s]);
				else
					dp[s - (1 << (i - 1))] = min(dp[s - (1 << (i - 1))], dp[s] + w[x][y]);
				// 找一下下一个点然后一起拿掉
				int nx = -1, ny = 0;
				for (int j = 2 * n - 1; j > i + 1; j --) {
					if (s >> j & 1) nx ++;
					else ny ++;
					if (s >> j & 1 && !(s >> (j - 1) & 1)) {
						// cout << nx << " " << ny << endl;
						if (a[x][y] == 'B' && a[nx][ny] == 'W') {
							dp[s - (1 << (i - 1)) - (1 << (j - 1))] = min(dp[s - (1 << (i - 1)) - (1 << (j - 1))], dp[s] + abs(w[x][y] - w[nx][ny]));
						}
						if (a[x][y] == 'W' && a[nx][ny] == 'B') {
							dp[s - (1 << (i - 1)) - (1 << (j - 1))] = min(dp[s - (1 << (i - 1)) - (1 << (j - 1))], dp[s] + abs(w[x][y] - w[nx][ny]));
						}
					}
				}
			}
		}
	}
	cout << dp[sta] << endl;
}

signed main() {
	OST;
	int T = 1;
	// cin >> T;
	while (T--) { solve(); }
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值