Skip to content

Commit e90527c

Browse files
committed
고다혜: [BOJ] 16724 피리 부는 사나이_250211
1 parent e6554cf commit e90527c

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

BOJ/15001-20000번/DH_16724.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 피리 부는 사나이
6+
*/
7+
8+
public class DH_16724 {
9+
static int N, M, cnt;
10+
static char[][] map;
11+
static int[] p;
12+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
13+
14+
public static void main(String[] args) throws Exception {
15+
initInput();
16+
initParent();
17+
18+
for(int r = 0; r < N; r++) {
19+
for(int c = 0; c < M; c++) {
20+
21+
int dir = dir(map[r][c]);
22+
int nr = r + dr[dir], nc = c + dc[dir];
23+
24+
int pos = r * M + c;
25+
int npos = nr * M + nc;
26+
27+
if(p[pos] == p[npos]) continue;
28+
union(pos, npos);
29+
}
30+
}
31+
32+
HashSet<Integer> parentSet = new HashSet<Integer>();
33+
34+
for(int p: p) parentSet.add(find(p));
35+
36+
System.out.println(parentSet.size());
37+
}
38+
39+
static void initParent() {
40+
for(int r = 0; r < N; r++) {
41+
for(int c = 0; c < M; c++) {
42+
int pos = r * M + c;
43+
p[pos] = pos;
44+
}
45+
}
46+
}
47+
48+
static void union(int a, int b) {
49+
a = find(a);
50+
b = find(b);
51+
52+
if(a != b) p[b] = a;
53+
}
54+
55+
static int find(int a) {
56+
return p[a] = p[a] == a ? a : find(p[a]);
57+
}
58+
59+
static boolean check(int r, int c) {
60+
return r >= 0 && r < N && c >= 0 && c < M;
61+
}
62+
63+
static void initInput() throws Exception {
64+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
65+
StringTokenizer st = new StringTokenizer(br.readLine());
66+
67+
N = Integer.parseInt(st.nextToken());
68+
M = Integer.parseInt(st.nextToken());
69+
70+
map = new char[N][M];
71+
p = new int[N * M];
72+
73+
for(int r = 0; r < N; r++) {
74+
String s = br.readLine();
75+
map[r] = s.toCharArray();
76+
}
77+
}
78+
79+
static int dir(char c) {
80+
if(c == 'U') return 0;
81+
if(c == 'D') return 1;
82+
if(c == 'L') return 2;
83+
else return 3;
84+
}
85+
}

BOJ/15001-20000번/DH_16724WA.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 피리 부는 사나이
6+
* 스택 오버플로우 발생하는데 통과되는 코드
7+
*/
8+
9+
public class DH_16724WA {
10+
static int N, M, cnt;
11+
static char[][] map;
12+
static int[][] idx;
13+
static HashMap<Integer, Integer> p = new HashMap<Integer, Integer>();
14+
15+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
16+
17+
public static void main(String[] args) throws Exception {
18+
initInput();
19+
20+
idx = new int[N][M];
21+
22+
int cnt = 1;
23+
24+
for(int r = 0; r < N; r++) {
25+
for(int c = 0; c < M; c++) {
26+
if(idx[r][c] != 0) continue;
27+
28+
p.put(cnt, cnt);
29+
30+
dfs(r, c, cnt);
31+
cnt += 1;
32+
}
33+
}
34+
35+
HashSet<Integer> parentSet = new HashSet<Integer>();
36+
for(int key: p.keySet()) {
37+
parentSet.add(find(p.get(key)));
38+
}
39+
System.out.println(parentSet.size());
40+
}
41+
42+
static void dfs(int r, int c, int cnt) {
43+
44+
idx[r][c] = cnt;
45+
46+
int dir = dir(map[r][c]);
47+
int nr = r + dr[dir];
48+
int nc = c + dc[dir];
49+
50+
if(idx[nr][nc] != 0) {
51+
if(idx[nr][nc] == cnt) return;
52+
else union(cnt, idx[nr][nc]);
53+
} else dfs(nr, nc, cnt);
54+
}
55+
56+
static void union(int a, int b) {
57+
a = find(a);
58+
b = find(b);
59+
60+
if(a != b) p.put(b, a);
61+
}
62+
63+
static int find(int a) {
64+
65+
if(a == p.get(a)) return a;
66+
else {
67+
p.put(a, find(p.get(a)));
68+
return p.get(a);
69+
}
70+
}
71+
72+
static boolean check(int r, int c) {
73+
return r >= 0 && r < N && c >= 0 && c < M;
74+
}
75+
static void initInput() throws Exception {
76+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
77+
StringTokenizer st = new StringTokenizer(br.readLine());
78+
79+
N = Integer.parseInt(st.nextToken());
80+
M = Integer.parseInt(st.nextToken());
81+
82+
map = new char[N][M];
83+
84+
for(int r = 0; r < N; r++) {
85+
String s = br.readLine();
86+
map[r] = s.toCharArray();
87+
}
88+
}
89+
90+
static int dir(char c) {
91+
if(c == 'U') return 0;
92+
if(c == 'D') return 1;
93+
if(c == 'L') return 2;
94+
else return 3;
95+
}
96+
}

0 commit comments

Comments
 (0)