POJ 1143 Number Game(状压DP)

4 篇文章 0 订阅

Description

Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. 
The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt chooses a number, then Christine again, and so on. The following rules restrict how new numbers may be chosen by the two players: 

  • A number which has already been selected by Christine or Matt, or a multiple of such a number,cannot be chosen. 
  • A sum of such multiples cannot be chosen, either.

If a player cannot choose any new number according to these rules, then that player loses the game. 
Here is an example: Christine starts by choosing 4. This prevents Matt from choosing 4, 8, 12, etc.Let's assume that his move is 3. Now the numbers 3, 6, 9, etc. are excluded, too; furthermore, numbers like: 7 = 3+4;10 = 2*3+4;11 = 3+2*4;13 = 3*3+4;... are also not available. So, in fact, the only numbers left are 2 and 5. Christine now selects 2. Since 5=2+3 is now forbidden, she wins because there is no number left for Matt to choose. 
Your task is to write a program which will help play (and win!) the Number Game. Of course, there might be an infinite number of choices for a player, so it may not be easy to find the best move among these possibilities. But after playing for some time, the number of remaining choices becomes finite, and that is the point where your program can help. Given a game position (a list of numbers which are not yet forbidden), your program should output all winning moves. 
A winning move is a move by which the player who is about to move can force a win, no matter what the other player will do afterwards. More formally, a winning move can be defined as follows. 

  • A winning move is a move after which the game position is a losing position. 
  • A winning position is a position in which a winning move exists. A losing position is a position in which no winning move exists. 
  • In particular, the position in which all numbers are forbidden is a losing position. (This makes sense since the player who would have to move in that case loses the game.)

Input

The input consists of several test cases. Each test case is given by exactly one line describing one position. 
Each line will start with a number n (1 <= n <= 20), the number of integers which are still available. The remainder of this line contains the list of these numbers a1;...;an(2 <= ai <= 20). 
The positions described in this way will always be positions which can really occur in the actual Number Game. For example, if 3 is not in the list of allowed numbers, 6 is not in the list, either. 
At the end of the input, there will be a line containing only a zero (instead of n); this line should not be processed.

Output

For each test case, your program should output "Test case #m", where m is the number of the test case (starting with 1). Follow this by either "There's no winning move." if this is true for the position described in the input file, or "The winning moves are: w1 w2 ... wk" where the wi are all winning moves in this position, satisfying wi < wi+1 for 1 <= i < k. After this line, output a blank line.

Sample Input

2 2 5
2 2 3
5 2 3 4 5 6
0

Sample Output

Test Case #1
The winning moves are: 2

Test Case #2
There's no winning move.

Test Case #3
The winning moves are: 4 5 6

分析:
这是一个博弈问题,只需要从终止状态开始,将每一个可能的状态标记为必胜态或必败态。终止状态为必败态,可以一步转移到必败态的是必胜态,否则是必败态。因为所有数不超过20,可以采用状态压缩表示每一个状态:第i位为1表示i在该状态下可以取,0表示不可以取。注意在考虑状态转移的时候需要判断状态是否合法:若x可以取,则对于x=y+z,其中y,z至少为2,y,z之一也是可以取的。
代码如下。
/*
	PROG: POJ1143
	PROB: DP + state compression
*/

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

#define DEBUG 1
#define LOG(...) do { if (DEBUG) fprintf(stderr, __VA_ARGS__); } while(0)

#define MAXN 1<<22
char c[23], d[23];
char dp[MAXN];
int main(void) {
	int kase = 0;
	int n;
	while (scanf("%d", &n), n) {
		printf("Test Case #%d\n", ++kase);
		vector<int> a(n);
		for (int i = 0; i < n; ++i) {
			int tmp; scanf("%d", &tmp);
			a[i] = tmp;
		}
		sort(a.begin(), a.end());
		memset(dp, 0, sizeof(dp));
		memset(c, 0, sizeof(c));
		for (int i = 0; i < n; ++i)
			c[a[i]] = 1;
		int N = 1<<n;
		for (int i = 0; i < N; ++i) {
			memset(d, 0, sizeof(d));
			for (int j = 0; j < n; ++j)
				d[a[j]] = (i>>j)&1;
			// whether the state is valid
			bool valid = true;
			for (int j = 0; j < n; ++j) {
				if (!d[a[j]]) continue;
				for (int k1 = 2, k2 = a[j]-2; k1 <= k2; ++k1, --k2)
					if (!d[k1]&&!d[k2]) {
						valid = false; break;
					}
				if (!valid) break;
			}
			if (!valid) continue;

			bool win = false;
			for (int j = 0; j < n; ++j) {
				if (!d[a[j]]) continue;
				int tmp = i;
				tmp &= ~(1<<j);
				for (int j1 = j+1; j1 < n; ++j1) {
					if (!d[a[j1]]) continue;
					bool change = false;
					for (int k = a[j1]-a[j]; k > 1; k -= a[j])
						if (!d[k]) {
							change = true; break;
						}
					if (a[j1]%a[j]==0) change = true;
					if (change) tmp &= ~(1<<j1);
				}
				if (!dp[tmp]) {
					win = true; break;
				}
			}
			dp[i] = win?1:0;
			// LOG("dp[%d]=%d\n", i, dp[i]);
		}
		int x = (1<<n)-1;
		vector<int> ok;
		for (int i = 0; i < n; ++i) {
			int tmp = x;
			tmp &= ~(1<<i);
			for (int j = i+1; j < n; ++j) {
				bool change = false;
				for (int k = a[j]-a[i]; k > 1; k -= a[i])
					if (!c[k]) {
						change = true; break;
					}
				if (a[j]%a[i]==0) change = true;
				if (change) tmp &= ~(1<<j);
			}
			// LOG("choose %d, tmp:%d\n", a[i], tmp);
			if (!dp[tmp]) ok.push_back(a[i]);
		}
		if (!ok.size()) printf("There's no winning move.\n\n");
		else {
			printf("The winning moves are:");
			for (int i = 0; i < ok.size(); ++i)
				printf(" %d", ok[i]);
			printf("\n\n");
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值