SSONG Cloud
[SWEA] 1215 회문1 본문
반응형
: 8x8 글자판에서 제시된 길이를 가진 회문의 총 개수를 구해야 한다.
: 행과 열에서 회문의 개수를 구하는 메소드를 각각 만든다.
: 행과 열의 메소드는 거의 유사하게 작동한다.
: 글자판의 각 글자(left)마다 제시된 길이만큼 떨어진 곳과 같은 글자(right)인지 확인을 하고
만약 같은 글자라면 left+1, right-1을 해서 또 같은 글자인지 확인한다. 만약 right와 left가 같아지거나
right가 left보다 더 작아지면 그 전까지 계속 같은 글자였던것이므로 회문으로 판정되어 회문의 개수(cnt)를
증가시키고 그게 아니라면 메소드 호출을 중단하고 글자판에서 다음 글자에서 또 같은 과정으로 확인한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
// 평면 글자판
static char[][] words = new char[8][8];
static int N;
static int cnt;
public static void main(String[] args) throws NumberFormatException, IOException {
for(int tc= 1; tc <= 10; tc++) {
// 찾아야 하는 회문 길이
N = Integer.parseInt(br.readLine());
cnt = 0;
for(int i = 0; i < 8; i++)
words[i] = br.readLine().toCharArray();
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
rowCheck(i, j, j+N-1);
colCheck(i, j, j+N-1);
}
}
sb.append(String.format("#%d %d\n", tc, cnt));
}
System.out.println(sb);
}
// 행 회문 검사
static void rowCheck(int row, int left, int right) {
if(left < 0 || right > 7 || words[row][left] != words[row][right])
return;
if(right - left <= 0) {
cnt++;
return;
}
if(words[row][left] == words[row][right])
rowCheck(row, left+1, right-1);
}
// 열 회문 검사
static void colCheck(int col, int top, int bottom) {
if(top < 0 || bottom > 7 || words[bottom][col] != words[top][col])
return;
if(bottom - top <= 0) {
cnt++;
return;
}
if(words[bottom][col] == words[top][col])
colCheck(col, top+1, bottom-1);
}
}
반응형
'Algorithm > SW Expert Academy' 카테고리의 다른 글
[SWEA] 10032 과자 분배 (0) | 2021.02.17 |
---|---|
[SWEA] 1230 암호문3 (0) | 2021.02.17 |
[SWEA] 3142 영준이와 신비한 뿔의 숲 (0) | 2021.02.14 |
[SWEA] 5356 의석이의 세로로 말해요 (0) | 2021.02.12 |
[SWEA] 7087 문제 제목 붙이기 (0) | 2021.02.12 |
Comments