挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
来源:互联网 发布:mac版本b站 编辑:程序博客网 时间:2024/05/21 18:46
挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
详细代码可以fork下Github上leetcode项目,不定期更新。
练习题如下:
- POJ 2068: Nim
POJ 2068: Nim
团体尼姆赛:传统的尼姆游戏由两名玩家进行,在一堆石头中,双方轮流取走任意合法数量块石头,取走最后一块石头的玩家落败。多人尼姆游戏将参赛人数拓展至两个队伍,每支队伍有n名队员交错入座,单次分别能最多取走Mi块石头,取走S块石头中的最后一块的队伍失败,求第一支队伍是否有必胜策略?
动态规划题,如果单纯的两个人轮流取,直接利用数学公式直接求出,但此题多轮多人且每人取的石头数也不同,这貌似只能动规了。
dp[i][j][k] 第i支队伍第k个人,剩余k个石子时,能否赢得当前轮注意当k = 0,表明是必胜态,而当k = 1时,一定为必输态。
代码如下:
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.StringTokenizer;public class Main{ String INPUT = "./data/judge/201709/P2068.txt"; public static void main(String[] args) throws IOException { new Main().run(); } static final int MAX_S = 1 << 13; boolean[][][] dp; //第k只队伍 第j个队员 剩余i个石头的状态 void solve() { while (true) { int n = ni(); if (n == 0) break; int s = ni(); dp = new boolean[2][12][MAX_S + 16]; int[] team = new int[2 * n]; for (int i = 0; i < 2 * n; ++i) { team[i] = ni(); } for (int i = 0; i < 2 * n; ++i) { dp[i & 1][i / 2 + 1][0] = true; dp[i & 1][i / 2 + 1][1] = false; // 必输态 } for (int i = 2; i <= s; ++i) { for (int now = 0; now < 2 * n; ++now) { int nxt = (now + 1) % (2 * n); for (int j = team[now]; j >= 1; --j) { if (i - j >= 0) { dp[now & 1][now / 2 + 1][i] |= !dp[nxt & 1][nxt / 2 + 1][i - j]; if (dp[now & 1][now / 2 + 1][i]) break; } } } } out.println(dp[0][1][s] ? "1" : "0"); } } FastScanner in; PrintWriter out; void run() throws IOException { boolean oj; try { oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode"); } catch (Exception e) { oj = System.getProperty("ONLINE_JUDGE") != null; } InputStream is = oj ? System.in : new FileInputStream(new File(INPUT)); in = new FastScanner(is); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!oj){ System.out.println("[" + (System.currentTimeMillis() - s) + "ms]"); } } public boolean more(){ return in.hasNext(); } public int ni(){ return in.nextInt(); } public long nl(){ return in.nextLong(); } public double nd(){ return in.nextDouble(); } public String ns(){ return in.nextString(); } public char nc(){ return in.nextChar(); } class FastScanner { BufferedReader br; StringTokenizer st; boolean hasNext; public FastScanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); hasNext = true; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { hasNext = false; return "##"; } } return st.nextToken(); } String next = null; public boolean hasNext(){ next = nextToken(); return hasNext; } public int nextInt() { if (next == null){ hasNext(); } String more = next; next = null; return Integer.parseInt(more); } public long nextLong() { if (next == null){ hasNext(); } String more = next; next = null; return Long.parseLong(more); } public double nextDouble() { if (next == null){ hasNext(); } String more = next; next = null; return Double.parseDouble(more); } public String nextString(){ if (next == null){ hasNext(); } String more = next; next = null; return more; } public char nextChar(){ if (next == null){ hasNext(); } String more = next; next = null; return more.charAt(0); } }}
阅读全文
0 0
- 挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
- 挑战程序竞赛系列(48):4.2 推理与动态规划算法(1)
- 挑战程序竞赛系列(50):4.2 推理与动态规划算法(3)
- 挑战程序竞赛系列(51):4.2 推理与动态规划算法(4)
- 挑战程序竞赛系列(1):2.3动态规划
- 《挑战程序设计竞赛》4.2.1 游戏必胜策略-推理与动态规划算法 POJ2484 2348 1082 2068 3688 1740(1)
- 挑战程序竞赛系列(64):4.7字符串上的动态规划(2)
- 挑战程序竞赛系列(63):4.7字符串上的动态规划(1)
- 挑战程序竞赛系列(65):4.7字符串上的动态规划(3)
- 挑战程序竞赛系列(29):3.4熟练掌握动态规划
- 挑战程序竞赛系列(32):4.5 A*与IDA*
- 挑战程序竞赛系列(82):4.3 LCA(2)
- 挑战程序竞赛系列(52):4.2 Nim 与 Grundy 数
- 挑战程序竞赛系列(24):3.5最大流与最小割
- 挑战程序竞赛系列(81):4.3 LCA(1)
- 挑战程序竞赛系列(4):2.1深度优先搜索
- 挑战程序竞赛系列(5):2.1广度优先搜索
- 挑战程序竞赛系列(6):2.1穷尽搜索
- python的md5和sha1加密
- Maven学习总结(七)——Maven与Eclipse整合
- SpringMVC与Struts2区别与比较总结
- 把数据库里的大表放到REDIS中(从Mysql将数据快速导入到redis中 )
- Seeds算法(SEEDS: Superpixels Extracted via Energy-Driven Sampling)
- 挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
- DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs
- Caffe入门以及迁移学习(Fintune)的方法
- kotlin mybatis:No constructor found in void matching
- vue学习笔记5——路由相关配置,引入其他插件等
- 浅谈前端CSS样的编写
- GeoServer基础教程(六):使用GeoServer发布PostGIS表数据
- CSS图片宽度设置百分比 , 高度同宽度相同
- 列表和字典嵌套完成电话簿(增删改查)