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 a7fc347

Browse files
committed
배수빈: [CT] 술래잡기 체스_241206
1 parent 13c2cb7 commit a7fc347

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_술래잡기_체스 {
7+
static int mx = -1;
8+
static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1};
9+
static int[] dy = {0, -1, -1, -1, 0, 1, 1, 1};
10+
11+
private static int[][] copyBoard(int[][] board) {
12+
int[][] tmp = new int[4][4];
13+
for (int i = 0; i < 4; i++) {
14+
for (int j = 0; j < 4; j++) {
15+
tmp[i][j] = board[i][j];
16+
}
17+
}
18+
return tmp;
19+
}
20+
21+
private static List<Piece> copyPiece(List<Piece> pieces) {
22+
List<Piece> tmpList = new ArrayList<>();
23+
for (Piece p : pieces) {
24+
tmpList.add(new Piece(p.idx, p.x, p.y, p.dir, p.isDie));
25+
}
26+
return tmpList;
27+
}
28+
private static void dfs(Tagger it, int[][] board, List<Piece> pieces) {
29+
// 도둑말들 움직이기
30+
moveThief(board, pieces);
31+
32+
// 현재 방향으로 잡을 수 있는 말 잡기(최대 3번 이동 가능)
33+
for (int i = 1; i < 4; i++) {
34+
int nx = it.x + dx[it.dir] * i;
35+
int ny = it.y + dy[it.dir] * i;
36+
37+
if (isValid(nx, ny) && board[nx][ny] != 0) {
38+
int[][] tmpBoard = copyBoard(board);
39+
List<Piece> tmpPiece = copyPiece(pieces);
40+
41+
tmpBoard[it.x][it.y] = 0;
42+
Piece thief = tmpPiece.get(board[nx][ny]);
43+
Tagger nit = new Tagger(thief.x, thief.y, thief.dir, it.cnt + thief.idx);
44+
thief.isDie = true;
45+
tmpBoard[thief.x][thief.y] = -1;
46+
47+
dfs(nit, tmpBoard, tmpPiece);
48+
}
49+
}
50+
mx = Math.max(mx, it.cnt);
51+
}
52+
53+
54+
private static void moveThief(int[][] board, List<Piece> pieces) {
55+
for (Piece cur : pieces) {
56+
if (cur.idx == 0 || cur.isDie) continue; // 죽었거나 술래면 스킵
57+
for (int d = 0; d < 8; d++) {
58+
int nd = (cur.dir + d) % 8;
59+
int nx = cur.x + dx[nd];
60+
int ny = cur.y + dy[nd];
61+
if (!isValid(nx, ny) || board[nx][ny] == -1) continue;
62+
board[cur.x][cur.y] = 0;
63+
if (board[nx][ny] == 0) cur.setPos(nx, ny); // 빈칸이면 바로 이동
64+
else { // 다른말있으면 교환
65+
Piece target = pieces.get(board[nx][ny]);
66+
target.setPos(cur.x, cur.y);
67+
board[cur.x][cur.y] = target.idx;
68+
cur.setPos(nx, ny);
69+
}
70+
board[nx][ny] = cur.idx; // 움직일 보드에 현재 말 놓기
71+
cur.dir = nd; // 현재 말의 방향 갱신
72+
break;
73+
}
74+
}
75+
}
76+
77+
private static boolean isValid(int x, int y) {
78+
return 0 <= x && x < 4 && 0 <= y && y < 4;
79+
}
80+
public static void main(String[] args) throws IOException {
81+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
82+
StringTokenizer st;
83+
84+
int[][] board = new int[4][4]; // 말들의 번호로 저장
85+
List<Piece> pieces = new ArrayList<>();
86+
87+
for (int i = 0; i < 4; i++) {
88+
st = new StringTokenizer(br.readLine());
89+
for (int j = 0; j < 4; j++) {
90+
int num = Integer.parseInt(st.nextToken());
91+
int dir = Integer.parseInt(st.nextToken()) - 1;
92+
Piece piece = new Piece(num, i, j, dir, false);
93+
pieces.add(piece);
94+
board[i][j] = num;
95+
}
96+
}
97+
98+
// 도둑말 인덱스 순으로 정렬
99+
pieces.add(new Piece(0, -1, -1, 0, false)); // 인덱스 0맞추기 위해 더미 객체 넣기
100+
pieces.sort(((o1, o2) -> o1.idx - o2.idx));
101+
102+
// 초기 0,0 도둑말 잡기
103+
Piece first = pieces.get(board[0][0]);
104+
Tagger tagger = new Tagger(0, 0, first.dir, first.idx);
105+
board[0][0] = -1;
106+
first.isDie = true;
107+
108+
// 술래말의 재귀 시작
109+
dfs(tagger, board, pieces);
110+
111+
System.out.println(mx);
112+
}
113+
114+
static class Tagger{
115+
int x, y, dir, cnt;
116+
117+
public Tagger(int x, int y, int dir, int cnt) {
118+
this.x = x;
119+
this.y = y;
120+
this.dir = dir;
121+
this.cnt = cnt;
122+
}
123+
}
124+
125+
static class Piece{
126+
int idx, x, y, dir;
127+
boolean isDie;
128+
129+
public Piece(int idx, int x, int y, int dir, boolean isDie) {
130+
this.idx = idx;
131+
this.x = x;
132+
this.y = y;
133+
this.dir = dir;
134+
this.isDie = isDie;
135+
}
136+
137+
private void setPos(int x, int y) {
138+
this.x = x;
139+
this.y = y;
140+
}
141+
}
142+
}

0 commit comments

Comments
(0)

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