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 e4acbb0

Browse files
committed
고다혜: [CT] 2개의 사탕_250203
1 parent c2a7684 commit e4acbb0

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

‎BOJ/1000-5000번/DH_2169.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 로봇 조종하기
6+
*/
7+
8+
public class DH_2169 {
9+
static int INF = - 1_000 * 1_000 * 10;
10+
11+
public static void main(String[] args) throws Exception {
12+
System.setIn(new FileInputStream("./input/BOJ2169.txt"));
13+
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
int N = Integer.parseInt(st.nextToken());
18+
int M = Integer.parseInt(st.nextToken());
19+
20+
int[][] arr = new int[N + 1][M + 1];
21+
int[][][] dp = new int[3][N + 1][M + 1];
22+
23+
for(int k = 0; k < 3; k++) {
24+
for(int r = 0; r < dp[0].length; r++) Arrays.fill(dp[k][r], INF);
25+
}
26+
for(int r = 1; r < arr.length; r++) {
27+
st = new StringTokenizer(br.readLine());
28+
for(int c = 1; c < arr[0].length; c++) {
29+
arr[r][c] = Integer.parseInt(st.nextToken());
30+
}
31+
}
32+
33+
// dp 테이블 채우기
34+
for(int r = 1; r < dp[0].length; r++) {
35+
if(r == 1) {
36+
dp[0][r][0] = 0;
37+
for(int c = 1; c < dp[0][0].length; c++) {
38+
dp[0][r][c] = arr[r][c] + dp[0][r][c - 1];
39+
}
40+
continue;
41+
}
42+
43+
// 위에서 오는 경우
44+
for(int c = 1; c < dp[0][0].length; c++) {
45+
dp[1][r][c] = Math.max(dp[0][r - 1][c], Math.max(dp[1][r - 1][c], dp[2][r - 1][c])) + arr[r][c];
46+
}
47+
48+
// 왼쪽에서 오는 경우
49+
for(int c = 1; c < dp[0][0].length; c++) {
50+
dp[0][r][c] = Math.max(dp[0][r][c - 1], dp[1][r][c - 1]) + arr[r][c];
51+
}
52+
53+
// 오른쪽에서 오는 경우
54+
for(int c = dp[0][0].length - 2; c > 0; c--) {
55+
dp[2][r][c] = Math.max(dp[1][r][c + 1], dp[2][r][c + 1]) + arr[r][c];
56+
}
57+
58+
59+
}
60+
System.out.println(Math.max(dp[0][N][M], Math.max(dp[1][N][M], dp[2][N][M])));
61+
}
62+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 2개의 사탕
6+
*/
7+
8+
public class DH_2개의_사탕 {
9+
static class Point {
10+
int r, c;
11+
public Point(int r, int c) {
12+
this.r = r;
13+
this.c = c;
14+
}
15+
16+
public void update(int r, int c) {
17+
this.r = r;
18+
this.c = c;
19+
}
20+
}
21+
static int N, M, result = Integer.MAX_VALUE;
22+
static char[][] map;
23+
static int[] dr = {-1, 0, 1, 0}, dc = {0, -1, 0, 1};
24+
static Point RED, BLUE;
25+
26+
public static void main(String[] args) throws Exception {
27+
System.setIn(new FileInputStream("./input/2개의사탕.txt"));
28+
29+
initInput();
30+
31+
dfs(RED, BLUE, 0, -1);
32+
33+
System.out.println(result == Integer.MAX_VALUE ? -1 : result);
34+
}
35+
static void dfs(Point red, Point blue, int depth, int prevDir) {
36+
37+
// 기울이는 횟수가 10번이 초과하는 경우 그만!!
38+
if(depth == 10) return;
39+
40+
// 네가지 방향으로 기울여보기
41+
for(int d = 0; d < 4; d++) {
42+
// 이전에 있던 위치를 향해서 못가게 하기
43+
if(prevDir == (d + 2) % 4) continue;
44+
45+
// 빨간색, 파란색 사탕 각각 기울였을 때, 도달하는 위치 구하기
46+
Point nextRed = getNextPoint(red, d);
47+
Point nextBlue = getNextPoint(blue, d);
48+
49+
// 파란색 사탕이 빠져나가면 안됨
50+
if(map[nextBlue.r][nextBlue.c] == 'O') continue;
51+
52+
// 기울였을 때, 파란색 사탕과 빨간색 사탕이 도달하는 위치가 같으면
53+
// 이전 위치를 비교해서 각 사탕이 있을 수 있는 위치 정해주기
54+
if(nextRed.r == nextBlue.r && nextRed.c == nextBlue.c) {
55+
// 상
56+
if(d == 0) {
57+
if(red.r > blue.r) nextRed.r += 1;
58+
else nextBlue.r += 1;
59+
}
60+
61+
// 좌
62+
if(d == 1) {
63+
if(red.c > blue.c) nextRed.c += 1;
64+
else nextBlue.c += 1;
65+
}
66+
67+
// 하
68+
if(d == 2) {
69+
if(red.r < blue.r) nextRed.r -= 1;
70+
else nextBlue.r -= 1;
71+
}
72+
73+
// 우
74+
if(d == 3) {
75+
if(red.c < blue.c) nextRed.c -= 1;
76+
else nextBlue.c -= 1;
77+
}
78+
}
79+
80+
// 두 사탕 모두 움직이지 못하는 경우 그만
81+
if(red.r == nextRed.r && red.c == nextRed.c && blue.r == nextBlue.r && blue.c == nextBlue.c) continue;
82+
if(map[nextRed.r][nextRed.c] == 'O' && map[nextBlue.r][nextBlue.c] == 'O') return;
83+
84+
// 빨간색 사탕을 움직일 수 있는 경우
85+
if(map[nextRed.r][nextRed.c] == 'O') {
86+
result = Math.min(result, depth + 1);
87+
return;
88+
}
89+
90+
dfs(nextRed, nextBlue, depth + 1, d);
91+
}
92+
93+
}
94+
95+
// 기울였을 때, 사탕이 도달하는 위치 구하기
96+
static Point getNextPoint(Point p, int dir) {
97+
Point next = new Point(p.r, p.c);
98+
99+
while(true) {
100+
int nr = next.r + dr[dir];
101+
int nc = next.c + dc[dir];
102+
103+
if(!isValid(nr, nc) || map[nr][nc] == '#') break;
104+
next.update(nr, nc);
105+
106+
if(map[next.r][next.c] == 'O') break;
107+
}
108+
109+
return next;
110+
}
111+
112+
static boolean isValid(int r, int c) {
113+
return r >= 0 && r < N && c >= 0 && c < M;
114+
}
115+
116+
static void initInput() throws Exception {
117+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
118+
StringTokenizer st = new StringTokenizer(br.readLine());
119+
120+
N = Integer.parseInt(st.nextToken());
121+
M = Integer.parseInt(st.nextToken());
122+
123+
map = new char[N][M];
124+
125+
for(int r = 0; r < map.length; r++) {
126+
map[r] = br.readLine().toCharArray();
127+
128+
for(int c = 0; c < map[r].length; c++) {
129+
if(map[r][c] != 'R' && map[r][c] != 'B') continue;
130+
if(map[r][c] == 'R') RED = new Point(r, c);
131+
if(map[r][c] == 'B') BLUE = new Point(r, c);
132+
map[r][c] = '.';
133+
}
134+
}
135+
}
136+
}

0 commit comments

Comments
(0)

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