[Java] 백준 20125 쿠키의 신체 측정

Lee GaEun·2024년 12월 29일

[Java] 알고리즘

목록 보기
39/93

20125 쿠키의 신체 측정 문제 링크

문제



#1

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

public class Main {
    static int N;
    static char[][] arr;

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

        N = Integer.parseInt(br.readLine());
        //StringTokenizer st = new StringTokenizer(br.readLine());;
        String a;
        arr = new char[N][N];
        for(int i=0; i<N; i++) {
            a = br.readLine();
            for(int j=0; j<N; j++) {
                arr[i][j] = a.charAt(j);
            }
        }

        Point heart = whereH();
        int[] arm = countA(heart);
        int waist = countW(heart);
        int[] leg = countL(heart, waist);

        bw.write((heart.x+1)+" "+(heart.y+1)+"\n");
        bw.write(arm[0]+" "+arm[1]+" "+waist+" "+leg[0]+" "+leg[1]);
        bw.flush();
        bw.close();
    }

    static Point whereH() {
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(arr[i][j] == '*') {
                    return new Point(i+1, j);
                }
            }
        }
        return new Point(0, 0);
    }

    static int[] countA(Point heart) {
        int[] count = new int[2];
        int Y = heart.y -1;
        while (Y>=0) {
            if(arr[heart.x][Y] == '*') {
                Y--;
                count[0]++;
            } else break;
        }
        Y = heart.y +1;
        while (Y<N) {
            if(arr[heart.x][Y] == '*') {
                Y++;
                count[1]++;
            } else break;
        }
        return count;
    }
    static int countW(Point heart) {
        int count = 0;
        int X = heart.x +1;
        while (X<N) {
            if(arr[X][heart.y] == '*') {
                X++;
                count++;
            } else break;
        }
        return count;
    }
    static int[] countL(Point heart, int waist) {
        int[] count = new int[2];
        int X = heart.x + waist + 1;
        while (X<N) {
            if(arr[X][heart.y-1] == '*') {
                X++;
                count[0]++;
            } else break;
        }
        X = heart.x + waist + 1;
        while (X<N) {
            if(arr[X][heart.y+1] == '*') {
                X++;
                count[1]++;
            } else break;
        }
        return count;
    }
}

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

0개의 댓글