알고리즘(종합)/Lv.3

1215 - 회문1

ch.0 2022. 11. 19. 21:29


package test;

import java.io.IOException;
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);

		int test = 10;

		for (int k = 0; k < test; k++) {

			String[][] array = new String[8][8];
			int legth = sc.nextInt();

			// 배열에 데이터 입력
			for (int i = 0; i < 8; i++) {
				String data = sc.next();
				for (int j = 0; j < 8; j++) {
					array[i][j] = data.substring(j, j + 1);
				}

			}

			StringBuilder sb = new StringBuilder();
			String compare1 = "";
			String compare2 = "";
			int count = 0;

			//가로
			for (int x = 0; x < 8; x++) {
				for (int i = 0; i < 9-legth; i++) {
					// 앞에서 n개씩 읽기
					sb.setLength(0);
					for (int j = 0 + i; j < legth + i; j++) {
						compare1 = (sb.append(array[x][j])).toString();

					}
					// 뒤에서 n개씩 읽기
					sb.setLength(0);
					for (int j = (legth-1) + i; j >= i; j--) {
						compare2 = (sb.append(array[x][j])).toString();

					}
					// 비교하기
					if (compare1.equals(compare2)) {
						count++;
					}
				}

			}
			
			//세로
			for (int x = 0; x < 8; x++) {
				for (int i = 0; i < 9-legth; i++) {
					// 위에서 n개씩 읽기
					sb.setLength(0);
					for (int j = 0 + i; j < legth + i; j++) {
						compare1 = (sb.append(array[j][x])).toString();

					}
					// 밑에서 n개씩 읽기
					sb.setLength(0);
					for (int j = (legth-1) + i; j >= i; j--) {
						compare2 = (sb.append(array[j][x])).toString();

					}
					// 비교하기
					if (compare1.equals(compare2)) {
						count++;
					}
				}

			}
			
			System.out.println("#" + (k + 1) + " "+count);
		}
	}

}