혼자서 하는 틱택토

LJM·2023년 8월 16일
0

programmers

목록 보기
60/92

반례들

OOO
XX.
.X.

0

XXX
OO.
OO.

0

XXX
OO.
O..
1

class Solution {
    public int solution(String[] board) {
        
        int oh = 0;
        int ex = 0;
        
        int[][] mat = new int[3][3];
        
        for(int i = 0; i < board.length; ++i)
        {
            String row = board[i];
            for(int j =0; j < row.length(); ++j)
            {
                if(row.charAt(j) == 'O')
                {
                    oh++;
                    mat[i][j] = 1;
                }
                else if(row.charAt(j) == 'X')
                {
                    ex++;
                    mat[i][j] = 2;
                }
            }
        }
        
        //둘다 0
        if(oh==0 && ex==0)
            return 1;
        
        //0 >= X
        if(oh<ex)
            return 0;
        
        if(oh-ex > 1)
            return 0;
        
        int xthree = 0;
        int othree = 0;
        for(int i = 0; i < 3; ++i)//행체크
        {
            if(mat[i][0] == mat[i][1] && mat[i][1] == mat[i][2])
            {
                if(1 == mat[i][0])
                    othree++;
                else if(2 == mat[i][0])
                    xthree++;
            }
                
        }
        for(int i = 0; i < 3; ++i)//열체크
        {
            if(mat[0][i] == mat[1][i] && mat[1][i] == mat[2][i])
            {
                if(1 == mat[0][i])
                    othree++;
                else if(2 == mat[0][i])
                    xthree++;
            }
        }
        //대각선체크
        if( mat[0][0] == mat[1][1] && mat[1][1] == mat[2][2] )
        {
            if(1 == mat[0][0])
                othree++;
            else if(2 == mat[0][0])
                xthree++;
        }
        
        if(mat[0][2] == mat[1][1] && mat[1][1] == mat[2][0])
        {
            if(1 == mat[0][2])
                othree++;
            else if(2 == mat[0][2])
                xthree++;
        }
        
        if(othree > 0 && xthree > 0)
            return 0;
        
        if(othree > 0 && ex == oh)
            return 0;
        
        if(xthree > 0 && ex < oh)
            return 0;
        
        return 1;
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글