首页 > 其他分享 >[abc349] [E - Weighted Tic-Tac-Toe ] 搜索

[abc349] [E - Weighted Tic-Tac-Toe ] 搜索

时间:2024-04-14 09:22:06浏览次数:27  
标签:chosed Weighted return int ++ static Tac abc349 new

  • 搜索
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {
    static long[][] board = new long[3][3];

    static int[][] chosed = new int[3][3];

    public static void main(String[] args) throws IOException {
        // init arr
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = rd.nextLong();
            }
        }

        boolean ret = dfs(true, 0, 0, 0);
        System.out.println(ret ? "Takahashi": "Aoki");
    }


    public static int check(int chosedNum) {
        // row line
        for (int i = 0; i < 3; i++) {
            boolean flag = true;
            int choice = chosed[i][0];
            if (choice == 0) {
                continue;
            }
            for (int j = 1; j < 3; j++) {
                if (chosed[i][j] != chosed[i][j-1]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return choice;
            }
        }

        // col line
        for (int c = 0; c < 3; c++) {
            boolean flag = true;
            int choice = chosed[0][c];
            if (choice == 0) {
                continue;
            }
            for (int r = 1; r < 3; r++) {
                if (chosed[r][c] != chosed[r-1][c]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return choice;
            }
        }

        //dia line

        if (chosed[0][0] != 0 && chosed[0][0] == chosed[1][1] && chosed[1][1] == chosed[2][2]) {
            return chosed[0][0];
        }

        if (chosed[0][2] != 0 && chosed[0][2] == chosed[1][1] && chosed[1][1] == chosed[2][0]) {
            return chosed[0][2];
        }

        return 0;
    }

    static String[] arr = new String[] {
            "Takahashi",
            "Aoki"
    };

    static int[][] path = new int[9][2];
    public static boolean dfs(boolean isBlack, int chosedNum, long blackSum, long whiteSum) {
        if (chosedNum == 9) {
            return blackSum > whiteSum;
        }
        int whoWin = check(chosedNum);
        if ( whoWin == 2) {// whitewin
            return false;
        }
        if ( whoWin == 1) {// blackwin;
            return true;
        }
        if (isBlack) {
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (chosed[i][j] == 0) {
                        // exist one chose, it can be true;
                        path[chosedNum] = new int[] {i,j};
                        chosed[i][j] = 1;
                        boolean result = dfs(false, chosedNum + 1, blackSum+board[i][j], whiteSum);
                        chosed[i][j] = 0;
                        if (result) {
                            return true;
                        }

                    }
                }
            }
            return false;
        }

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (chosed[i][j] == 0) {
                    path[chosedNum] = new int[] {i,j};
                    // for all the white chose, it must be true;
                    chosed[i][j] = 2;
                    boolean result = dfs(true, chosedNum + 1, blackSum, whiteSum + board[i][j]);
                    chosed[i][j] = 0;
                    if (!result) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}

class rd {
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    // nextLine()读取字符串
    static String nextLine() throws IOException {
        return reader.readLine();
    }

    // next()读取字符串
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    // 读取一个int型数值
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    // 读取一个double型数值
    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    // 读取一个long型数值
    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    // 读取一个BigInteger
    static BigInteger nextBigInteger() throws IOException {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

标签:chosed,Weighted,return,int,++,static,Tac,abc349,new
From: https://www.cnblogs.com/fishcanfly/p/18133760

相关文章

  • ABC349E
    E-WeightedTic-Tac-Toe(atcoder.jp)这可不是博弈论!推了半天性质,脑子要干爆了,发现这题固定的\(3\times3\)棋盘,可以爆搜啊。直接用搜索模拟所有过程即可,难点在优雅地实现。inta[9];intdp[512][512];//记忆化inlineboolcheck(intX){for(inti=0;i<=......
  • [atcode abc349] D - Divide Interval
    解决方法,贪心。importjava.io.*;importjava.math.BigInteger;importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args)throwsIOException{longL,R;L=rd.nextLong();R=rd.nextLong();PrintWri......
  • CF1923B Monsters Attack! 题解
    题目简述数轴上有$n$个怪兽。最初第$i$个怪兽在$x_i$位置上,且血量为$a_i$。你在位置$0$上。在每秒钟会发生:你给任意怪兽发射总共$k$颗子弹,受到攻击的怪兽血量减一。血量小于等于$0$的怪兽死亡。没有死亡的怪兽向你移动一个单位。当一个怪兽来到你的位置,你就输......
  • 52 Things: Number 43: Describe some basic (maybe ineffective) defences against s
    52Things:Number43:Describesomebasic(maybeineffective)defencesagainstsidechannelattacksproposedintheliteratureforAES52件事:第43件:描述AES文献中提出的针对侧信道攻击的一些基本(可能无效)防御措施 Thisisthelatestinaseriesofblogpoststoa......
  • 52 Things: Number 44: Describe some basic (maybe ineffective) defences against s
    52Things:Number44:Describesomebasic(maybeineffective)defencesagainstsidechannelattacksproposedintheliteratureforECC.52件事:第44件:描述文献中为ECC提出的一些针对侧信道攻击的基本(可能无效)防御措施。 Thisisthelatestinaseriesofblogposts......
  • 52 Things: Number 45: Describe some basic (maybe ineffective) defences against s
    52Things:Number45:Describesomebasic(maybeineffective)defencesagainstsidechannelattacksproposedintheliteratureforRSA.52件事:第45件:描述RSA文献中提出的针对侧信道攻击的一些基本(可能无效)防御措施。 Thisisthelatestinaseriesofblogpostst......
  • 52 Things: Number 39: What is the difference between a side-channel attack and a
    52Things:Number39:Whatisthedifferencebetweenaside-channelattackandafaultattack?52件事:第39件:侧通道攻击和故障攻击之间的区别是什么? Thisisthelatestinaseriesofblogpoststoaddressthelistof '52ThingsEveryPhDStudentShouldKnowT......
  • 52 Things: Number 33: How does the Bellcore attack work against RSA with CRT?
    52Things:Number33:HowdoestheBellcoreattackworkagainstRSAwithCRT?52件事:第33件:Bellcore攻击如何使用CRT对抗RSA? Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography':......
  • 进阶 stack smashing--canary 报错利用 && environ泄露栈地址
    进阶stacksmashing--canary报错利用&&environ泄露栈地址这部分是对进阶stacksmashing的使用,以及对environ的认识,我们可以看一个buu上具体的题目题目连接https://buuoj.cn/challenges#wdb2018_guess看一下保护,pie没有开64位ida载入看一下那么在ida里面看见还是挺麻......
  • stack smashing--canary报错利用
    stacksmashing--canary报错利用一般这种都是考察点比较狭窄,因为这个漏洞在libc2.23以上就被修复了,漏洞产生的原因是因为当覆盖掉canary的时候程序会报错,程序会执行__stack_chk_fail函数来打印__libc_argv[0]指针所指向的字符串,如果把这个字符串覆盖成flag地址那么就可以得......