Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 입실 퇴실
- 문서자동화
- openssl
- 위클리 챌린지
- ElementTree
- 그래프
- 좋은 수열
- 10597
- 백준
- 백트렉킹
- BOJ
- 1174
- BFS
- 부분 수열의 합
- 백트래킹
- 프로그래머스
- 줄어드는 숫자
- Java
- 완전 탐색
- 백트랙킹
- 복서 정렬하기
- 순열장난
- 너비우선탐색
- 39080
- 위클리 6주차
- 코딩테스트
- 재귀
- 몯느 순열
- DP
- dfs
Archives
개발자-H 입니다.
BOJ - N과 M (1) 본문
https://www.acmicpc.net/problem/15649
15649번: N과 M (1)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
- 단계별 풀어보기 - 백트렉킹 문제이다.
- https://www.acmicpc.net/step/34
- 시간초과가 엄청나게 나서 자바에서 최적화 관련으로 열심히 뒤져서 겨우 통과 했다.
- 최적화 단계
- DFS 함수 콜 3 -> 2 으로 변경
- System.out.printf -> System.out.print 변경
- System.out.print -> StringBuilder 마지막 한번 출력으로 변경
- 통과 코드
package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static int N;
public static int M;
public static StringBuilder builder = new StringBuilder();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
ArrayList<Integer> numbers = new ArrayList<>();
boolean[] visited = new boolean[N + 1];
dfs(numbers, visited, 0);
System.out.println(builder);
}
private static void dfs(ArrayList<Integer> numbers, boolean[] visited, int depth) {
if (depth == M) {
for (Integer no : numbers) {
builder.append(no + " ");
}
builder.append("\n");
return;
}
for (int i = 1; i <= N; i++) {
if (visited[i] == false) {
Integer next = i;
visited[i] = true;
numbers.add(next);
dfs(numbers, visited, depth + 1);
visited[next] = false;
numbers.remove(next);
}
}
}
}
- 시간 초과 났던 처음에 만든 코드
import java.util.*;
public class Main {
public static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception {
int N = scanner.nextInt();
int M = scanner.nextInt();
dfs(N, M);
}
private static void dfs(int n, int m) {
for (int i = 1; i <= n; i++) {
ArrayList<Integer> numbers = new ArrayList<>();
boolean[] visited = new boolean[n + 1];
dfs(i, n, m, numbers, visited, 0);
}
}
private static void dfs(int current, int n, int m, ArrayList<Integer> numbers, boolean[] visited, int depth) {
if (depth == m) {
return;
}
if (visited[current] == true) return;
Integer number = current;
visited[current] = true;
numbers.add(number);
if(numbers.size() == m) {
for(Integer no : numbers) {
System.out.printf("%d ", no);
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
if (visited[i] == true) continue;
dfs(i, n, m, numbers, visited, depth + 1);
}
visited[current] = false;
numbers.remove(number);
}
}
'Algorithm > 문제 풀이' 카테고리의 다른 글
BOJ - N과 M (3) (0) | 2021.08.11 |
---|---|
BOJ - N과 M (2) (0) | 2021.08.11 |
BOJ - 숨바꼭질 (1697) (0) | 2021.08.09 |
BOJ - 미로 탐색 (2178) (0) | 2021.08.09 |
BOJ - 바이러스 (0) | 2021.08.08 |
Comments