반응형
문제출처
https://www.acmicpc.net/problem/2661
소스코드
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 | import java.io.BufferedReader; import java.io.InputStreamReader; // BOJ_2661 public class Main { private static int n; private static boolean stopFlag = false; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); dfs(1, "1"); br.close(); } private static boolean isSafe(String str) { int len = str.length(); int repeatCnt = len / 2; int start = len - 1; int end = len; for (int i = 1; i <= repeatCnt; i++) { String first = str.substring(start - i, end - i); String second = str.substring(start, end); if (first.equals(second)) { return false; } start--; } return true; } private static void dfs(int len, String str) { if (stopFlag || !isSafe(str)) { return; } if (len == n) { stopFlag = true; System.out.println(str); return; } for (int i = 1; i <= 3; i++) { dfs(len + 1, str + i); } } } | cs |
반응형
'Algorithm' 카테고리의 다른 글
[BOJ 10039] 평균점수 (0) | 2019.01.22 |
---|---|
[BOJ 2468] 안전영역 (0) | 2019.01.22 |
[BOJ 1759] 암호만들기 (0) | 2019.01.21 |
[BOJ 1012] 유기농 배추 (0) | 2019.01.21 |
[BOJ 1966] 프린터큐 (0) | 2019.01.17 |