회문1
in Algorithm on SW Expert Academy
Software Expert Academy [1215] 회문1
풀이
가로 방향, 세로 방향으로 회문을 탐색한다.
한번 정해진 글자 수만큼 탐색한 후엔 이미 탐색한 구간을 재 활용하기 위해 이전에 탐색한 첫번째 문자열을 제거하고 다음 문자열을 넣어 회문인지 확인한다.
소스코드
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
private static int length;
private static String[][] map;
private static int result;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int tc = 1; tc <= 10; tc++) {
length = sc.nextInt();
result = 0;
map = new String[8][8];
for (int i = 0; i < 8; i++) {
String str = sc.next();
map[i] = str.split("");
}
findPalindrome(true);
findPalindrome(false);
System.out.println("#" + tc + " " + result);
}
}
private static void findPalindrome(boolean isRightCheck) {
List<String> strList = new ArrayList<>();
for (int i = 0; i < 8; i++) {
int idx = length - 1;
for (int j = 0; j < 8; j++) {
if (isRightCheck) {
strList.add(map[i][j]);
} else {
strList.add(map[j][i]);
}
if (strList.size() == length) {
if (isPalindrome(strList)) result++;
strList.remove(0);
j = idx++;
}
}
strList.clear();
}
}
private static boolean isPalindrome(List<String> strList) {
int strLength = strList.size();
for (int i = 0; i < strLength / 2; i++) {
String checkStr1 = strList.get(i);
String checkStr2 = strList.get(strLength - 1 - i);
if (!checkStr1.equals(checkStr2)) return false;
}
return true;
}
}