알고리즘/삼성 SW expert Academy

[SWEA] 11315. 오목 판정_JAVA

뇌장하드 2022. 5. 20. 00:31

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AXaSUPYqPYMDFASQ 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

import java.util.Scanner;
  
class Solution {
    static int[] dx = {1, -1, 0, 0, 1, 1};
    static int[] dy = {0, 0, 1, -1, 1, -1};
    static int n;
    static char[][] board;
    static boolean find;
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
  
        int T = sc.nextInt();
  
        for (int t = 1; t <= T ; t++) {
            n = sc.nextInt();
  
            board= new char[n][n];
  
            for (int i = 0; i < n; i++) {
                String line = sc.next();
                for (int j = 0; j < n; j++) {
                    board[i][j] = line.charAt(j);
                }
            }
  
            find = false;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
  
                    if (board[i][j] != 'o') continue;
                    if (find) break;
  
                    for (int k = 0; k < 6; k++) {
                        dfs(i, j, 0, dx[k], dy[k]);
                        if (find) break;
                    }
                }
            }
  
            System.out.println("#" + t + " " + (find ? "YES" : "NO"));
        }
    }
  
    static void dfs(int x, int y, int depth, int movex, int movey){
  
        if(depth == 4 && board[x][y] == 'o'){
            find = true;
            return;
        }
  
        int nx = x + movex;
        int ny = y + movey;
  
        if(nx < 0 || nx >= n || ny < 0 || ny >= n) return;
        if(board[nx][ny] != 'o') return;
  
        dfs(nx, ny,depth+1, movex, movey);
    }
}