inblog logo
|
LHS's Study Space
    알고리즘문제풀기랜덤마라톤

    [랜덤 마라톤] Snowflakes(26498)

    lhs's avatar
    lhs
    Nov 13, 2024
    [랜덤 마라톤] Snowflakes(26498)
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리
    www.acmicpc.net
    https://www.acmicpc.net/problem/26498
    notion image
    notion image

    1. 문제 풀이 아이디어

    • for문을 사용해 대칭인지 비교만 해주면 간단하게 풀 수 있다.

    2. 나의 정답 코드

    public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StringBuilder stringBuilder = new StringBuilder(); int n = Integer.parseInt(bufferedReader.readLine()); for (int i = 0; i < n; i++) { int k = Integer.parseInt(bufferedReader.readLine()); int[][] flower = new int[k][k]; for (int x = 0; x < k; x++) { String s = bufferedReader.readLine(); for (int y = 0; y < k; y++) { flower[x][y] = Character.getNumericValue(s.charAt(y)); } } boolean horisontal = true, vertical = true; for (int x = 0; x < k; x++) { for (int y = 0; y < k / 2; y++) { if (flower[x][y] != flower[x][k - 1 - y]) { vertical = false; x = k; break; } } } for (int y = 0; y < k; y++) { for (int x = 0; x < k / 2; x++) { if (flower[x][y] != flower[k - 1 - x][y]) { horisontal = false; y=k; break; } } } if (horisontal && vertical) stringBuilder.append("Magnificent\n"); else if (horisontal) stringBuilder.append("Beautiful\n"); else if (vertical) stringBuilder.append("Graceful\n"); else stringBuilder.append("Useless\n"); } System.out.print(stringBuilder); bufferedReader.close(); } }
     

    3. 정리

    • for문을 사용해 배열 크기의 절반만큼 반복한다.
    • 현재 인덱스의 배열 값과, 배열의 끝에서 현재 인덱스를 뺀 값에 해당하는 배열 값을 비교한다.
    • 두 값이 다르면 각각의 boolean 값을 false로 설정하고, 반복문을 종료한다.
    • horisontal과 vertical의 값에 따라 결과를 출력한다.
    Share article

    LHS's Study Space

    RSS·Powered by Inblog