Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d584ca9

Browse files
committed
배수빈: [BOJ] 16724 피리 부는 사나이_240212
1 parent ed03470 commit d584ca9

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

‎BOJ/15001-20000번/SB_16724.java‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
/**
7+
* 어느 구역에 있더라도 SAFE ZONE 들거가게 하는 SAFE ZONE의 최소 개수 구하기
8+
* => 순환 그래프의 개수 구하기
9+
*/
10+
11+
public class SB_16724 {
12+
static int N, M;
13+
static int[][] board;
14+
static int[] dx = {-1, 1, 0, 0}; // 행 이동
15+
static int[] dy = {0, 0, -1, 1}; // 열 이동
16+
static boolean[][] visited;
17+
static boolean[][] finished;
18+
static int cycle = 0;
19+
20+
private static void dfs(int x, int y) {
21+
visited[x][y] = true;
22+
23+
// 다음 좌표
24+
int d = board[x][y];
25+
int nx = x+dx[d];
26+
int ny = y+dy[d];
27+
28+
if (visited[nx][ny] && !finished[nx][ny]) { // 사이클 찾은 경우
29+
cycle++;
30+
}
31+
32+
if (!visited[nx][ny]) dfs(nx, ny); // 방문안했을 경우 다음 좌표로 이동
33+
34+
finished[x][y] = true; // 현재 노드의 dfs를 다 돌았으면 끝났음 표시
35+
}
36+
public static void main(String[] args) throws IOException {
37+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
40+
N = Integer.parseInt(st.nextToken());
41+
M = Integer.parseInt(st.nextToken());
42+
43+
board = new int[N][M];
44+
visited = new boolean[N][M];
45+
finished = new boolean[N][M];
46+
for (int i = 0; i < N; i++) {
47+
String line = br.readLine();
48+
for (int j = 0; j < M; j++) {
49+
char d = line.charAt(j);
50+
switch (d){
51+
case 'U':
52+
board[i][j] = 0;
53+
break;
54+
case 'D':
55+
board[i][j] = 1;
56+
break;
57+
case 'L':
58+
board[i][j] = 2;
59+
break;
60+
case 'R':
61+
board[i][j] = 3;
62+
break;
63+
}
64+
}
65+
}
66+
67+
for (int i = 0; i < N; i++) {
68+
for (int j = 0; j < M; j++) {
69+
if (!finished[i][j]) dfs(i, j);
70+
}
71+
}
72+
73+
System.out.println(cycle);
74+
}
75+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /