Lined Notebook

[1215] [Java] 회문 1

by HeshAlgo

<회문 1>

문제 설명

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14QpAaAAwCFAYi

 

내 생각

반복문의 조건을 짜는데 시간을 많이 잡아먹었습니다. 역시 아직 부족한 부분이 많은 것 같습니다ㅠㅠ. 반복문의 조건만 잘 짤 수 있었다면 문제는 크게 어렵진 않았습니다. 

 

푼 시간

57분 47초

 

작성 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.FileInputStream;
import java.util.*;
 
public class E1215 {
 
    public static void main(String args[]) throws Exception {
        String path = "Input Path 경로 설정";
        System.setIn(new FileInputStream(path));
        Scanner sc = new Scanner(System.in);
    
        for(int test_case = 1; test_case <= 10; test_case++) {
            int T = sc.nextInt();
            int[][] array = new int[8][8];
 
            for (int i = 0; i < 8; i++) {
                String str = sc.next();
                for (int j = 0; j < 8; j++) {
                    array[i][j] = str.charAt(j);
                }
            }
            int result = 0;
            // 가로
            for (int i = 0; i < 8; i++) {        
                for (int j = 0; j < 8-T+1; j++) {    
                    int cnt = 0;
                    for (int k = 0; k < T/2; k++) {
                        if (array[i][j+k] != array[i][j+T-k-1]) {
                            cnt = -1;
                        }
                    }
                    
                    if (cnt == 0) {
                        result++;
                    }
                    
                }
            }
            
            // 세로
            for (int i = 0; i < 8-T+1; i++) {        
                for (int j = 0; j < 8; j++) {    
                    int cnt = 0;
                    for (int k = 0; k < T/2; k++) {
                        if (array[i+k][j] != array[i+T-k-1][j]) {
                            cnt = -1;
                        }
                    }
                    
                    if (cnt == 0) {
                        result++;
                    }
                    
                }
            }
            
            System.out.println("#" + test_case + " " + result);
        }
        
    }
    
}
 
 

 

실행 결과

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기