2666번: 벽장문의 이동
첫 번째 줄에 벽장의 개수를 나타내는 3보다 크고 20보다 작거나 같은 하나의 정수, 두 번째 줄에 초기에 열려있는 두 개의 벽장을 나타내는 두 개의 정수, 그리고 세 번째 줄에는 사용할 벽장들
www.acmicpc.net
- 생각
N개의 벽장에서 N-2개의 벽장만 문이 닫혀있고 2개는 문이 열려있다.
벽장문의 이동 횟수와, 이동 순서가 주어지는데 해당 이동순서대로 벽장을 이동했을 때 최소로 이동할 수 있는 값을 찾으면 된다.
벽장문이 이동하는 경우의 수는 왼쪽, 오른쪽 2가지이다.
해당 문제는 읽어보면 알겠지만 모든 경우의 수를 돌아봐야하는 브루트포스 문제이다.
나는 dfs 방식을 선택했다.
dfs 방식
- basecase는 모든 index를 돌아서 index가 이동 횟수를 넘었을 때
- 추가적으로 현재 count가 지금까지 모든 경우의 수를 돌았던 count보다 낮을 시 작업을 더이상 하지않는다.
- basecase로 걸러지지 않았을 땐, 왼쪽 오른쪽 모든 경우의수를 돌았다. (왼쪽, 오른쪽을 쉽게 구하기 위해 파라미터로 문이 없는 벽장의 index를 포함시킨다.)
쉽게 구하기 위해
- Math.abs을 활용 했다. (Math.abs는 절대값을 씌워주는 함수이다.)
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class Main {
static int answer, move, cupboard;
static int[] moves;
public static void main(String[] args) throws Exception {
SetData();
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
answer = Integer.MAX_VALUE;
cupboard = in.nextInt();
int first = in.nextInt();
int second = in.nextInt();
move = in.nextInt();
moves = new int[move];
for(int i = 0; i < move; i++) {
moves[i] = in.nextInt();
}
dfs(first, second, 0, 0);
}
private static void dfs(int first, int second, int index, int count) {
// 분기한정
if (count > answer) return;
// basecase
if (index >= move) {
answer = Math.min(answer, count);
return;
}
// left
dfs(moves[index], second, index + 1, count + Math.abs(first - moves[index]));
// right
dfs(first, moves[index], index + 1, count + Math.abs(second - moves[index]));
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 1525번 : 퍼즐 (0) | 2021.03.10 |
---|---|
[BOJ/JAVA] 백준 1963번 : 소수 경로 (0) | 2021.03.08 |
[BOJ/JAVA] 백준 2312번 : 수 복원하기 (0) | 2021.03.03 |
[BOJ/JAVA] 백준 9421번 : 소수상근수 (0) | 2021.03.02 |
[BOJ/JAVA] 백준 17136번 : 색종이 붙이기 (0) | 2021.02.26 |