1. 문제

2615번: 오목



2. 개요

19 x 19 바둑판에서 오목을 했다.

바둑판의 상태가 주어졌을 때, 승자 및 오목의 첫번째 바둑알의 위치 (가장 좌상단)를 출력하는 문제.



3. 풀이 및 코드

3-1. 풀이

좌상단부터 바둑판을 살펴보면서 바둑알이 있는 위치에서 가로, 세로, 우상향 대각선, 우하향 대각선의 4방향을 탐색한다. 이 때, 육목인 경우에는 오목으로 치지 않으므로 이를 무시할 수 있도록 해준다.

네 방향 중 어떤 한 방향으로라도 오목이 존재한다면 탐색을 시작한 위치의 바둑알이 가장 좌상단에 위치한 바둑알이 되므로 이 위치를 출력하고 프로그램을 종료한다.

모든 위치에 대해 탐색을 끝냈으나 오목이 존재하지 않는 경우 승자가 없으므로 0을 출력한다.

3-2. Python

import sys

board = []
answer = [0, 0, 0]

for i in range(19):
    board.append(list(map(int, sys.stdin.readline().split())))

def chkver(y, x, color):
    if y > 14:
        return False

    for i in range(5):
        if board[y + i][x] != color:
            return False

    if y < 14 and board[y + 5][x] == color:
        return False

    if y > 0 and board[y - 1][x] == color:
        return False

    return True

def chkhor(y, x, color):
    if x > 14:
        return False

    for i in range(5):
        if board[y][x + i] != color:
            return False

    if x < 14 and board[y][x + 5] == color:
        return False

    if x > 0 and board[y][x - 1] == color:
        return False

    return True

def chkdiagonal_rd(y, x, color):
    if y > 14 or x > 14:
        return False

    for i in range(5):
        if board[y + i][x + i] != color:
            return False

    if y < 14 and x < 14 and board[y + 5][x + 5] == color:
        return False

    if y > 0 and x > 0 and board[y - 1][x - 1] == color:
        return False

    return True

def chkdiagonal_ru(y, x, color):
    if y < 4 or x > 14:
        return False

    for i in range(5):
        if board[y - i][x + i] != color:
            return False

    if y > 4 and x < 14 and board[y - 5][x + 5] == color:
        return False

    if y < 18 and x > 0 and board[y + 1][x - 1] == color:
        return False

    return True

def chk(y, x, color):
    if chkver(y, x, color) or chkhor(y, x, color) or chkdiagonal_rd(y, x, color) or chkdiagonal_ru(y, x, color):
        return [color, y, x]

    return [0, 0, 0]

for i in range(19):
    for j in range(19):
        if board[i][j] != 0:
            answer = chk(i, j, board[i][j])

        if answer[0] != 0:
            print(answer[0])
            print(answer[1] + 1, answer[2] + 1)
            sys.exit()

print(answer[0])

3-3. C#

namespace boj_2615
{
    internal class Program
    {
        static int[,] board;

        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            board = new int[19, 19];
            int[] answer = new int[3];

            for (int i = 0; i < 19; i++)
            {
                string[] boardrow = sr.ReadLine().Split();

                for (int j = 0; j < 19; j++)
                {
                    board[i, j] = int.Parse(boardrow[j]);
                }
            }

            for (int i = 0; i < 19; i++)
            {
                for (int j = 0; j < 19; j++)
                {
                    if (board[i, j] != 0)
                        answer = chk(i, j, board[i, j]);

                    if (answer[0] != 0)
                    {
                        sw.WriteLine(answer[0]);
                        sw.WriteLine($"{answer[1] + 1} {answer[2] + 1}");
                        sw.Close();
                        Environment.Exit(0);
                    }
                }
            }

            sw.WriteLine(answer[0]);
            sw.Close();
        }

        static bool chkhor(int y, int x, int color)
        {
            if (y > 14)
                return false;

            for (int i = 0; i < 5; i++)
            {
                if (board[y + i, x] != color)
                    return false;
            }

            if (y < 14 && board[y + 5, x] == color)
                return false;

            if (y > 0 && board[y - 1, x] == color)
                return false;

            return true;
        }

        static bool chkver(int y, int x, int color)
        {
            if (x > 14)
                return false;

            for (int i = 0; i < 5; i++)
            {
                if (board[y, x + i] != color)
                    return false;
            }

            if (x < 14 && board[y, x + 5] == color)
                return false;

            if (x > 0 && board[y, x - 1] == color)
                return false;

            return true;
        }

        static bool chkdiagonal_rd(int y, int x, int color)
        {
            if (y > 14 || x > 14)
                return false;

            for (int i = 0; i < 5; i++)
            {
                if (board[y + i, x + i] != color)
                    return false;
            }

            if (y < 14 && x < 14 && board[y + 5, x + 5] == color)
                return false;

            if (y > 0 && x > 0 && board[y - 1, x - 1] == color)
                return false;

            return true;
        }

        static bool chkdiagonal_ru(int y, int x, int color)
        {
            if (y < 4 || x > 14)
                return false;

            for (int i = 0; i < 5; i++)
            {
                if (board[y - i, x + i] != color)
                    return false;
            }

            if (y > 4 && x < 14 && board[y - 5, x + 5] == color)
                return false;

            if (y < 18 && x > 0 && board[y + 1, x - 1] == color)
                return false;

            return true;
        }

        static int[] chk(int y, int x, int color)
        {
            if (chkhor(y, x, color) || chkver(y, x, color) || chkdiagonal_rd(y, x, color) || chkdiagonal_ru(y, x, color))
                return new int[] { color, y, x };

            return new int[3];
        }
    }
}

Comments