[SWEA] #1215 회문1

KwonSC·2021년 11월 7일
0

SWEA - Java

목록 보기
8/26
post-thumbnail

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14QpAaAAwCFAYi&categoryId=AV14QpAaAAwCFAYi&categoryType=CODE


Code

import java.util.Scanner;

class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        for (int testCase = 1; testCase <= 10; testCase++) {
            int n = sc.nextInt();
            char arr[][] = new char[8][8];
            for (int i = 0; i < 8; i++) {
                String temp = sc.next();
                arr[i] = temp.toCharArray();
            }
            int result = 0;
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j <= 8 - n; j++) {
                    boolean row = true;
                    for (int k = 0; k < n / 2; k++) {
                        if (row && arr[i][j + k] != arr[i][j + n - 1 - k]) {
                            row = false;
                        }
                    }
                    if (row) {
                        result++;
                    }
                }
            }
            for (int i = 0; i <= 8 - n; i++) {
                for (int j = 0; j < 8; j++) {
                    boolean col = true;
                    for (int k = 0; k < n / 2; k++) {
                        if (col && arr[i + k][j] != arr[i + n - 1 - k][j]) {
                            col = false;
                        }
                    }
                    if (col) {
                        result++;
                    }
                }
            }
            System.out.printf("#%d %d\n", testCase, result);
        }
    }
}

Solution

찾아야할 회문의 길이 n을 가지고 가로기준, 세로기준으로 3중 for문을 2번 반복
ex) 예를 들어 가로row 기준이라면 arr[i][j]에서 + k, arr[i][j + n - 1]에서 -k 한것들을 서로 같은지 비교
그후 boolean 값이 true면 result값을 증가시켜 출력

0개의 댓글