Skip to content

Commit 0e1cd56

Browse files
authored
Merge pull request #175 from AlgorithmWithGod/khj20006
[20250225] BOJ / G2 / 어른 상어 / 권혁준
2 parents dd7d25d + f1e1b6f commit 0e1cd56

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Shark{
7+
int id, dir, x, y;
8+
int[][] prior;
9+
Shark(int id, int x, int y){
10+
this.id = id;
11+
this.x = x;
12+
this.y = y;
13+
prior = new int[5][5];
14+
}
15+
}
16+
17+
class Main {
18+
19+
// IO field
20+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
22+
static StringTokenizer st;
23+
24+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
25+
static int nextInt() {return Integer.parseInt(st.nextToken());}
26+
static long nextLong() {return Long.parseLong(st.nextToken());}
27+
static void bwEnd() throws Exception {bw.flush();bw.close();}
28+
29+
// Additional field
30+
static final int[] dx = {0,-1,1,0,0};
31+
static final int[] dy = {0,0,0,-1,1};
32+
33+
static Shark[] sharks;
34+
static boolean[] alive;
35+
static int[][] A;
36+
static int[][] smell_id, smell_time;
37+
static int N, M, K, R;
38+
39+
public static void main(String[] args) throws Exception {
40+
41+
ready();
42+
solve();
43+
44+
bwEnd();
45+
46+
}
47+
48+
static void ready() throws Exception{
49+
50+
nextLine();
51+
N = nextInt();
52+
M = nextInt();
53+
K = nextInt();
54+
smell_id = new int[N][N];
55+
smell_time = new int[N][N];
56+
A = new int[N][N];
57+
sharks = new Shark[M+1];
58+
alive = new boolean[M+1];
59+
Arrays.fill(alive, true);
60+
R = M;
61+
62+
63+
for(int i=0;i<N;i++) {
64+
nextLine();
65+
for(int j=0;j<N;j++) {
66+
A[i][j] = nextInt();
67+
if(A[i][j] != 0) {
68+
Shark temp = new Shark(A[i][j], i, j);
69+
sharks[A[i][j]] = temp;
70+
}
71+
}
72+
}
73+
74+
nextLine();
75+
for(int i=1;i<=M;i++) sharks[i].dir = nextInt();
76+
77+
for(int i=1;i<=M;i++) {
78+
for(int k=1;k<=4;k++) {
79+
nextLine();
80+
for(int j=1;j<=4;j++) sharks[i].prior[k][j] = nextInt();
81+
}
82+
}
83+
84+
}
85+
86+
static void solve() throws Exception{
87+
88+
for(int time=0;time<1000;time++) {
89+
SmellSpread();
90+
MoveSharks();
91+
SmellsTimeSpend();
92+
if(R == 1) {
93+
bw.write((time+1) + "\n");
94+
return;
95+
}
96+
}
97+
bw.write("-1");
98+
99+
}
100+
101+
static void SmellsTimeSpend() {
102+
103+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) {
104+
if(smell_time[i][j] != 0) smell_time[i][j]--;
105+
if(smell_time[i][j] == 0) smell_id[i][j] = 0;
106+
}
107+
108+
}
109+
110+
static void SmellSpread() {
111+
for(int i=1;i<=M;i++) if(alive[i]) {
112+
int x = sharks[i].x, y = sharks[i].y;
113+
smell_id[x][y] = sharks[i].id;
114+
smell_time[x][y] = K;
115+
}
116+
}
117+
118+
static void MoveSharks() {
119+
120+
int[][] NA = new int[N][N];
121+
for(int i=1;i<=M;i++) if(alive[i]) {
122+
int dir = sharks[i].dir;
123+
124+
boolean done = false;
125+
for(int k=1;k<=4;k++) {
126+
int x = sharks[i].x + dx[sharks[i].prior[dir][k]];
127+
int y = sharks[i].y + dy[sharks[i].prior[dir][k]];
128+
if(x<0 || x>=N || y<0 || y>=N || smell_id[x][y] != 0) continue;
129+
done = true;
130+
if(NA[x][y] != 0) {
131+
R--;
132+
if(NA[x][y] < sharks[i].id) {
133+
alive[sharks[i].id] = false;
134+
break;
135+
}
136+
else alive[NA[x][y]] = false;
137+
}
138+
sharks[i].x = x;
139+
sharks[i].y = y;
140+
sharks[i].dir = sharks[i].prior[dir][k];
141+
NA[x][y] = sharks[i].id;
142+
break;
143+
}
144+
145+
if(done) continue;
146+
for(int k=1;k<=4;k++) {
147+
int x = sharks[i].x + dx[sharks[i].prior[dir][k]];
148+
int y = sharks[i].y + dy[sharks[i].prior[dir][k]];
149+
if(x<0 || x>=N || y<0 || y>=N || smell_id[x][y] != sharks[i].id) continue;
150+
sharks[i].x = x;
151+
sharks[i].y = y;
152+
sharks[i].dir = sharks[i].prior[dir][k];
153+
NA[x][y] = sharks[i].id;
154+
break;
155+
}
156+
}
157+
A = NA;
158+
159+
160+
}
161+
162+
}
163+
164+
```

0 commit comments

Comments
 (0)