Skip to content

Commit 3eddb76

Browse files
committed
[DFS] swea-2105
1 parent c36e236 commit 3eddb76

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
| 13 | | [Baekjoon-3109 빵집](./src/DFS/P3109) | |
103103
| 14 | | [Baekjoon-1987 알파벳](./src/DFS/P1987) | |
104104
| 15 | | [Baekjoon-17070 파이프 옮기기 1](./src/DFS/P17070) | 삼성 A형 기출 |
105+
| 16 | | [SWEA-2105 디저트 카페](./src/DFS/swea2105) | |
105106

106107
### BFS
107108

src/DFS/swea2105/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## [SW Expert Academy - 2105] 디저트 카페
2+
3+
![image](https://user-images.githubusercontent.com/22045163/109147298-3148d580-77a8-11eb-81a2-8649fe72c0ea.png)
4+
![image](https://user-images.githubusercontent.com/22045163/109147501-6c4b0900-77a8-11eb-801c-346348805f40.png)
5+
![image](https://user-images.githubusercontent.com/22045163/109147563-7c62e880-77a8-11eb-872c-e20313b7cc23.png)
6+
![image](https://user-images.githubusercontent.com/22045163/109147604-88e74100-77a8-11eb-8a6d-b0835eb0064a.png)
7+
8+
![image](https://user-images.githubusercontent.com/22045163/109147661-9997b700-77a8-11eb-9c81-bc8e89cb5c95.png)

src/DFS/swea2105/Solution.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package DFS.swea2105;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Solution {
9+
10+
static int T, N, ans;
11+
static int[][] map;
12+
static boolean[] eat;
13+
14+
static int[] di = {1, 1, -1, -1};
15+
static int[] dj = {1, -1, -1, 1};
16+
17+
public static void main(String[] args) throws Exception {
18+
System.setIn(new FileInputStream("src/DFS/swea2105/sample_input.txt"));
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
StringBuilder sb = new StringBuilder();
21+
22+
T = Integer.parseInt(br.readLine());
23+
for (int t = 1; t <= T; t++) {
24+
N = Integer.parseInt(br.readLine());
25+
map = new int[N][N];
26+
27+
for (int i = 0; i < N; i++) {
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
for (int j = 0; j < N; j++) map[i][j] = Integer.parseInt(st.nextToken());
30+
}
31+
32+
ans = -1;
33+
eat = new boolean[101];
34+
for (int i = 0; i < N-2; i++) {
35+
for (int j = 1; j < N-1; j++) {
36+
eat[map[i][j]] = true;
37+
dfs(i, j, i, j,0, 1);
38+
eat[map[i][j]] = false;
39+
}
40+
}
41+
42+
sb.append("#").append(t).append(" ").append(ans).append("\n");
43+
}
44+
System.out.println(sb.toString());
45+
}
46+
47+
static void dfs(int si, int sj, int i, int j, int dir, int sum) {
48+
for (int d = dir; d <= dir+1; d++) {
49+
if (d > 3) continue;
50+
int to_i = i + di[d];
51+
int to_j = j + dj[d];
52+
53+
if (si == to_i && sj == to_j) {
54+
ans = Math.max(ans, sum);
55+
return;
56+
}
57+
58+
if (isValidPath(to_i, to_j) && !eat[map[to_i][to_j]]) {
59+
eat[map[to_i][to_j]] = true;
60+
dfs(si, sj, to_i, to_j, d, sum+1);
61+
eat[map[to_i][to_j]] = false;
62+
}
63+
}
64+
}
65+
66+
static boolean isValidPath(int i, int j) {
67+
return 0 <= i && i < N && 0 <= j && j < N;
68+
}
69+
}

src/DFS/swea2105/sample_input.txt

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
10
2+
4
3+
9 8 9 8
4+
4 6 9 4
5+
8 7 7 8
6+
4 5 3 5
7+
5
8+
8 2 9 6 6
9+
1 9 3 3 4
10+
8 2 3 3 6
11+
4 3 4 4 9
12+
7 4 6 3 5
13+
6
14+
1 8 9 6 3 9
15+
5 3 1 9 8 2
16+
6 9 3 4 1 2
17+
7 1 1 5 1 9
18+
1 9 6 8 7 3
19+
7 6 4 5 5 5
20+
7
21+
7 4 1 5 1 7 9
22+
9 4 6 1 4 6 8
23+
9 6 4 8 4 7 4
24+
3 2 6 2 4 2 8
25+
4 9 4 6 2 4 7
26+
1 7 6 8 9 5 8
27+
1 9 4 7 2 9 7
28+
8
29+
18 18 7 16 15 3 5 6
30+
3 6 8 3 15 13 15 2
31+
4 1 11 17 3 4 3 17
32+
16 2 18 10 2 3 11 12
33+
11 17 16 2 9 16 5 4
34+
17 7 6 16 16 11 15 8
35+
2 1 7 18 12 11 6 2
36+
13 12 12 15 9 11 12 18
37+
9
38+
12 3 10 8 11 12 5 3 11
39+
8 6 4 9 8 2 4 7 6
40+
6 10 12 8 3 8 11 3 3
41+
6 10 5 5 5 11 8 10 2
42+
5 13 3 7 5 6 5 12 6
43+
6 1 5 4 4 13 8 7 2
44+
12 7 13 3 5 1 11 7 3
45+
13 12 7 5 6 12 12 9 6
46+
1 12 13 13 11 3 4 10 9
47+
10
48+
18 8 21 24 8 4 20 15 14 23
49+
17 22 3 14 3 19 19 7 6 13
50+
2 26 10 10 10 7 18 14 15 17
51+
13 25 7 20 18 21 8 2 4 24
52+
4 3 1 5 15 3 15 12 22 23
53+
19 22 9 17 6 9 22 26 2 5
54+
12 13 19 13 6 2 12 19 24 8
55+
21 21 24 15 4 1 20 13 14 5
56+
6 10 17 13 7 4 22 16 9 7
57+
17 8 12 11 20 13 5 24 11 3
58+
11
59+
19 1 20 18 8 11 21 11 4 19 14
60+
14 17 6 10 19 3 5 9 18 20 7
61+
4 8 9 3 3 1 3 17 3 19 21
62+
20 19 13 21 20 17 5 21 15 3 10
63+
18 1 7 16 19 21 15 8 7 17 5
64+
21 1 3 11 14 4 15 10 14 15 17
65+
5 15 5 12 16 5 15 14 8 11 5
66+
14 18 2 19 19 8 5 7 11 11 1
67+
20 9 13 8 16 4 21 20 12 16 1
68+
9 11 7 18 5 19 5 18 13 18 20
69+
5 16 1 12 6 15 8 15 3 18 7
70+
14
71+
11 31 22 3 36 20 10 23 6 5 22 22 19 29
72+
9 7 13 9 31 15 7 1 13 33 11 24 7 36
73+
21 22 6 19 23 4 6 21 14 36 9 4 30 21
74+
17 2 30 13 26 36 2 13 32 27 36 5 28 16
75+
8 20 12 16 31 10 32 15 19 24 34 20 1 16
76+
17 18 22 3 10 2 30 26 27 29 10 16 24 12
77+
25 32 31 20 10 29 19 11 32 23 28 20 33 24
78+
9 13 19 4 6 27 24 5 16 2 8 34 2 7
79+
21 5 5 26 2 35 7 36 21 22 23 33 2 6
80+
16 21 15 21 12 11 13 28 3 3 14 23 16 4
81+
1 31 35 33 23 29 12 18 24 25 19 33 17 13
82+
29 6 30 19 33 14 35 14 6 23 27 16 12 24
83+
26 31 30 10 16 21 7 4 16 25 31 19 21 8
84+
12 5 2 4 4 27 29 2 18 20 19 26 32 31
85+
20
86+
11 34 7 49 59 88 79 12 63 38 13 27 9 70 97 92 86 95 84 18
87+
11 84 39 44 86 86 59 52 61 97 81 94 92 78 32 7 5 62 41 75
88+
15 61 71 27 3 4 79 51 95 99 73 27 75 31 4 7 15 51 50 16
89+
6 81 32 61 75 24 36 26 57 55 52 15 35 44 30 25 2 54 12 25
90+
42 4 66 1 23 44 1 7 63 27 82 70 40 45 4 3 12 35 11 85
91+
97 55 69 49 34 79 37 69 89 66 85 22 23 88 24 79 1 48 85 72
92+
4 67 23 3 27 18 37 61 7 68 88 80 35 21 42 88 38 10 81 84
93+
78 86 21 50 46 13 50 9 54 3 1 94 85 75 80 45 31 100 29 70
94+
9 59 7 48 81 82 43 68 90 37 26 41 84 31 58 42 4 96 86 20
95+
22 4 49 94 74 42 6 38 84 90 29 95 84 36 18 4 10 34 71 26
96+
46 43 7 88 18 21 96 57 3 72 52 83 50 53 56 51 19 50 57 6
97+
15 30 88 26 49 10 6 12 98 81 47 88 82 2 68 85 62 12 92 88
98+
100 31 5 15 76 84 39 10 52 61 28 12 50 22 35 85 1 83 2 76
99+
17 27 83 45 18 4 95 37 23 96 58 49 36 47 13 10 41 38 37 6
100+
22 92 59 68 51 15 65 88 18 69 40 49 7 11 78 14 95 94 45 27
101+
13 36 33 22 29 49 11 10 50 91 15 71 87 83 63 26 76 89 28 9
102+
98 9 96 58 72 79 28 9 63 67 85 16 40 66 46 47 17 85 16 99
103+
42 87 28 97 60 89 92 90 51 60 96 22 51 95 55 44 16 9 51 69
104+
27 45 53 43 45 52 12 90 86 91 47 39 84 9 21 77 69 56 5 69
105+
99 47 66 91 71 2 9 26 43 54 52 30 4 94 97 92 2 67 12 85

src/DFS/swea2105/sample_output.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#1 6
2+
#2 -1
3+
#3 4
4+
#4 4
5+
#5 8
6+
#6 6
7+
#7 14
8+
#8 12
9+
#9 18
10+
#10 30

0 commit comments

Comments
 (0)