๋ฐ์ํ
https://www.acmicpc.net/problem/19237
๋ฌธ์ ์ค๋ช
- ๋งค ์ด๋ง๋ค ์์ง์ด๋ฉฐ ๋์๋ฅผ ๋ฟ๋ฆฌ๋ M๋ง๋ฆฌ์ ์์ด ์กด์ฌ
- ๊ฐ ์์ด๋ค์ ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ๊ณผ ๊ธฐ์ค์ ๋ฐ๋ผ ๋ค์ ์์ง์ผ ๋ฐฉํฅ ์ ํ
- ์ธ์ ํ ์นธ ์ค ์๋ฌด ๋์๊ฐ ์๋ ์นธ์ ๋ฐฉํฅ์ผ๋ก
- ์๋ค๋ฉด, ์์ ์ ๋์ธ๊ฐ ์๋ ์นธ์ ๋ฐฉํฅ์ผ๋ก
- ์ด๋ฌํ ์นธ๋ค์ด ์ฌ๋ฌ ๊ฐ๋ผ๋ฉด, ๊ฐ ์์ด์ ๋ฐ๋ฅธ ํน์ ํ ์ฐ์ ์์๋ฅผ ๋ฐ๋ฅธ๋ค.
- ๊ฐ์ ๊ฒฉ์์ ๋ค์ด๊ฐ๊ฒ ๋๋ฉด ๋ฒํธ๊ฐ ์์ ์์ด๋ง ๋จ์
- 1๋ฒ ์์ด๋ง ๋จ๊ธฐ๊น์ง ๊ฑธ๋ฆฌ๋ ์๊ฐ์??
- 1000์ด๊ฐ ๋์ผ๋ฉด -1
๋ฌธ์ ํ์ด
Shark Class
- ์์ด์ ์ขํ, ํ์ฌ ๋ฑกํฅ์ ๊ฐ์
remainingTimeOfSmell, sharkNumOfSmell, priorityOfDir, sharks
remainingTimeOfSmell
: ๊ฐ ์นธ ๋์์ ๋จ์ ์๊ฐsharkNumOfSmell
: ๊ฐ ์นธ ๋์์ ์์ด์ ๋ฒํธpriorityOfDir
: ๊ฐ ์์ด์ ๋ฐฉํฅ๋ณ ์ฐ์ ์์sharks
: ์์ด๋ค์ ์ ๋ณด๋ฅผ ์ ์ฅํ ๋ฐฐ์ด
Solve()
- ๋จ์ ์์ด๊ฐ 1๋ง๋ฆฌ๋ผ๋ฉด ์ข ๋ฃ!
- ์๊ฐ์ด 1000์ด ์ง๋ฌ๋ค๋ฉด -1 ์ถ๋ ฅ ํ ์ข ๋ฃ!
- ๋๋ค ์๋๋ผ๋ฉด ์์ด ์์ง์ด๊ธฐ
3-1.moveShark(tmp, shark)
3-1-1. ๋ฐฉํฅ์ ์ฐ์ ์์ ์์๋๋ก ํ์ํ๊ณ , ๋ฒ์ ๋ด๊ณ ๋์๊ฐ ์๋ค๋ฉด ๐ flag = true ํ, break 3-1-2. ์ฐพ์ง ๋ชปํ์ฑ ์ข ๋ฃ๋๋ฉด`if(!flag)`, ๋ค์ ๋ณธ์ธ ๋์๋ฅผ ์ฐพ๊ธฐ 3-1-3. 1,2 ๋ ์ค์์ ์ด๋ ๊ฐ๋ฅํ ์นธ์ ์ฐพ์๋ค๋ฉด, ์ด๋ 3-1-4. ์๋ค๋ฉด, ์์ด ์ ๊ฑฐโผ๏ธ
- ๋์ ์๊ฐ -1 ์ํค๊ธฐ
- tim += 1
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_BOJ_19237_์ด๋ฅธ_์์ด {
static class Shark{
int x;
int y;
int dir;
public Shark(int x, int y, int dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
}
static int N, M, k;
static int[][] remainingTimeOfSmell, sharkNumOfSmell;
static int[][][] priorityOfDir;
static Shark[] sharks;
static int[] dx = {0, -1, 1, 0, 0};
static int[] dy = {0, 0, 0, -1, 1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
N = Integer.parseInt(stringTokenizer.nextToken());
M = Integer.parseInt(stringTokenizer.nextToken());
k = Integer.parseInt(stringTokenizer.nextToken());
remainingTimeOfSmell = new int[N][N];
sharkNumOfSmell = new int[N][N];
priorityOfDir = new int[M + 1][5][4];
sharks = new Shark[M + 1];
for (int i = 0; i < N; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
int tmp = Integer.parseInt(stringTokenizer.nextToken());
if (tmp > 0) {
sharks[tmp] = new Shark(i, j, 0);
remainingTimeOfSmell[i][j] = k;
sharkNumOfSmell[i][j] = tmp;
}
}
}
stringTokenizer = new StringTokenizer(br.readLine());
for (int i = 1; i <= M; i++) {
sharks[i].dir = Integer.parseInt(stringTokenizer.nextToken());
}
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= 4; j++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int l = 0; l < 4; l++) {
priorityOfDir[i][j][l] = Integer.parseInt(stringTokenizer.nextToken());
}
}
}
System.out.println(Solve());
}
private static int Solve() {
int time = 0;
while (true) {
int cnt = 0;
for (int i = 1; i <= M; i++) {
if (sharks[i] != null) {
cnt += 1;
}
}
if(cnt == 1 && sharks[1] != null) return time;
if(time >= 1000) return -1;
int[][] tmp = new int[N][N];
for (int shark = 1; shark <= M; shark++) {
if(sharks[shark] != null) moveShark(tmp, shark);
}
// ๋์ ์ ํจ๊ธฐ๊ฐ ํ๋์ฉ ์ค์ด๊ธฐ
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(remainingTimeOfSmell[i][j] > 0) remainingTimeOfSmell[i][j] -= 1;
if(remainingTimeOfSmell[i][j] == 0) sharkNumOfSmell[i][j] = 0;
}
}
// ์ด๋ ํ์ ์์ด ์์น์ ๋์ ์ ๋ณด์ ์ ํจ๊ธฐ๊ฐ ์ด๊ธฐํํ๊ธฐ
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (tmp[i][j] > 0) {
remainingTimeOfSmell[i][j] = k;
sharkNumOfSmell[i][j] = tmp[i][j];
}
}
}
time += 1;
}
}
private static void moveShark(int[][] tmp, int shark) {
int d = 0;
int nx = 0;
int ny = 0;
boolean flag = false;
// 1-1. ๋์ ์ฐ์ ์์๋ถํฐ ์ฐจ๋ก๋๋ก ํ์
for (int i = 0; i < 4; i++) {
d = priorityOfDir[shark][sharks[shark].dir][i];
nx = sharks[shark].x + dx[d];
ny = sharks[shark].y + dy[d];
if(nx < 0 || ny < 0 || N <= nx || N <= ny) continue;
if ((0 <= nx && nx < N) && (0 <= ny && ny < N) && sharkNumOfSmell[nx][ny] == 0) {
flag = true;
break;
}
}
// 1-2. ๋์๊ฐ ์๋ ๊ณณ์ด ์๋ ๊ฒฝ์ฐ
if (!flag) {
for (int i = 0; i < 4; i++) {
d = priorityOfDir[shark][sharks[shark].dir][i];
nx = sharks[shark].x + dx[d];
ny = sharks[shark].y + dy[d];
if(nx < 0 || ny < 0 || N <= nx || N <= ny) continue;
if ((0 <= nx && nx < N) && (0 <= ny && ny < N) && sharkNumOfSmell[nx][ny] == shark) break;
}
}
if ((0 <= nx && nx < N) && (0 <= ny && ny < N) && tmp[nx][ny] == 0) {
tmp[nx][ny] = shark;
sharks[shark].x = nx;
sharks[shark].y = ny;
sharks[shark].dir = d;
} else {
sharks[shark] = null;
}
}
}
728x90
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ BOJ / ๋ฐฑ์ค 17143 ] ๋์์ ( ์๋ฐ / JAVA ) (0) | 2022.02.09 |
---|---|
[ ๋ฐฑ์ค / BOJ 19236 ] ์ฒญ์๋ ์์ด ( ์๋ฐ / JAVA ) (0) | 2022.02.08 |
[ ๋ฐฑ์ค / BOJ 17822 ] ์ํ ๋๋ฆฌ๊ธฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.07 |
[ BOJ / ๋ฐฑ์ค 20058 ] ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด์คํฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.05 |
[ BOJ / ๋ฐฑ์ค 19238 ] ์คํํธ ํ์ ( ์๋ฐ / JAVA ) (0) | 2022.02.04 |