[Java] SWEA 11315 오목 판정

Lee GaEun·2024년 11월 15일

[Java] 알고리즘

목록 보기
20/93

11315 오목 판정 문제 링크

문제분석

  • N X N 크기의 오목 판
  • 판의 각 칸에는 돌이 있거나 없을 수 있음
  • 돌이 가로, 세로, 대각선 중 하나의 방향으로 다섯 개 이상 연속한 부분이 있는지 없는지 판정하는 프로그램 개발

제약 사항

입력 조건

  • 첫째 줄 : 테스트 케이스 수 ( T <= 10 )
  • 둘째 줄 : 오목판 크기 N(5 ≤ N ≤ 20)
  • 셋째 줄 : 오목판
    • N개의 줄의 각 줄에는 길이 N인 문자열
    • ‘o’는 돌이 있는 칸, ‘.’는 돌이 없는 칸

출력 조건

#부호 + 테스트 케이스 번호 + " " + 5개이상 연속이 있으면 “YES”, 아니면 “NO” 출력

#1

import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            char[][] arr = new char[N][N];
            String a;
            String nun = "NO";
            int countC = 0;
            int countL = 0;
            int countA = 0;
            int countB = 0;

            for(int i=0; i<N; i++) {
                a = sc.next();
                for(int j=0; j<N; j++) {
                    arr[i][j] = a.charAt(j);
                }
            }

            for(int i=0; i<N; i++) {
                countC=0;
                countL=0;
                for(int j=0; j<N; j++) {
                    if(arr[i][j]=='o') countC++;
                    if(arr[j][i]=='o') countL++;
                    if(i==j) {
                        if(arr[i][j]=='o') countA++;
                    }
                    if(i+j == N-1) {
                        if(arr[i][j]=='o') countB++;
                    }
                }
                if(countC==N || countL==N) nun="YES";
            }
            if(countA==N || countB==N) nun="YES";

            System.out.println(nun);
        }
    }
}

#2

  • 애초에 잘못 생각함..
  • 가로, 세로, 대각선에 있는 게 모두 "o"인게 아니라 5개 이상이면 됨
    • 가로, 세로는 else countC=0;해서 연속이 아닐 경우를 체크해줌
    • 대각선 i==j, i+j == N-1 하면 안됨
      • 대각선 중간부터 중간까지 5개가 빙고일 수도 있으니까
    • 각 자리별로 대각선 양쪽 방향으로 빙고가 있는지 탐색해줌
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            char[][] arr = new char[N][N];
            String nun = "NO";
            int countC = 0;
            int countL = 0;

            for(int i=0; i<N; i++) {
                String a = sc.next();
                for(int j=0; j<N; j++) {
                    arr[i][j] = a.charAt(j);
                }
            }

            for(int i=0; i<N; i++) {
                countC=0;
                countL=0;

                for(int j=0; j<N; j++) {
                    if(arr[i][j]=='o') countC++;
                    else countC=0;

                    if(arr[j][i]=='o') countL++;
                    else countL=0;

                    String b = nn(i,j,N,arr);
                    if(b.equals("YES")) nun="YES";

                    if(countC>=5 || countL>=5) nun="YES";
                }
            }
            System.out.println("#" + test_case + " " + nun);
        }
    }
    static String nn(int i, int j, int N, char[][] arr) {
        int countA = 0;
        int countB = 0;

        if(i+4<=N-1 && j+5<N-1) {
            for(int a=0; a<5; a++) {
                if(arr[i+a][j+a] == 'o') countA++;
            }
        }

        if(i+4<=N-1 && j>=4) {
            for(int a=0; a<5; a++) {
                if(arr[i+a][j-a] == 'o') countB++;
            }
        }
        return countA==5 || countB==5 ? "YES" : "NO";
    }
}

#3

  • j+5<N-1 -> j+4<=N-1 수정
  • 코드로 보면 비슷한 코드 같은데 숫자 대입 해보면 다름
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            char[][] arr = new char[N][N];
            String nun = "NO";
            int countC = 0;
            int countL = 0;

            for(int i=0; i<N; i++) {
                String a = sc.next();
                for(int j=0; j<N; j++) {
                    arr[i][j] = a.charAt(j);
                }
            }

            for(int i=0; i<N; i++) {
                countC=0;
                countL=0;

                for(int j=0; j<N; j++) {
                    if(arr[i][j]=='o') countC++;
                    else countC=0;

                    if(arr[j][i]=='o') countL++;
                    else countL=0;

                    String b = nn(i,j,N,arr);
                    if(b.equals("YES")) nun="YES";

                    if(countC>=5 || countL>=5) nun="YES";
                }
            }
            System.out.println("#" + test_case + " " + nun);
        }
    }
    static String nn(int i, int j, int N, char[][] arr) {
        int countA = 0;
        int countB = 0;

        if(i+4<=N-1 && j+4<=N-1) {
            for(int a=0; a<5; a++) {
                if(arr[i+a][j+a] == 'o') countA++;
            }
        }

        if(i+4<=N-1 && j>=4) {
            for(int a=0; a<5; a++) {
                if(arr[i+a][j-a] == 'o') countB++;
            }
        }
        return countA==5 || countB==5 ? "YES" : "NO";
    }
}

  • 성공!
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글