Skip to content

Commit 08d851c

Browse files
committed
[Silver I] Title: 컴백홈, Time: 108 ms, Memory: 15072 KB -BaekjoonHub
1 parent 2bf0da9 commit 08d851c

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [Silver I] 컴백홈 - 1189
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1189)
4+
5+
### 성능 요약
6+
7+
메모리: 15072 KB, 시간: 108 ms
8+
9+
### 분류
10+
11+
그래프 이론, 브루트포스 알고리즘, 그래프 탐색, 깊이 우선 탐색, 백트래킹, 격자 그래프
12+
13+
### 제출 일자
14+
15+
2025년 7월 17일 12:46:25
16+
17+
### 문제 설명
18+
19+
<p>한수는 캠프를 마치고 집에 돌아가려 한다. 한수는 현재 왼쪽 아래점에 있고 집은 오른쪽 위에 있다. 그리고 한수는 집에 돌아가는 방법이 다양하다. 단, 한수는 똑똑하여 한번 지나친 곳을 다시 방문하지는 않는다.</p>
20+
21+
<pre> cdef ...f ..ef ..gh cdeh cdej ...f
22+
bT.. .T.e .Td. .Tfe bTfg bTfi .Tde
23+
a... abcd abc. abcd a... a.gh abc.
24+
거리 : 6 6 6 8 8 10 6</pre>
25+
26+
<p>위 예제는 한수가 집에 돌아갈 수 있는 모든 경우를 나타낸 것이다. T로 표시된 부분은 가지 못하는 부분이다. 문제는 R x C 맵에 못가는 부분이 주어지고 거리 K가 주어지면 한수가 집까지도 도착하는 경우 중 거리가 K인 가짓수를 구하는 것이다.</p>
27+
28+
### 입력
29+
30+
<p>첫 줄에 정수 R(1 ≤ R ≤ 5), C(1 ≤ C ≤ 5), K(1 ≤ K ≤ R×C)가 공백으로 구분되어 주어진다. 두 번째부터 R+1번째 줄까지는 R×C 맵의 정보를 나타내는 '.'과 'T'로 구성된 길이가 C인 문자열이 주어진다.</p>
31+
32+
### 출력
33+
34+
<p>첫 줄에 거리가 K인 가짓수를 출력한다.</p>
35+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
private static int R;
6+
private static int C;
7+
private static int K;
8+
private static Character[][] arr;
9+
private static boolean[][] visit;
10+
11+
private static int[] dx = {0, 0, -1, 1};
12+
private static int[] dy = {1, -1, 0, 0};
13+
private static int count = 0;
14+
15+
private static class Node {
16+
int x;
17+
int y;
18+
int t;
19+
20+
public Node(int x, int y, int t) {
21+
this.x = x;
22+
this.y = y;
23+
this.t = t;
24+
}
25+
}
26+
27+
private static Queue<Node> queue = new LinkedList<>();
28+
29+
public static void main(String[] args) throws IOException {
30+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
31+
StringTokenizer st = new StringTokenizer(br.readLine());
32+
33+
R = Integer.parseInt(st.nextToken());
34+
C = Integer.parseInt(st.nextToken());
35+
K = Integer.parseInt(st.nextToken());
36+
37+
arr = new Character[R][C];
38+
visit = new boolean[R][C];
39+
40+
for (int i = 0; i < R; i++) {
41+
String temp = br.readLine();
42+
for (int j = 0; j < C; j++) {
43+
arr[i][j] = temp.charAt(j);
44+
}
45+
}
46+
visit[R - 1][0] = true;
47+
dfs(R - 1, 0, 1);
48+
System.out.print(count);
49+
}
50+
51+
private static void dfs(int x, int y, int depth) {
52+
if (x == 0 && y == C - 1) {
53+
if (depth == K) count++;
54+
return;
55+
}
56+
57+
for (int i = 0; i < 4; i++) {
58+
int nx = x + dx[i];
59+
int ny = y + dy[i];
60+
61+
if (nx >= 0 && nx < R && ny >= 0 && ny < C &&
62+
!visit[nx][ny] && arr[nx][ny] != 'T') {
63+
64+
visit[nx][ny] = true;
65+
dfs(nx, ny, depth + 1);
66+
visit[nx][ny] = false;
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)