일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AOP
- dfs
- 추상화
- Secret
- 다형성
- Solid
- 객체지향
- github
- DP
- MariaDB
- git
- package
- 동적계획법
- CSS
- mock
- 서브셋폰트
- bfs
- netlify
- 클라우드
- azure
- npm
- PostgreSQL
- 상속
- 디자인 패턴
- process.env
- GOF
- 메모이제이션
- Java
- dotenv
- 캡슐화
Archives
- Today
- Total
이것저것 해보기🌼
[BOJ] 16985번:Maaaaaaaaaze - 완전탐색(Permutation), BFS, 구현 본문
https://www.acmicpc.net/problem/16985
지난번 배열돌리기 문제와 같이, 배열을 회전시키는 등의 구현과 더불어
순열과 BFS 가 모두 활용되는 좋은 문제여서 공부
좋은 풀이법 : https://gre-eny.tistory.com/321
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import java.util.Scanner;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
public class Main {
public static int [][]A;
public static int [][]B;
public static int [][]C;
public static int [][]D;
public static int [][]E;
public static int [][][]matrix;
public static int []dx = {-1,1,0,0,0,0};
public static int []dy = {0,0,-1,1,0,0};
public static int []dz = {0,0,0,0,-1,1};
public static int min = Integer.MAX_VALUE;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
A = new int[5][5];
B = new int[5][5];
C = new int[5][5];
D = new int[5][5];
E = new int[5][5];
matrix = new int[5][5][5];
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
A[i][j] = sc.nextInt();
}
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
B[i][j] = sc.nextInt();
}
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
C[i][j] = sc.nextInt();
}
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
D[i][j] = sc.nextInt();
}
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
E[i][j] = sc.nextInt();
}
}
int[] arr = new int[5];
arr[0] = 0; arr[1] = 1;arr[2] = 2; arr[3] = 3;arr[4] = 4;
per1(arr, 0, 5);
if(min==Integer.MAX_VALUE) System.out.println("-1");
else System.out.println(min);
sc.close();
}
static void makeMatrix(int a, int b, int c, int d, int e){
for(int i=0; i<5; i++){
System.arraycopy(A[i], 0, matrix[a][i], 0, 5);
}
for(int i=0; i<5; i++){
System.arraycopy(B[i], 0, matrix[b][i], 0, 5);
}
for(int i=0; i<5; i++){
System.arraycopy(C[i], 0, matrix[c][i], 0, 5);
}
for(int i=0; i<5; i++){
System.arraycopy(D[i], 0, matrix[d][i], 0, 5);
}
for(int i=0; i<5; i++){
System.arraycopy(E[i], 0, matrix[e][i], 0, 5);
}
}
static void rotate(int d){ //z축:d, 90도회전
int[][] tmp = new int[5][5];
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
tmp[i][j] = matrix[d][4-j][i];
}
}
for(int i=0; i<5; i++){
System.arraycopy(tmp[i], 0, matrix[d][i], 0, 5);
}
}
static void backtracking(int depth){
if(depth==5){
bfs(0,0,0);
return;
}
backtracking(depth+1);
rotate(depth);
backtracking(depth+1);
rotate(depth);
backtracking(depth+1);
rotate(depth);
backtracking(depth+1);
rotate(depth);
}
static void per1(int[] arr, int depth, int n) {
if(depth == n) {
makeMatrix(arr[0], arr[1], arr[2], arr[3], arr[4]);
backtracking(0);
return;
}
for(int i=depth; i<n; i++) {
swap(arr, depth, i);
per1(arr, depth + 1, n);
swap(arr, depth, i);
}
}
static void swap(int[] arr, int depth, int i) { //두 배열의 값을 바꾸는 Swap 함수
int temp = arr[depth];
arr[depth] = arr[i];
arr[i] = temp;
}
static void bfs(int x, int y, int z){
if(matrix[x][y][z]==0) return ;
if(x>=5 || y>=5 || z>=5 || x<0 || y<0 || z<0) return ;
Queue<Point> q = new ArrayDeque<>();
boolean[][][] visited = new boolean[5][5][5];
q.offer(new Point(x,y,z,0));
visited[x][y][z]=true;
while(!q.isEmpty()){
Point cur = q.poll();
if(cur.x==4 && cur.y==4 && cur.z==4){
min = Math.min(min, cur.len);
return;
}
for(int i=0; i<6; i++){
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
int nz = cur.z + dz[i];
if(isValid(nx, ny, nz) && matrix[nx][ny][nz]==1 && !visited[nx][ny][nz]){
q.offer(new Point(nx,ny,nz, cur.len+1));
visited[nx][ny][nz]=true;
}
}
}
}
static boolean isValid(int x, int y, int z){
if(x>=0 && x<5 && y>=0 && y<5 && z>=0 && z<5) return true;
return false;
}
static class Point{
int x,y,z,len;
Point(int x,int y,int z,int len){
this.x=x;
this.y=y;
this.z=z;
this.len=len;
}
}
}
|
cs |
'코딩테스트' 카테고리의 다른 글
2021 KAKAO BLIND RECRUITMENT (0) | 2022.09.09 |
---|---|
[BOJ] 1931번. 회의실 배정 - Greedy 알고리즘 (0) | 2022.09.09 |
DP (dynamic programming) 연습 (0) | 2022.09.07 |
[프로그래머스] 송전탑 분할 (0) | 2021.07.11 |
[자료구조] List, Map, Set, Stack, Queue 연습 (JAVA) (0) | 2021.07.02 |