每日一题:794. 有效的井字游戏

解题思路

DFS+回溯,主要是要考虑各种情况

代码

class Point{
    int x;
    int y;
    Point(int n,int m){
        this.x = n;
        this.y = m;
    }
}
class Solution {
    boolean Flag = false;
    public boolean validTicTacToe(String[] board) {
        char[][] chessBoard = new char[3][3];
        char[][] curBoard = new char[3][3];
        ArrayList<Point> Xlist = new ArrayList<>();
        ArrayList<Point> Olist = new ArrayList<>();
        int X = 0,O = 0;
        for (int i=0;i<board.length;i++){
            for (int j=0;j<board[i].length();j++){
                chessBoard[i][j] = board[i].charAt(j);
                if (chessBoard[i][j] == 'X'){
                    X++;
                    Xlist.add(new Point(i,j));
                }
                else if (chessBoard[i][j] == 'O'){
                    O++;
                    Olist.add(new Point(i,j));
                }
            }
            //填充棋盘
            Arrays.fill(curBoard[i],' ');
        }
        if (O > X || Math.abs(O-X)>1){
            return false;
        }
        boolean[] Xvisited = new boolean[Xlist.size()];
        boolean[] Ovisited = new boolean[Olist.size()];
        dfs(chessBoard,curBoard,Xlist,Olist,'X',Xvisited,Ovisited);
        return Flag;
    }
    //cur棋盘有空格问题
    public void dfs(char[][] chessBoard, char[][] curBoard, ArrayList<Point> Xlist, ArrayList<Point> Olist, char cur,boolean[] Xvisited,boolean[] Ovisited){
        if (Flag){
            return;
        }
        if (judgeSucess(curBoard)){
            for (int i=0;i<Xvisited.length;i++){
                if (!Xvisited[i]){
                    return;
                }
            }
            for (int i=0;i<Ovisited.length;i++){
                if (!Ovisited[i]){
                    return;
                }
            }
            if (judgeSame(chessBoard,curBoard)){
                Flag = true;
            }
            return;
        }
        if (judgeSame(chessBoard,curBoard)){
            Flag = true;
            return;
        }
        if (cur == 'X'){
            for (int i=0;i<Xlist.size();i++){
                if (!Xvisited[i]){
                    Point first = Xlist.get(i);
                    Xvisited[i] = true;
                    char c = curBoard[first.x][first.y];
                    curBoard[first.x][first.y] = 'X';
                    dfs(chessBoard, curBoard, Xlist, Olist, 'O',Xvisited,Ovisited);
                    curBoard[first.x][first.y] = c;
                    Xvisited[i] = false;
                }
            }
        }
        else if (cur == 'O'){
            for (int i=0;i<Olist.size();i++){
                if (!Ovisited[i]){
                    Point first = Olist.get(i);
                    Ovisited[i] = true;
                    char c = curBoard[first.x][first.y];
                    curBoard[first.x][first.y] = 'O';
                    dfs(chessBoard, curBoard, Xlist, Olist, 'X',Xvisited,Ovisited);
                    curBoard[first.x][first.y] = c;
                    Ovisited[i] = false;
                }
            }
        }
    }
    //判断当前棋盘是否有一方已经胜利
    public boolean judgeSucess(char[][] curBoard){
        return (curBoard[0][0]!=' ' && curBoard[0][1]!=' ' && curBoard[0][2]!=' ' && curBoard[0][0]==curBoard[0][1] && curBoard[0][1]==curBoard[0][2]) ||
                (curBoard[1][0]!=' ' && curBoard[1][1]!=' ' && curBoard[1][2]!=' ' && curBoard[1][0]==curBoard[1][1] && curBoard[1][1]==curBoard[1][2]) ||
                (curBoard[2][0]!=' ' && curBoard[2][1]!=' ' && curBoard[2][2]!=' ' && curBoard[2][0]==curBoard[2][1] && curBoard[2][1]==curBoard[2][2]) ||
                (curBoard[0][0]!=' ' && curBoard[1][1]!=' ' && curBoard[2][2]!=' ' && curBoard[0][0]==curBoard[1][1] && curBoard[1][1]==curBoard[2][2]) ||
                (curBoard[0][2]!=' ' && curBoard[1][1]!=' ' && curBoard[2][0]!=' ' && curBoard[0][2]==curBoard[1][1] && curBoard[1][1]==curBoard[2][0]) ||
                (curBoard[0][0]!=' ' && curBoard[1][0]!=' ' && curBoard[2][0]!=' ' && curBoard[0][0]==curBoard[1][0] && curBoard[1][0]==curBoard[2][0]) ||
                (curBoard[0][1]!=' ' && curBoard[1][1]!=' ' && curBoard[2][1]!=' ' && curBoard[0][1]==curBoard[1][1] && curBoard[1][1]==curBoard[2][1]) ||
                (curBoard[0][2]!=' ' && curBoard[1][2]!=' ' && curBoard[2][2]!=' ' && curBoard[0][2]==curBoard[1][2] && curBoard[1][1]==curBoard[2][2]) ;
    }
    //判断当前棋盘是否相同
    public boolean judgeSame (char[][] chessBoard,char[][] curBoard){
        for (int i=0;i<3;i++){
            String s1 = new String(chessBoard[i]);
            String s2 = new String(curBoard[i]);
            if (!s1.equals(s2)){
                return false;
            }
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值