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 a075b3c

Browse files
committed
배수빈: [BOJ] 18430 무기공학_250214
1 parent 7ca48fc commit a075b3c

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class SB_18430 {
7+
static int N,M;
8+
static int[][] board;
9+
static int[][] wings = {
10+
{0, -1, 1, 0},
11+
{-1, 0, 0, -1},
12+
{-1, 0, 0, 1},
13+
{0, 1, 1, 0}
14+
};
15+
static int ans = 0;
16+
17+
private static void dfs(int x, int y, int val, boolean[][] visited) {
18+
if(x==N) { // 모든 칸 탐색
19+
ans = Math.max(ans, val);
20+
return;
21+
}
22+
23+
int nx = (y+1 == M) ? x+1 : x; // 다음 위치, 다음 행으로 이동 or 열로 이동
24+
int ny = (y+1 == M) ? 0 : y+1;
25+
26+
if (visited[x][y]){
27+
dfs(nx, ny, val, visited);
28+
return;
29+
}
30+
31+
visited[x][y] = true;
32+
for (int[] w : wings){
33+
int lx = x + w[0]; // 날개 좌표
34+
int ly = y + w[1];
35+
int rx = x + w[2];
36+
int ry = y + w[3];
37+
38+
if (!isValid(lx, ly) || !isValid(rx, ry) || visited[lx][ly] || visited[rx][ry]) continue;
39+
40+
visited[lx][ly] = true;
41+
visited[rx][ry] = true;
42+
43+
int curVal = val + board[x][y]*2 + board[lx][ly] + board[rx][ry];
44+
dfs(nx, ny, curVal, visited);
45+
visited[lx][ly] = false;
46+
visited[rx][ry] = false;
47+
}
48+
visited[x][y] = false;
49+
50+
// 부메랑 안만드는 경우
51+
dfs(nx, ny, val, visited);
52+
53+
}
54+
55+
private static boolean isValid(int x, int y){
56+
return 0<=x && x<N && 0<=y && y<M;
57+
}
58+
59+
public static void main(String[] args) throws IOException {
60+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
61+
StringTokenizer st = new StringTokenizer(br.readLine());
62+
63+
N = Integer.parseInt(st.nextToken());
64+
M = Integer.parseInt(st.nextToken());
65+
66+
board = new int[N][M];
67+
for (int i = 0; i < N; i++) {
68+
st = new StringTokenizer(br.readLine());
69+
for (int j = 0; j < M; j++) {
70+
board[i][j] = Integer.parseInt(st.nextToken());
71+
}
72+
}
73+
74+
boolean[][] visited = new boolean[N][M];
75+
dfs(0, 0, 0, visited);
76+
System.out.println(ans);
77+
}
78+
}

0 commit comments

Comments
(0)

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