前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)

挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)

作者头像
用户1147447
发布2019-05-26 09:42:31
2860
发布2019-05-26 09:42:31
举报
文章被收录于专栏:机器学习入门机器学习入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1434722

挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)

详细代码可以fork下Github上leetcode项目,不定期更新。

练习题如下:

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);
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 挑战程序竞赛系列(49):4.2 推理与动态规划算法(2)
    • POJ 2068: Nim
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档