Skip to content

Commit 40345c2

Browse files
authored
[허승연] 백준 8월 4주차 숙제 (9/7/화) (#107)
* 5547 * solve: 2931 * solve: 17090
1 parent 085af38 commit 40345c2

File tree

3 files changed

+360
-0
lines changed

3 files changed

+360
-0
lines changed

‎syheo/baekjoon_java/Main_17090.java

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.awt.*;
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
import java.util.StringTokenizer;
10+
11+
/**
12+
* 백준
13+
* solved.ac
14+
* 17090
15+
* 미로 탈출하기
16+
* 골드 2
17+
* 아이디어 :
18+
* 방문하지 않은 노드에 대해서 dfs를 돈다.
19+
* dfs에서 탈출조건은
20+
* 1. 범위 밖으로 이탈했을 경우
21+
* 2. dp[row][col] 이 0보다 클 경우 ( 이전에 탈출 경로를 지나친 경우 )
22+
* 그외에 싸이클을 발견했을 경우 즉, 방문했던 노드를 또 지나는 경우에
23+
* 해당 경로의 방문처리를 취소한다.
24+
* */
25+
26+
//U인 경우에는 (r-1, c)로 이동해야 한다.
27+
//R인 경우에는 (r, c+1)로 이동해야 한다.
28+
//D인 경우에는 (r+1, c)로 이동해야 한다.
29+
//L인 경우에는 (r, c-1)로 이동해야 한다.
30+
31+
public class Main_17090 {
32+
33+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
34+
static StringTokenizer tokens;
35+
static int N,M;
36+
static String inputStr;
37+
static char[][] maps;
38+
static boolean[][] visited;
39+
static int[][] dp;
40+
static int cnt = 0;
41+
static Queue<Point> queue = new LinkedList<>();
42+
43+
public static void main(String[] args) throws IOException {
44+
// 1. input
45+
tokens = new StringTokenizer(input.readLine());
46+
N = Integer.parseInt(tokens.nextToken());
47+
M = Integer.parseInt(tokens.nextToken());
48+
maps = new char[N][M];
49+
50+
for (int i = 0; i < N; i++) {
51+
inputStr = input.readLine();
52+
for (int j = 0; j < M; j++) {
53+
maps[i][j] = inputStr.charAt(j);
54+
}
55+
}
56+
57+
visited = new boolean[N][M];
58+
dp = new int[N][M];
59+
60+
// 2. bfs
61+
for (int i = 0; i < N; i++) {
62+
for (int j = 0; j < M; j++) {
63+
if(!visited[i][j]){
64+
cnt+=dfs(i,j,0);
65+
}
66+
}
67+
}
68+
69+
// 3. print cnt
70+
System.out.println(cnt);
71+
}
72+
73+
private static int dfs(int row, int col,int cnt) {
74+
// 탈출 체크
75+
if((row<0 || row>=N) || (col<0 || col>=M) || dp[row][col] >0){
76+
return cnt;
77+
}
78+
// 방문한델 또 방문? -> 싸이클
79+
if(visited[row][col]){
80+
return 0;
81+
}
82+
// 방문 처리
83+
else{
84+
visited[row][col] = true;
85+
}
86+
// 방향에 따라 방문
87+
if(maps[row][col]=='U'){
88+
cnt=dfs(row-1,col,cnt+1);
89+
}
90+
if(maps[row][col]=='D'){
91+
cnt=dfs(row+1,col,cnt+1);
92+
}
93+
if(maps[row][col]=='R'){
94+
cnt=dfs(row,col+1,cnt+1);
95+
}
96+
if(maps[row][col]=='L'){
97+
cnt=dfs(row,col-1,cnt+1);
98+
}
99+
if(cnt == 0)
100+
visited[row][col] = false;
101+
else
102+
dp[row][col] += cnt;
103+
104+
105+
106+
return cnt;
107+
}
108+
}

‎syheo/baekjoon_java/Main_2931.java

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* 백준
11+
* solved.ac
12+
* 2931
13+
* 가스관
14+
* 골드 3
15+
* 구현
16+
* 아이디어 : 진짜 쌉구��� 문제..
17+
* 일단 아이디어는 '.'인 곳 중 상하좌우에 1~7 에 해당하는 파이프가 directionMap 을 통해 연결되어 있는 상태로 존재하는지 확인함.
18+
* 만약 위 경우가 있을 경우 어떤 파이프를 둬야 하는지 확인후 출력 후 종료.
19+
* */
20+
21+
public class Main_2931 {
22+
23+
static Map<Integer,List<Integer>> pipes;
24+
// 1-하,우 2-상,우 3-좌,상 4-좌,하 5-상,하 6-좌,우 7-상,하,좌,우, 8-m, 9-z
25+
static Map<Integer, List<Integer>> directionMap;
26+
static int[] dx = {-1,0,1,0};
27+
static int[] dy = {0,1,0,-1};
28+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
29+
static StringTokenizer tokens;
30+
static int R, C;
31+
static int[][] maps;
32+
static List<Integer> result = new ArrayList<>();
33+
34+
public static void main(String[] args) throws IOException {
35+
36+
// 1. input
37+
tokens = new StringTokenizer(input.readLine());
38+
39+
R = Integer.parseInt(tokens.nextToken());
40+
C = Integer.parseInt(tokens.nextToken());
41+
42+
maps = new int[R][C];
43+
for (int i = 0; i < R; i++) {
44+
String nowRow = input.readLine();
45+
for (int j = 0; j < C; j++) {
46+
if (nowRow.charAt(j) == '|') {
47+
maps[i][j] = 5;
48+
} else if (nowRow.charAt(j) == '-') {
49+
maps[i][j] = 6;
50+
} else if (nowRow.charAt(j) == '+') {
51+
maps[i][j] = 7;
52+
} else if (nowRow.charAt(j) == 'M') {
53+
maps[i][j] = 8;
54+
} else if (nowRow.charAt(j) == 'Z') {
55+
maps[i][j] = 9;
56+
} else if (nowRow.charAt(j) == '.') {
57+
maps[i][j] = 0;
58+
} else {
59+
maps[i][j] = nowRow.charAt(j) - '0';
60+
}
61+
} // for
62+
} // for
63+
64+
// 2-1. set directionMap
65+
directionMap = new HashMap<>();
66+
directionMap.put(0, Arrays.stream(new int[]{1, 4, 5, 7})
67+
.boxed()
68+
.collect(Collectors.toList()));
69+
directionMap.put(1, Arrays.stream(new int[]{3,4,6,7})
70+
.boxed()
71+
.collect(Collectors.toList()));
72+
directionMap.put(2, Arrays.stream(new int[]{2, 3, 5, 7})
73+
.boxed()
74+
.collect(Collectors.toList()));
75+
directionMap.put(3, Arrays.stream(new int[]{1, 2,6, 7})
76+
.boxed()
77+
.collect(Collectors.toList()));
78+
79+
// 2-2. set pipes
80+
pipes = new HashMap<>();
81+
82+
pipes.put(1, Arrays.stream(new int[]{1,2})
83+
.boxed()
84+
.collect(Collectors.toList()));
85+
pipes.put(2, Arrays.stream(new int[]{0,1})
86+
.boxed()
87+
.collect(Collectors.toList()));
88+
pipes.put(3, Arrays.stream(new int[]{0,3})
89+
.boxed()
90+
.collect(Collectors.toList()));
91+
pipes.put(4, Arrays.stream(new int[]{2,3})
92+
.boxed()
93+
.collect(Collectors.toList()));
94+
pipes.put(5, Arrays.stream(new int[]{0,2})
95+
.boxed()
96+
.collect(Collectors.toList()));
97+
pipes.put(6, Arrays.stream(new int[]{1,3})
98+
.boxed()
99+
.collect(Collectors.toList()));
100+
pipes.put(7, Arrays.stream(new int[]{0,1,2,3})
101+
.boxed()
102+
.collect(Collectors.toList()));
103+
104+
// 3. find
105+
for (int i = 0; i < R; i++) {
106+
for (int j = 0; j < C; j++) {
107+
if (maps[i][j]==0){
108+
result = new ArrayList<>();
109+
// 상하좌우 탐색
110+
for (int k = 0; k < 4; k++) {
111+
int row = i+dx[k];
112+
int col = j+dy[k];
113+
if(0<=row && row<R && 0<=col && col<C){
114+
// 열려있는 파이프 구멍이 있나?
115+
if(directionMap.get(k).contains(maps[row][col])){
116+
result.add(k);
117+
}
118+
}
119+
}
120+
// 정답일 경우
121+
if(!result.isEmpty()){
122+
// 어떤 파이프인지 고름
123+
for (int k = 1; k <= 7; k++) {
124+
if(result.equals(pipes.get(k))){
125+
char answer='0';
126+
if(k<=4){
127+
answer = Integer.toString(k).charAt(0);
128+
}
129+
else{
130+
if(k==5){
131+
answer = '|';
132+
}
133+
if(k==6){
134+
answer = '-';
135+
}
136+
if(k==7){
137+
answer = '+';
138+
}
139+
}
140+
System.out.println((i+1)+" "+(j+1)+" "+answer);
141+
return ;
142+
}
143+
}
144+
}
145+
}
146+
}
147+
}
148+
149+
}
150+
}

‎syheo/baekjoon_java/Main_5547.java

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package syheo.baekjoon_java;
2+
3+
import java.awt.*;
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
import java.util.StringTokenizer;
10+
11+
/**
12+
* 백준
13+
* solved.ac
14+
* 5547
15+
* 일루미네이션
16+
* 실버1
17+
* BFS
18+
* 아이디어 :
19+
* 처음엔 빌딩을 기준으로 bfs를 돌려서 감싸져 있는 빈 공간에 대한 처리를 하려다가
20+
* 빈 공간이 1개일 경우만 고려해서 실패
21+
* 다음 아이디어 :
22+
* 테두리를 모두 0 으로 초기화 시키고 0,0 위치에서 bfs 를 실행하여
23+
* 빌딩을 만나면 cnt 를 1 증가시켜서 답을 구함.
24+
*/
25+
26+
public class Main_5547 {
27+
28+
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
29+
static StringTokenizer tokens;
30+
static int W,H;
31+
static int cnt = 0;
32+
static int[] dx = {-1,-1,0,1,1,0,-1,-1,0,1,1,0}; //row 기준 0~5 홀수 일 떄 , 6~11 짝수 일 때
33+
static int[] dy = {0,1,1,1,0,-1,-1,0,1,0,-1,-1};
34+
static int[][] maps;
35+
static boolean[][] visited;
36+
static int[][] dp;
37+
static Queue<Point> queue = new LinkedList<>();
38+
39+
public static void main(String[] args) throws IOException {
40+
// 1. input
41+
tokens = new StringTokenizer(input.readLine());
42+
43+
W = Integer.parseInt(tokens.nextToken());
44+
H = Integer.parseInt(tokens.nextToken());
45+
46+
maps = new int[H+2][W+2];
47+
for (int j = 0; j <= W+1; j++) {
48+
maps[0][j] = 0;
49+
}
50+
for (int i = 1; i <= H; i++) {
51+
tokens = new StringTokenizer(input.readLine());
52+
maps[i][0]=0;
53+
for (int j = 1; j <= W; j++) {
54+
maps[i][j] = Integer.parseInt(tokens.nextToken());
55+
}
56+
maps[i][W+1]=0;
57+
}
58+
for (int j = 0; j <= W+1; j++) {
59+
maps[H+1][j] = 0;
60+
}
61+
62+
dp = new int[H+2][W+2];
63+
visited = new boolean[H+2][W+2];
64+
// 2. calculate sum of light by bfS
65+
//
66+
bfs(0,0);
67+
68+
69+
// 3. print
70+
System.out.println(cnt);
71+
72+
}
73+
74+
private static void bfs(int i, int j) {
75+
Point p;
76+
int plus = 0;
77+
queue.add(new Point(i,j));
78+
visited[i][j]=true;
79+
while(!queue.isEmpty()){
80+
p = queue.poll();
81+
if (p.x % 2 == 0) {
82+
plus = 6;
83+
}
84+
else{
85+
plus = 0;
86+
}
87+
for (int k = plus; k < plus+6; k++) {
88+
if(p.x+dx[k]>=0 && p.x+dx[k]<=H+1 && p.y+dy[k]>=0 && p.y+dy[k]<=W+1 && !visited[p.x+dx[k]][p.y+dy[k]]){
89+
// 빈 공간이면 queue에 넣어줌
90+
if(maps[p.x+dx[k]][p.y+dy[k]]==0){
91+
queue.add(new Point(p.x+dx[k],p.y+dy[k]));
92+
visited[p.x+dx[k]][p.y+dy[k]] = true;
93+
}
94+
// 건물을 만나면 카운팅
95+
if(maps[p.x+dx[k]][p.y+dy[k]]==1){
96+
cnt+=1;
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)