성능 요약
메모리: 18016 KB, 시간: 340 ms
분류
다이나믹 프로그래밍(dp), 그래프 이론(graphs), 그래프 탐색(graph_traversal)
문제 설명
유현이가 새 집으로 이사했다. 새 집의 크기는 N×N의 격자판으로 나타낼 수 있고, 1×1크기의 정사각형 칸으로 나누어져 있다. 각각의 칸은 (r, c)로 나타낼 수 있다. 여기서 r은 행의 번호, c는 열의 번호이고, 행과 열의 번호는 1부터 시작한다. 각각의 칸은 빈 칸이거나 벽이다.
오늘은 집 수리를 위해서 파이프 하나를 옮기려고 한다. 파이프는 아래와 같은 형태이고, 2개의 연속된 칸을 차지하는 크기이다.
파이프는 회전시킬 수 있으며, 아래와 같이 3가지 방향이 가능하다.
파이프는 매우 무겁기 때문에, 유현이는 파이프를 밀어서 이동시키려고 한다. 벽에는 새로운 벽지를 발랐기 때문에, 파이프가 벽을 긁으면 안 된다. 즉, 파이프는 항상 빈 칸만 차지해야 한다.
파이프를 밀 수 있는 방향은 총 3가지가 있으며, →, ↘, ↓ 방향이다. 파이프는 밀면서 회전시킬 수 있다. 회전은 45도만 회전시킬 수 있으며, 미는 방향은 오른쪽, 아래, 또는 오른쪽 아래 대각선 방향이어야 한다.
파이프가 가로로 놓여진 경우에 가능한 이동 방법은 총 2가지, 세로로 놓여진 경우에는 2가지, 대각선 방향으로 놓여진 경우에는 3가지가 있다.
아래 그림은 파이프가 놓여진 방향에 따라서 이동할 수 있는 방법을 모두 나타낸 것이고, 꼭 빈 칸이어야 하는 곳은 색으로 표시되어져 있다.
가로
세로
대각선
가장 처음에 파이프는 (1, 1)와 (1, 2)를 차지하고 있고, 방향은 가로이다. 파이프의 한쪽 끝을 (N, N)로 이동시키는 방법의 개수를 구해보자.
입력
첫째 줄에 집의 크기 N(3 ≤ N ≤ 16)이 주어진다. 둘째 줄부터 N개의 줄에는 집의 상태가 주어진다. 빈 칸은 0, 벽은 1로 주어진다. (1, 1)과 (1, 2)는 항상 빈 칸이다.
출력
첫째 줄에 파이프의 한쪽 끝을 (N, N)으로 이동시키는 방법의 수를 출력한다. 이동시킬 수 없는 경우에는 0을 출력한다. 방법의 수는 항상 1,000,000보다 작거나 같다.
가로, 세로, 대각선에 따라 움직일 수 있는 방향이 다르고, 또 움직이는 방향마다 고려해야 할 벽의 개수가 다르다.
1. 가로
- 가로, 대각선으로 이동 가능
2. 세로
- 세로, 대각선으로 이동 가능
3. 대각선
- 가로, 세로, 대각선으로 이동 가능
그리고 가로, 세로는 해당 방향이 벽이 아닌지만 검사하면 되지만, 대각선의 경우 해당 방향과 그 방향의 위, 왼쪽도 검사해야 한다.
이 경우를 고려해서 dr, dc 배열을 각 방향에 따라 이동할 수 있는 방향으로 만들었다.
index가 0일 때는 가로, 1은 세로, 2는 대각선을 의미한다.
static int[][] dr = { { 0, 1 }, { 1, 1 }, { 1, 1, 0 } };
static int[][] dc = { { 1, 1 }, { 0, 1 }, { 1, 0, 1 } }; // 가로 세로 대각선 이동 위치
그리고 dfs를 통해 이동 가능한 경로를 모두 탐색한다.
N-1, N-1 에 도착한 경우 cnt를 더해주고 함수를 종료한다.
이동할 때에도 이동 방향에 따라 벽의 유무를 검사하며 dfs를 계속하였다.
static void dfs(int r, int c, int dir) {
// 0은 가로, 1은 세로 , 2는 대각선
if (r == N - 1 && c == N - 1) {
cnt++;
return;
}
for (int i = 0; i < dr[dir].length; i++) {
int nr = r + dr[dir][i];
int nc = c + dc[dir][i];
if (nr < N && nc < N) {
// 대각선
if (dr[dir][i] == 1 && dc[dir][i] == 1) {
if (map[nr][nc] == 0 && map[nr - 1][nc] == 0 && map[nr][nc - 1] == 0) {
dfs(nr, nc, 2);
}
}
// 가로 세로
else if (map[nr][nc] == 0) {
if (dr[dir][i] == 1)
dfs(nr, nc, 1);
// 가로
else
dfs(nr, nc, 0);
// 세로
}
}
}
}
다른 코드를 참조하니까 좀 더 간단하게 구현할 수도 있을 것 같다!
전체 코드
package Gold.g5;
import java.util.Scanner;
public class BOJ_17070_파이프옮기기 {
static int[][] map;
static int[][] dr = { { 0, 1 }, { 1, 1 }, { 1, 1, 0 } };
static int[][] dc = { { 1, 1 }, { 0, 1 }, { 1, 0, 1 } }; // 가로 세로 대각선 이동 위치
static int N;
static int cnt;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
map = new int[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
map[i][j] = sc.nextInt();
dfs(0, 1, 0);
System.out.println(cnt);
}
static void dfs(int r, int c, int dir) {
// 0은 가로, 1은 세로 , 2는 대각선
if (r == N - 1 && c == N - 1) {
cnt++;
return;
}
for (int i = 0; i < dr[dir].length; i++) {
int nr = r + dr[dir][i];
int nc = c + dc[dir][i];
if (nr < N && nc < N) {
// 대각선
if (dr[dir][i] == 1 && dc[dir][i] == 1) {
if (map[nr][nc] == 0 && map[nr - 1][nc] == 0 && map[nr][nc - 1] == 0) {
dfs(nr, nc, 2);
}
}
// 가로 세로
else if (map[nr][nc] == 0) {
if (dr[dir][i] == 1)
dfs(nr, nc, 1);
// 가로
else
dfs(nr, nc, 0);
// 세로
}
}
}
}
}
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 11444 - 피보나치 수 6 (1) | 2022.09.20 |
---|---|
[백준] 2630 - 색종이 만들기 (자바/Java) (0) | 2022.09.20 |
[백준] 10830 - 행렬제곱 (자바/Java) (1) | 2022.09.20 |
[백준] 1074 - Z (자바/Java) (0) | 2022.09.20 |
[백준] 10972 - 다음 순열 (자바/Java) (0) | 2022.09.20 |