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 2419c2b

Browse files
committed
이예진: [CT] 술래잡기 체스_241202
1 parent bfdb515 commit 2419c2b

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+
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class YJ_술래잡기_체스 {
6+
static class Chess implements Comparable<Chess>{
7+
int x;
8+
int y;
9+
int number;
10+
int direction;
11+
12+
Chess (int x, int y, int number, int direction){
13+
this.x = x;
14+
this.y = y;
15+
this.number = number;
16+
this.direction = direction;
17+
}
18+
19+
@Override
20+
public int compareTo(Chess c){
21+
return this.number - c.number;
22+
}
23+
}
24+
25+
static final int NUM = 4;
26+
static final Chess EMPTY = new Chess(0,0,0,0);
27+
static Chess[][] game = new Chess[NUM][NUM];
28+
static PriorityQueue<Chess> pq = new PriorityQueue<>();
29+
static int score = 0;
30+
static Chess tagger;
31+
32+
public static void main(String[] args) throws IOException {
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
//초기값 세팅
35+
for(int i=0; i<NUM; i++){
36+
StringTokenizer st = new StringTokenizer(br.readLine());
37+
for(int j=0; j<NUM; j++){
38+
int number = Integer.parseInt(st.nextToken());
39+
int direction = Integer.parseInt(st.nextToken())-1;
40+
Chess chess = new Chess(i,j,number,direction);
41+
game[i][j] = chess;
42+
pq.offer(chess);
43+
}
44+
}
45+
//술래 위치 세팅
46+
score = game[0][0].number;
47+
tagger = game[0][0];
48+
game[0][0] = null;
49+
50+
//체스 시작
51+
boolean flag = true;
52+
while(flag){
53+
moveThiefs(); //말 이동
54+
flag = moveTagger(); //술래 이동
55+
}
56+
System.out.println(score);
57+
}
58+
59+
// ↑, ↖, ←, ↙, ↓, ↘, →, ↗
60+
static int[] nx = {-1,-1,0,1,1,1,0,-1};
61+
static int[] ny = {0,-1,-1,-1,0,1,1,1};
62+
public static void moveThiefs(){
63+
while(!pq.isEmpty()){
64+
Chess thief = pq.poll(); //체스 자리 찾을 때까지 이동
65+
int x = thief.x;
66+
int y = thief.y;
67+
//FIXME 자리찾기
68+
findSpace(thief);
69+
70+
//FIXME 자리교체
71+
Chess temp = game[thief.x][thief.y];
72+
game[thief.x][thief.y] = thief;
73+
game[x][y] = temp;
74+
}
75+
//pq 채우기
76+
for(Chess[] rows : game){
77+
for(Chess chess : rows){
78+
pq.offer(chess);
79+
}
80+
}
81+
}
82+
83+
private static void findSpace(Chess chess){
84+
int tempD = chess.direction;
85+
for (int i=0; i<NUM*2; i++){
86+
tempD %= 8;
87+
int tempX = chess.x + nx[tempD];
88+
int tempY = chess.y + ny[tempD];
89+
if(stop(tempX,tempY) || Objects.isNull(game[tempX][tempY])){
90+
tempD++;
91+
continue;
92+
}
93+
chess.x = tempX;
94+
chess.y = tempY;
95+
return;
96+
}
97+
}
98+
99+
//bfs 탐색을 통해 갈 수 있는 곳 중 가장 큰 말 찾기
100+
public static boolean moveTagger(){
101+
Deque<Chess> deque = new ArrayDeque<>();
102+
Chess maxThief = EMPTY;
103+
104+
boolean[][] visited = new boolean[NUM][NUM];
105+
deque.offer(tagger);
106+
visited[tagger.x][tagger.y] = true;
107+
108+
while(!deque.isEmpty()){
109+
Chess tagger = deque.poll();
110+
for (int i=0; i<8; i++){
111+
int tempX = tagger.x + nx[i];
112+
int tempY = tagger.y + ny[i];
113+
//술래말은 도둑말이 없는 곳으로는 이동할 수 없습니다
114+
if(stop(tempX,tempY) || visited[tagger.x][tagger.y] || game[tempX][tempY].number == 0){
115+
continue;
116+
}
117+
118+
visited[tagger.x][tagger.y] = true;
119+
Chess thief = game[tempX][tempY];
120+
deque.offer(thief);
121+
if(thief.number > maxThief.number){
122+
maxThief = thief;
123+
}
124+
}
125+
}
126+
127+
if(maxThief.number != 0){
128+
game[tagger.x][tagger.y] = EMPTY; //기존에 술래가 있던 위치 빈공간으로 만들기
129+
//술래 위치 이동
130+
score += maxThief.number;
131+
tagger = game[maxThief.x][maxThief.y];
132+
game[maxThief.x][maxThief.y] = null;
133+
return true;
134+
}
135+
return false; //도둑말을 잡지 못했다면 게임 종료
136+
}
137+
138+
139+
private static boolean stop(int x, int y){
140+
return x < 0 || x >= NUM || y < 0 || y >= NUM;
141+
}
142+
}

0 commit comments

Comments
(0)

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