Skip to content

Commit 1acf426

Browse files
committed
[Gold IV] Title: 3루수는 몰라, Time: 296 ms, Memory: 27848 KB -BaekjoonHub
1 parent f5896e3 commit 1acf426

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static Map<Character, Integer> MOLA;
6+
static int[] dx = {1, 0};
7+
static int[] dy = {0, 1};
8+
static int[][][] dp;
9+
static int n, ans;
10+
static char[][] map;
11+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
public static void main(String[] args) throws Exception{
13+
inputSetting();
14+
15+
System.out.println(recur(0, 0, MOLA.getOrDefault(map[0][0], 0)));
16+
}
17+
18+
static int recur(int x, int y, int pre){
19+
if(x == n - 1 && y == n - 1) return 0;
20+
if(dp[pre][x][y] != -1) return dp[pre][x][y];
21+
int nx, ny, now, rot;
22+
23+
rot = 0;
24+
for(int i = 0; i < 2; i++) {
25+
nx = x + dx[i];
26+
ny = y + dy[i];
27+
28+
if (nx < 0 || n <= nx || ny < 0 || n <= ny) continue;
29+
30+
now = MOLA.getOrDefault(map[nx][ny], 0);
31+
32+
if (now == 4 && pre == 3) rot = Math.max(rot, recur(nx, ny, 0) + 1);
33+
else if (pre + 1 == now || now == 1) rot = Math.max(rot,recur(nx, ny, now));
34+
else rot = Math.max(rot,recur(nx, ny,0));
35+
36+
}
37+
return dp[pre][x][y] = rot;
38+
}
39+
40+
static void inputSetting() throws Exception{
41+
n = Integer.parseInt(br.readLine());
42+
map = new char[n][n];
43+
MOLA = new HashMap<>();
44+
dp = new int[5][n][n];
45+
46+
for(int i = 0; i < 5; i++){
47+
for(int j = 0; j < n; j++) Arrays.fill(dp[i][j], -1);
48+
}
49+
50+
for(int i = 0; i < n; i++) map[i] = br.readLine().toCharArray();
51+
MOLA.put('M', 1);
52+
MOLA.put('O', 2);
53+
MOLA.put('L', 3);
54+
MOLA.put('A', 4);
55+
}
56+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [Gold IV] 3루수는 몰라 - 17351
2+
3+
[문제 링크](https://www.acmicpc.net/problem/17351)
4+
5+
### 성능 요약
6+
7+
메모리: 27848 KB, 시간: 296 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2025년 4월 9일 11:22:06
16+
17+
### 문제 설명
18+
19+
<p>기어코 2루수의 이름을 알아낸 선수들. 2루수에게 매운맛을 보여 주려는 순간, 2루수가 충격적인 사실을 고백했다!</p>
20+
21+
<p style="text-align: center;"><em>"사실 음료수는 3루수가 보낸 거야!!"</em></p>
22+
23+
<p>선수들은 서로 3루수가 아니라고 주장하기 시작했다. 하지만 이젠 아무도 믿을 수 없다! 이번에는 감독님도 별로 도움이 안 되는 것 같다.</p>
24+
25+
<p style="text-align: center;"><em>"1루수가 누구야 2루수 이름이 뭐야 3루수는 몰라"</em></p>
26+
27+
<p>하지만 언제나 희망은 있다! 운동장에 3루수에 대한 단서가 흩뿌려져 있는 듯하다.</p>
28+
29+
<ol>
30+
<li>운동장은 N×N 크기의 격자로 표현된다.</li>
31+
<li>운동장의 각 칸에는 알파벳 대문자로 표현되는 단서들이 숨겨져 있다.</li>
32+
<li>우리는 (1, 1)에서 (N, N)까지 이동하면서 단서를 수집해야 한다. (각 좌표는 시작점과 도착점이다.)</li>
33+
<li>지금은 여름이라 몹시 더우므로, (r+1, c) 또는 (r, c+1)로만 움직여야 한다. (안 그러면 탈진해서 쓰러질 것이다!) 물론, 운동장을 벗어날 수는 없다.</li>
34+
<li>운동장을 지나면서 수집한 단서를 <strong>순서대로</strong> 이어 붙인 문자열을 S라고 하자. S에 "MOLA"라는 부분 문자열이 최대한 많이 등장하도록 움직여야 한다.</li>
35+
<li>어떤 칸을 지나면 반드시 해당 칸의 단서를 수집해야 한다. 즉, 단서를 취사 선택하지 않는다.</li>
36+
</ol>
37+
38+
<p><strong>3루수는 몰라몰라몰라몰라아몰르파티~</strong></p>
39+
40+
### 입력
41+
42+
<p>첫째 줄에 운동장의 크기 N이 주어진다. (1 ≤ N ≤ 500)</p>
43+
44+
<p>둘째 줄부터 N개의 줄마다 N개의 문자가 주어진다. 이는 운동장 각 칸의 단서를 적어둔 지도이며, 알파벳 대문자로만 이루어져 있다.</p>
45+
46+
<p>첫 줄의 첫 문자가 (1, 1)이고, 마지막 줄의 마지막 문자가 (N, N)이다.</p>
47+
48+
### 출력
49+
50+
<p>주어진 규칙을 따라 이동하면서 부분 문자열로 "MOLA"를 최대한 많이 포함하는 S를 만들고, 이때 S에 포함된 부분 문자열 "MOLA"의 개수를 출력한다.</p>
51+

0 commit comments

Comments
 (0)