SSAFY/SWEA

[SWEA] 1215 : 회문 1

믕비 2023. 4. 26. 21:14
반응형

https://swexpertacademy.com/main/talk/solvingClub/problemView.do?solveclubId=AV6kld8aisgDFASb&contestProbId=AV14QpAaAAwCFAYi&probBoxId=AV-4MojKLNADFATz&type=PROBLEM&problemBoxTitle=%5BD2~D3+%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4%5D+%EA%B8%B0%EC%B4%88+%EB%8B%A4%EC%A7%80%EA%B8%B0+Part4&problemBoxCnt=14 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

완전탐색으로 풀었음.

 

내 코드

메모리 : 18276 KB

시간 : 99 ms

코드길이 : 1700 B

 

[내 코드]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
	static int length;
	static char[][] graph;

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;
		
		for(int t = 1; t <= 10; t++) {
			sb.append('#').append(t).append(' ');
			
			length = Integer.parseInt(br.readLine());
			graph = new char[8][8];
			int result = 0;
			
			for(int r = 0; r < 8; r++) {
				String row = br.readLine();
				for(int c = 0; c < 8; c++) {
					graph[r][c] = row.charAt(c);
				}
			}
			
			for(int i = 0; i < 8; i++) {
				for(int j = 0; j < 8; j++) {
					result = palindrome_hori(i, j) ? result + 1 : result;
					result = palindrome_verti(i, j) ? result + 1 : result;
				}
			}
			sb.append(result).append('\n');
		}
		System.out.print(sb);

	}
	
	public static boolean palindrome_hori(int startRow, int startColumn) {
		//가로로 찾기
		//범위 내일 때 
		if(startColumn + (length - 1) < 8) {
			for(int l = 0; l < length / 2; l++) {
				//다르면
				if(graph[startRow][startColumn + l] != graph[startRow][startColumn + (length - 1) - l])
					return false;
			}
			return true;
		}
		
		return false;
	}
	
	public static boolean palindrome_verti(int startRow, int startColumn) {
		//세로로 찾기
		//범위 내일 때 
		if(startRow + (length - 1) < 8) {
			for(int l = 0; l < length / 2; l++) {
				//다르면
				if(graph[startRow + l][startColumn] != graph[startRow + (length - 1) - l][startColumn])
					return false;
			}
			return true;
		}
		
		return false;
	}

}

 

반응형

'SSAFY > SWEA' 카테고리의 다른 글

[SWEA] 1216 : 회문 2  (0) 2023.04.27
[SWEA] 1244 : 최대 상금  (0) 2023.04.26
[SWEA] 1230 : 암호문 3  (0) 2023.04.26
[SWEA] 1229 : 암호문 2  (0) 2023.04.26
[SWEA] 1228 : 암호문 1  (0) 2023.04.26