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

  • 搜索
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;
    }
}
posted @ 2024-04-14 09:18  fishcanfly  阅读(28)  评论(0)    收藏  举报
//雪花飘落效果