본문 바로가기
Language/Java

[백준 | Java] 2615번 오목

by ㅇ달빛천사ㅇ 2025. 2. 7.
728x90
2615번 / 오목

오목

🏷️ 관련 주제 : Brute Force




💦 나의 시도

완전탐색 시도

테스트 케이스로 주어진 문제는 정답이 출력되는 것을 확인했으나 제출 실패ㅜㅜ

import java.io.*;
import java.util.*;

public class Main {
    public static int[][] board = new int[19][19];
    public static int[][] directions = {
        {-1, 1},
        {0, 1},
        {1, 1},
        {1, 0}
    };

    public static boolean win = false;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        for (int i = 0; i < 19; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());

            for (int j = 0; j < 19; j++) {
                board[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        for (int i = 0; i < 19 && !win; i++) {
            for (int j = 0; j < 19 && !win; j++) {
                int color = board[i][j];
                int[] curIdx = {i, j};
                int[] startIdx = {i + 1, j + 1};

                if (color != 0) {
                    for (int[] dir : directions) {
                        dfs(color, startIdx, curIdx, dir, 1);

                        if (win) {
                            return;
                        }
                    }
                }
            }
        }

        System.out.println(0);

    }

    public static void dfs (int color, int[] startIdx, int[] curIdx, int[] dir, int cnt) {
        if (!(curIdx[0] >= 0 && curIdx[0] < 19 && curIdx[1] >= 0 && curIdx[1] < 19)) {
            return;
        }

        System.out.println("curIdx: {"+curIdx[0]+", "+curIdx[1]+"} , dir:"+Arrays.toString(dir)+", cnt:"+cnt);
        int curColor = board[curIdx[0]][curIdx[1]];

        if (curColor == 0) {
            return;
        }

        if (curColor == color) {
            cnt++;

            if (cnt > 5) {
                return;
            }

            int[] nextIdx = {curIdx[0] + dir[0], curIdx[1] + dir[1]};

            if (nextIdx[0] >= 0 && nextIdx[0] < 19 && nextIdx[1] >= 0 && nextIdx[1] < 19) {
                if (board[nextIdx[0]][nextIdx[1]] == curColor) {
                    dfs(color, startIdx, nextIdx, dir, cnt);
                }
            }


            if (cnt == 5) {
                System.out.println(color);
                System.out.println(startIdx[0] + " " + startIdx[1]);
                win = true;
                return;
            }
        }
    }
}



📑제출 기록 및 오답 원인



💯 해결 방법

(미완료)

728x90