UVa11553 - Grid Game

20 篇文章 0 订阅

Alice and Bob both have lots of candies but want more. They decide to play the following turn-based game.

They fill an n grid M with random integers. Alice begins the game by crossing off an uncrossed row i of the grid. Now it’s Bob turn and he crosses off an uncrossed column j of the grid. At the end of Bob’s turn, Alice takes the number candies in the ith row and jth column ofM, call this value M(i, j), from Bob. (If M(ij) is negative, then Alice gives |M(ij)| candies to Bob.) The game continues alternating turns from Alice to Bob until the entire board is crossed off.

What is the largest amount of candies that Alice can win from Bob (or least amount to lose if she cannot win) if both Alice and Bob play optimally?

The beginning of a game between Alice (red) and Bob (blue).

Program Input

The first line of the input contains an integer (1 ≤ ≤ 20), the number of test cases. Each test case starts with (1 ≤ ≤ 8), the size of the grid. Then follow lines containing nnumbers separated by spaces describing M. We call the jth number on ith line M(ij) (-1000 ≤M(ij)≤ 1000).

Program Output

For each test case, print the largest amount of candies that Alice can win from Bob. If she cannot win, print the negative number indicating the minimum number of candies she loses.

Sample Input & Output

INPUT

3
2
10 10
-5 -5
2
10 -5
10 -5
2
10 -5
-5 10
OUTPUT
5
5
-10

#include <cstdio>
#include <algorithm>
#include <climits>

using namespace std;

const int N = 8;

int grid[N][N];
int n;

void input()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &grid[i][j]);
		}
	}
}

void solve()
{
	int seq[N] = {0, 1, 2, 3, 4, 5, 6, 7};
	int Min = INT_MAX;

	do {
		int sum = 0;
		for (int i = 0; i < n; i++) {
			sum += grid[i][seq[i]];
		}

		Min = min(Min, sum);
	} while (next_permutation(seq, seq + n));

	printf("%d\n", Min);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	int t;
	scanf("%d", &t);
	while (t--) {
		input();
		solve();
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值