UVA 1558 - Number Game(博弈dp)
UVA 1558 - Number Game
题意:20之内的数字,每次能够选一个数字,然后它的倍数,还有其它已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法
思路:利用dp记忆化去求解,要输出方案就枚举第一步就可以,状态转移过程中,选中一个数字,对应的变化写成一个函数,然后就是普通的博弈问题了,必胜态之后必有必败态,必败态之后全是必胜态
代码:
#include <stdio.h>
#include <string.h> const int N = 1050005;
int t, n, w, start, dp[N], ans[25], an; int getnext(int state, int x) {
for (int i = x; i <= 20; i += x)
if (state&(1<<(i - 2)))
state ^= (1<<(i - 2));
for (int i = 2; i <= 20; i++) {
if (state&(1<<(i - 2))) {
for (int j = x; i - j >= 2; j += x) {
if (!(state&(1<<(i - j - 2)))) {
state ^= (1<<(i - 2));
break;
}
}
}
}
return state;
} int dfs(int state) {
if (dp[state] != -1) return dp[state];
if (state == 0) return dp[state] = 0; for (int i = 2; i <= 20; i++) {
if (state&(1<<(i - 2))) {
if (dfs(getnext(state, i)) == 0)
return dp[state] = 1;
}
}
return dp[state] = 0;
} int main() {
int cas = 0;
scanf("%d", &t);
memset(dp, -1, sizeof(dp));
while (t--) {
start = 0; an = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &w);
start |= (1<<(w - 2));
}
for (int i = 2; i <= 20; i++) {
if (start&(1<<(i - 2))) {
if (dfs(getnext(start, i)) == 0)
ans[an++] = i;
}
}
printf("Scenario #%d:\n", ++cas);
if (an) {
printf("The winning moves are:");
for (int i = 0; i < an; i++)
printf(" %d", ans[i]);
printf(".\n");
}
else printf("There is no winning move.\n");
printf("\n");
}
return 0;
}
UVA 1558 - Number Game(博弈dp)的更多相关文章
- HDU 5623 KK's Number (博弈DP)
KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...
- UVA 1557 - Calendar Game(博弈dp)
UVA 1557 - Calendar Game 题目链接 题意:给定一个日期,两个人轮流走,每次能够走一月或者一天,问最后谁能走到2001.11.4这个日子 思路:记忆化搜索,对于每一个日期,假设下 ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- 博弈dp 以I Love this Game! POJ - 1678 为例
写在前面的话 知识基础:一些基础的博弈论的方法,动态规划的一些知识 前言:博弈论就是一些关于策略或者游戏之间的最优解,动态规划就是对于一些状态之间转移的一些递推式(or 递归),dp分为很多很多种,比 ...
- uva 11885 - Number of Battlefields(矩阵高速幂)
题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...
- 博弈dp入门 POJ - 1678 HDU - 4597
本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVA 10404 Bachet's Game(dp + 博弈?)
Problem B: Bachet's Game Bachet's game is probably known to all but probably not by this name. Initi ...
- UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...
随机推荐
- jstl经典用法
jstl的forEach使用和set变量实现自增: <body> <c:set var="index" value="0" /> < ...
- SQL Profiler工具简介
一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...
- mybatis的简单使用
使用mybatis数据库时,需要添加一下jar包: asm-3.3.1.jarcglib-2.2.2.jarjavassist-3.17.1-GA.jarlog4j-1.2.17.jarmybatis ...
- FineUI 点击按钮添加标签页
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...
- shell获取日期(昨天,明天,上月,下月)
今天 sh-4.1$ echo `date +%Y-%m-%d` 2016-08-17 昨天 sh-4.1$ echo `date -d "last day" +%Y-%m-%d` ...
- Swift中对计算属性的理解和对之前的补充
这个功能的重点作用应该是在计算上. 对于一般的属性,要么直接存一个,要么直接读一个,计算属性则可以根据所设置内容,进行一些修改或计算之类的, 比如: import UIKit class sample ...
- (六)backbone - API学习 - Backbone路由
Backbone路由本质 Backbone路由分为两个大块,Router以及History用户在Router中定义相关规则,然后开启history.start进行路由监控,执行默认的回调所以,Rout ...
- POj3421 X-factor Chains(质因数分解+排列组合)
POj3421X-factor Chains 一开始没读懂题意,不太明白 Xi | Xi+1 where a | b means a perfectly divides into b的意思,后来才发现 ...
- The use of function Merge (update、insert、delete)
1.The use of function merge(update.insert.delete) Example: #1.Initialize the data create table #test ...
- angular ng-bind-html 对src路径失效 解决方案
json内容 ;<img src="/newsfile/1506271512489.jpg" width="600" height="450&q ...