μ½λνΈλ¦¬ | μ½λ©ν μ€νΈ μ€λΉλ₯Ό μν μκ³ λ¦¬μ¦ μ μ
κ΅κ°λνκ° λ§λ μ½λ© 곡λΆμ κ°μ΄λλΆ μ½λ© μμ΄λ³΄λΆν° κΏμ μ§μ₯ μ½ν ν©κ²©κΉμ§, κ΅κ°λνκ° μμ ν 컀리νλΌμΌλ‘ μ€λΉν΄λ³΄μΈμ.
www.codetree.ai
μ λ¬Έμ μ ν΅μ¬ ꡬνμ¬νμ μλμ κ°μ΅λλ€.
1. 3μ°¨μ μ μ‘면체μ μ΅λ¨κ±°λ¦¬λ₯Ό μ΄λ»κ² ꡬνν μ μμκΉ?
1λ²μ μλͺ»λ μ κ·Ό (μ²μ λμ μκ°)
μ μ‘면체λ₯Ό νλ©΄λλ‘ νΌμ³μ BFSλ‘ μ΅λ¨κ±°λ¦¬λ₯Ό ꡬνλ©΄ λμ§ μμκΉ?
μλ©΄μμ λ€λ₯Έλ©΄μΌλ‘ μ΄λνλ μ΅λ¨κ²½λ‘λ ꡬν μ μμ΅λλ€
νμ§λ§
μλ«λ©΄μμ λ€λ₯Έ λ©΄μΌλ‘ μ΄λνλ κ²½μ°λ ꡬνν μ μμ΅λλ€.
μ΄λ₯Ό κ·Έλ¦ΌμΌλ‘ νννλ©΄ μλμ κ°μ΅λλ€.
μ κ·Έλ¦Όμ²λΌ μλ©΄μμ λ€λ₯Έλ©΄μΌλ‘ μ΄λνλ μ΅λ¨κ²½λ‘ BFSλ‘ κ΅¬νν μ μμ§λ§
νλμ νμ΄ν μ²λΌ μλ©΄μ΄ μλ νλ©΄μμ λ€λ₯Έ μλ©΄μ΄ μλ νλ©΄μΌλ‘ μ΄λνλ κ²½μ°λ₯Ό BFSλ‘ κ΅¬ννμ§ λͺ» νμ΅λλ€.
1λ²μ λν μ¬λ°λ₯Έ μ κ·Ό
κ° νλ©΄( μκ°μ λ°© νλ©΄ 5κ° + λ―Έμ§μ κ³΅κ° νλ©΄ 1κ° )μ μ’νμ λν΄ λ²νΈλ₯Ό ν λΉνκ³ ν λΉλ λ²νΈλ₯Ό νμ©νμ¬ μΈμ νλ ¬μ ꡬννλ€λ©΄ μ΄λ₯Ό ν΄κ²°ν μ μμ΅λλ€.
μΈμ νλ ¬μ κ° μ’νμμ λ λ¨ λΆ μ λ°©ν₯μΌλ‘ μΈμ ν μ’ν λ²νΈλ₯Ό ν λΉν©λλ€.
μ΄λ κ² κ° μ’νμ λν΄ λ²νΈλ₯Ό ν λΉνκ³ λ λ¨ λΆ μ λ°©ν₯μΌλ‘ μΈμ ν λ²νΈλ₯Ό ν΅ν΄ μΈμ νλ ¬ κ·Έλνλ₯Ό λ§λ€ μ μμ΅λλ€.
μλ₯Όλ€μ΄
μ κ·Έλ¦Όμμ 1λ²μ 2 3 4 5 λ²μ μ’νλ‘ μ΄λν μ μμ΅λλ€.
μ΄λ₯Ό 2μ°¨μ μΈμ νλ ¬λ‘ νννλ©΄ μλμ κ°μ΅λλ€.
μ΄μ μ λ΄μ©μ μ½λλ‘ κ΅¬ννλ©΄ μλμ κ°μ΅λλ€.
μ°μ κ° μ’νλ₯Ό μννλ©° λ²νΈλ₯Ό ν λΉν©λλ€.
cnt λ³μλ₯Ό μ¦κ°μν€λ©° λ²νΈλ₯Ό ν λΉν©λλ€.
↓
static void build_graph(int N, int M) {
int cnt = 0;
// κ° μ
μ λν΄ λμλ λ²νΈλ₯Ό μ°¨λ‘λ‘ λΆμ¬ν©λλ€
// νλ©΄λ μ€ μκ°μ λ²½μ΄ μλ λΆλΆμ μ
λ€μ μνν ν,
// λ¨λ©΄λμ μνλ μ
λ€μ λ¨λ©΄λμ λμͺ½ -> λ¨μͺ½ -> μμͺ½ -> λΆμͺ½ -> μμͺ½ μμΌλ‘ μ
λ€μ μννλ©΄μ λ²νΈλ₯Ό λΆμ¬ν©λλ€
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] != 3) { // μκ°μ λ²½μ΄ μλ μ
μ΄ μλ κ²½μ°μλ§ λ²νΈλ₯Ό λΆμ¬ν©λλ€
spaceMapCellId[i][j] = ++cnt;
}
}
}
// λ¨λ©΄λμ λμͺ½, λ¨μͺ½, μμͺ½, λΆμͺ½, μμͺ½ μμΌλ‘ μννλ©° μ
μ λ²νΈλ₯Ό λΆμ¬ν©λλ€
for(int t = 0; t < 5; t++) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
timeWallCellId[t][i][j] = ++cnt;
}
}
}
...
μ΄ν graph μΈμ νλ ¬μ μμ±νκ³ μ΄λν μ μλ μ’νλΌλ¦¬ μ°κ²°ν΄μ€λλ€.
graph μΈμ νλ ¬μ (cnt + 1) x (4) ν¬κΈ°λ‘ μμ±ν©λλ€.
μ΄ν λ―Έμ§μ 곡κ°μ μννλ©° μΈμ νλ ¬μ κ°μ μ λ ₯ν©λλ€.
cnt + 1 λ‘ ν ν¬κΈ°λ₯Ό μ§μ ν μ΄μ λ κ° μ’νμ λν λ²νΈλ₯Ό 1λ² λΆν° ν λΉνκΈ° λλ¬Έμ λλ€.
↓
graph = new int[cnt+1][4];
for(int i = 0; i < cnt+1; i++) {
Arrays.fill(graph[i], -1);
}
// κ°μ μ μΆκ°νλ μμ
μ μν΄ μ¬μ©ν dx, dy λ°°μ΄
// λ, λ¨, λΆ, μμ λμλλ μμλ‘ μ±μμ Έ μμ΅λλ€
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
// νλ©΄λμ μνλ μ
μ λμλλ λ²νΈμ μ μ μμ λν΄ κ°μ μ μΆκ°ν©λλ€
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] != 3) { // νμ¬ μ
μ μ₯μ λ¬Όμ΄ μλ κ²½μ°
int idx = spaceMapCellId[i][j];
// λ, λ¨, λΆ, μ μμΌλ‘ μΈμ ν μ
λ€μ νμν©λλ€
for(int dd = 0; dd < 4; dd++) {
int nx = i + dx[dd];
int ny = j + dy[dd];
// λ²μλ₯Ό λ²μ΄λλ©΄ λμ΄κ°λλ€
if(nx < 0 || ny < 0 || nx >= N || ny >= N) continue;
// μ
μ μ₯μ λ¬Όμ΄ μλ κ²½μ° λμ΄κ°λλ€
if(spaceMap[nx][ny] == 3) continue;
// κ·Έλ μ§ μμ κ²½μ°, μ΄μ΄μ€λλ€
graph[idx][dd] = spaceMapCellId[nx][ny];
}
}
}
}
μκ°μ λ°© λ λ¨ μ λΆ νλ©΄λ κ° (μλ©΄ μ μΈ) μΈμ νλ ¬μ μμ±ν©λλ€.
μ΄λ 5 x M x M ν¬κΈ° 3μ°¨μ λ°°μ΄ timeWallCellIdμ
timeWallCellId[0][ ][ ] : λμͺ½ νλ©΄λ
timeWallCellId[1][ ][ ] : λ¨μͺ½ νλ©΄λ
timeWallCellId[2][ ][ ] : μμͺ½ νλ©΄λ
timeWallCellId[3][ ][ ] : λΆμͺ½ νλ©΄λ
timeWallCellId[4][ ][ ] : μμͺ½ νλ©΄λ
(λ μ λ¨ λΆ) μμκ° μλ (λ λ¨ μ λΆ) μμλ‘μ΄κΈ°ν νκΈ° λλ¬Έμ + μ°μ°μμ % μ°μ°μλ‘ νλ©΄λλ₯Ό μννλ©° μΈμ ν κ²½μ°λ₯Ό ꡬνν μ μμ΅λλ€.
λ¬Έμ μμλ (λ μ λ¨ λΆ) νλ©΄λ μμλλ‘ μ λ ₯μ΄ μ£Όμ΄μ§λλ€.
↓
// λ¨λ©΄λμ λμͺ½, λ¨μͺ½, μμͺ½, λΆμͺ½μ μλ μ
λ€μ΄ μΈμ ν κ²½μ°
// λμλλ λ²νΈμ μ μ λ€μ μ΄μ΄μ€λλ€
for(int t = 0; t < 4; t++) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
int idx = timeWallCellId[t][i][j];
// μμ λΉμ·νκ² 4λ°©ν₯ νμ
for(int dd = 0; dd < 4; dd++) {
int nx = i + dx[dd];
int ny = j + dy[dd];
// ν λ²μκ° λμ΄κ°μ κ²½μ° ν΅κ³Ό
if(nx < 0 || nx >= M) continue;
// μ΄ λ²νΈκ° 0λ³΄λ€ μμμ§ κ²½μ°, μκ³λ°©ν₯μμΌλ‘ νλ μμ μλ λ¨λ©΄λμ κ°μ₯ μ€λ₯Έμͺ½ μ΄μ μ
κ³Ό μΈμ ν©λλ€
if(ny < 0) {
graph[idx][dd] = timeWallCellId[(t+1)%4][nx][M-1];
}
// μ΄ λ²νΈκ° M-1λ³΄λ€ μ»€μ§ κ²½μ°, λ°μκ³λ°©ν₯μμΌλ‘ νλ μμ μλ λ¨λ©΄λμ κ°μ₯ μΌμͺ½ μ΄μ μ
κ³Ό μΈμ ν©λλ€
else if(ny >= M) {
graph[idx][dd] = timeWallCellId[(t+3)%4][nx][0];
}
// κ·Έ μΈμ κ²½μ° νλ©΄λμ κ²½μ°μ²λΌ μ΄μ΄μ€λλ€
else {
graph[idx][dd] = timeWallCellId[t][nx][ny];
}
}
}
}
}
μμͺ½ λ¨λ©΄λμμ μ΄λν μ μλ μ’νκ° μΈμ νλ ¬μ μ λ ₯ν©λλ€.
↓
// μμͺ½ λ¨λ©΄λμ μνλ μ
λ€μ λμλλ λ²νΈμ μ μ μμ λν΄ κ°μ μ μΆκ°ν©λλ€
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
int idx = timeWallCellId[4][i][j];
for(int dd = 0; dd < 4; dd++) {
int nx = i + dx[dd];
int ny = j + dy[dd];
// λ²μλ₯Ό λ²μ΄λ κ²½μ° λμ΄κ°λλ€
if(nx < 0 || ny < 0 || nx >= M || ny >= M) continue;
// κ·Έλ μ§ μμ κ²½μ° μ΄μ΄μ€λλ€
graph[idx][dd] = timeWallCellId[4][nx][ny];
}
}
}
μμͺ½ λ¨λ©΄λμ λ, λ¨, μ, λΆ λ¨λ©΄λλ₯Ό μ΄μ΄μ€λλ€.
↓
// λμͺ½ λ¨λ©΄λμ μΈμ ν μ
λ€μ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][i][M-1]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[0][0][M-1-i]; // μΈμ ν λμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][0] = idy;
graph[idy][3] = idx;
}
// λ¨μͺ½ λ¨λ©΄λμ μΈμ ν μ
λ€μ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][M-1][i]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[1][0][i]; // μΈμ ν λ¨μͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][1] = idy;
graph[idy][3] = idx;
}
// μμͺ½ λ¨λ©΄λμ μΈμ ν μ
λ€μ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][i][0]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[2][0][i]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][2] = idy;
graph[idy][3] = idx;
}
// λΆμͺ½ λ¨λ©΄λμ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][0][i]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[3][0][M-1-i]; // μΈμ ν λΆμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][3] = idy;
graph[idy][3] = idx;
}
νλ©΄λμμ μκ°μ λ²½μ΄ μμνλ μ μ μμΉλ₯Ό νμ©νμ¬ μκ°μ λ°© (λ, λ¨, μ, λΆ) λ©΄κ³Ό λ―Έμ§μ κ³΅κ° νλ©΄λλ₯Ό μ΄μ΄μ€λλ€.
↓
// νλ©΄λμμ μκ°μ λ²½μ΄ μμνλ μ
μ ν λ²νΈ, μ΄ λ²νΈ
int timewallStartx = -1;
int timewallStarty = -1;
// νλ©΄λμμ μκ°μ λ²½μ΄ μμνλ μ
μ μμΉλ₯Ό νμ
outer:
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] == 3) {
timewallStartx = i;
timewallStarty = j;
break outer;
}
}
}
// νλ©΄λμ μΈμ νλ λ¨λ©΄λ μ
λ€μ λμλλ λ²νΈμ μ μ μ μλ κ°μ μΆκ°
// λμͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStarty + M < N) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[0][M-1][i];
int idy = spaceMapCellId[timewallStartx + (M - 1) - i][timewallStarty + M];
graph[idx][1] = idy;
graph[idy][2] = idx;
}
}
// λ¨μͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStartx + M < N) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[1][M-1][i];
int idy = spaceMapCellId[timewallStartx + M][timewallStarty + i];
graph[idx][1] = idy;
graph[idy][3] = idx;
}
}
// μμͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStarty > 0) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[2][M-1][i];
int idy = spaceMapCellId[timewallStartx + i][timewallStarty - 1];
graph[idx][1] = idy;
graph[idy][0] = idx;
}
}
// λΆμͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStartx > 0) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[3][M-1][i];
int idy = spaceMapCellId[timewallStartx - 1][timewallStarty + (M-1) - i];
graph[idx][1] = idy;
graph[idy][1] = idx;
}
}
μκ²λ μ
- μ’νμ λ²νΈλ₯Ό ν λΉνκ³ κ·Έ λ²νΈλ₯Ό λ΄λ μΆκ° μλ£κ΅¬μ‘°λ₯Ό νμ©ν΄ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€.
(ν λΉλ λ²νΈλ‘ μΈμ νλ ¬ ꡬνμ νμ©) - 거리 λ°°μ΄λ‘ μ₯μ λ¬Ό μ²λ¦¬λ₯Ό ν΄μ€ μ μλ€ ( μΈμ νλ ¬μ λ§λ€ λ 미리 κ°μ§ λͺ» νλ μ’νλ₯Ό μ²λ¦¬ν΄μ€ μ λ μμ )
- λͺ« μ°μ°, λλ¨Έμ§ μ°μ°μΌλ‘ μννλ λ‘μ§μ ꡬνν μ μλ€.
- λ°©ν₯벑ν°μ μμλ₯Ό (λ λ¨ μ λΆ) κ³Ό κ°μ΄ λ³κ²½ν΄μ λ¬Έμ ν΄κ²°μ νμ©ν μ μλ€.
μ 체μ½λ
package com.mincheolsong;
import java.util.*;
import java.io.*;
// μκ° μ΄μ νμμ λν μ 보λ₯Ό μ μ₯ν ꡬ쑰체
class AbnormalTimeEvent{
// μκ° μ΄μ νμμ΄ μμμ μ νλ²νΈ, μ΄λ²νΈ, λ°©ν₯, νμ₯ μ£ΌκΈ°, μκ° μ΄μ νμμ μ§νμ¬λΆλ₯Ό μ°¨λ‘λ‘ μ μ₯ν©λλ€
int xpos, ypos, direction, cycle, alive;
}
public class Main {
static final int INF = (int)1e9;
static int[][] spaceMap; // λ―Έμ§μ 곡κ°μ νλ©΄λ
static int[][] spaceMapCellId; // νλ©΄λμ κ° μ
μ λμνλ κ·Έλν μ μ μ λ²νΈλ₯Ό μ μ₯νλ λ°°μ΄
static int[][][] timeWall; // μκ°μ λ²½μ λ¨λ©΄λ
static int[][][] timeWallCellId; // μκ°μ λ²½μ λ¨λ©΄λμ κ° μ
μ λμνλ κ·Έλν μ μ μ λ²νΈλ₯Ό μ μ₯νλ λ°°μ΄
static AbnormalTimeEvent[] events;
// κ·Έλνλ₯Ό μ μ₯ν μΈμ 리μ€νΈ
static int[][] graph;
// νμλ¨Έμ μμ ν΄λΉ λ²νΈμ μ
κΉμ§ λλ¬νλλ° νμν μ΅μ ν΄ νμ
static int[] dist;
// κ·Έλν μμ± ν¨μ
static void build_graph(int N, int M) {
int cnt = 0;
// κ° μ
μ λν΄ λμλ λ²νΈλ₯Ό μ°¨λ‘λ‘ λΆμ¬ν©λλ€
// νλ©΄λ μ€ μκ°μ λ²½μ΄ μλ λΆλΆμ μ
λ€μ μνν ν,
// λ¨λ©΄λμ μνλ μ
λ€μ λ¨λ©΄λμ λμͺ½ -> λ¨μͺ½ -> μμͺ½ -> λΆμͺ½ -> μμͺ½ μμΌλ‘ μ
λ€μ μννλ©΄μ λ²νΈλ₯Ό λΆμ¬ν©λλ€
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] != 3) { // μκ°μ λ²½μ΄ μλ μ
μ΄ μλ κ²½μ°μλ§ λ²νΈλ₯Ό λΆμ¬ν©λλ€
spaceMapCellId[i][j] = ++cnt;
}
}
}
// λ¨λ©΄λμ λμͺ½, λ¨μͺ½, μμͺ½, λΆμͺ½, μμͺ½ μμΌλ‘ μννλ©° μ
μ λ²νΈλ₯Ό λΆμ¬ν©λλ€
for(int t = 0; t < 5; t++) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
timeWallCellId[t][i][j] = ++cnt;
}
}
}
// λΆμ¬ν λ²νΈμ μ μ λ€λ‘ ꡬμ±λ κ·Έλν
// μ μ μ λ²νΈμ λμλλ μ
κ³Ό μΈμ ν μ
μ λ²νΈλ₯Ό κ°μ§λ μ μ κ³Ό κ°μ μΌλ‘ μ°κ²°ν©λλ€
// μ΅λ 4κ°μ μ μ κ³Ό μ°κ²°λ μ μμ΅λλ€
// νλ©΄λ(λ¨λ©΄λ)μμ, μ€λ₯Έμͺ½μΌλ‘ μΈμ ν κ²½μ° 0, μλμͺ½μΌλ‘ μΈμ ν κ²½μ° 1, μΌμͺ½μΌλ‘ μΈμ ν κ²½μ° 2, μμͺ½μΌλ‘ μΈμ ν κ²½μ° 3μ μΈλ±μ€μ μ μ₯ν©λλ€
graph = new int[cnt+1][4];
for(int i = 0; i < cnt+1; i++) {
Arrays.fill(graph[i], -1);
}
// κ°μ μ μΆκ°νλ μμ
μ μν΄ μ¬μ©ν dx, dy λ°°μ΄
// λ, λ¨, λΆ, μμ λμλλ μμλ‘ μ±μμ Έ μμ΅λλ€
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
// νλ©΄λμ μνλ μ
μ λμλλ λ²νΈμ μ μ μμ λν΄ κ°μ μ μΆκ°ν©λλ€
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] != 3) { // νμ¬ μ
μ μ₯μ λ¬Όμ΄ μλ κ²½μ°
int idx = spaceMapCellId[i][j];
// λ, λ¨, λΆ, μ μμΌλ‘ μΈμ ν μ
λ€μ νμν©λλ€
for(int dd = 0; dd < 4; dd++) {
int nx = i + dx[dd];
int ny = j + dy[dd];
// λ²μλ₯Ό λ²μ΄λλ©΄ λμ΄κ°λλ€
if(nx < 0 || ny < 0 || nx >= N || ny >= N) continue;
// μ
μ μ₯μ λ¬Όμ΄ μλ κ²½μ° λμ΄κ°λλ€
if(spaceMap[nx][ny] == 3) continue;
// κ·Έλ μ§ μμ κ²½μ°, μ΄μ΄μ€λλ€
graph[idx][dd] = spaceMapCellId[nx][ny];
}
}
}
}
// λ¨λ©΄λμ λμͺ½, λ¨μͺ½, μμͺ½, λΆμͺ½μ μλ μ
λ€μ΄ μΈμ ν κ²½μ°
// λμλλ λ²νΈμ μ μ λ€μ μ΄μ΄μ€λλ€
for(int t = 0; t < 4; t++) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
int idx = timeWallCellId[t][i][j];
// μμ λΉμ·νκ² 4λ°©ν₯ νμ
for(int dd = 0; dd < 4; dd++) {
int nx = i + dx[dd];
int ny = j + dy[dd];
// ν λ²μκ° λμ΄κ°μ κ²½μ° ν΅κ³Ό
if(nx < 0 || nx >= M) continue;
// μ΄ λ²νΈκ° 0λ³΄λ€ μμμ§ κ²½μ°, μκ³λ°©ν₯μμΌλ‘ νλ μμ μλ λ¨λ©΄λμ κ°μ₯ μ€λ₯Έμͺ½ μ΄μ μ
κ³Ό μΈμ ν©λλ€
if(ny < 0) {
graph[idx][dd] = timeWallCellId[(t+1)%4][nx][M-1];
}
// μ΄ λ²νΈκ° M-1λ³΄λ€ μ»€μ§ κ²½μ°, λ°μκ³λ°©ν₯μμΌλ‘ νλ μμ μλ λ¨λ©΄λμ κ°μ₯ μΌμͺ½ μ΄μ μ
κ³Ό μΈμ ν©λλ€
else if(ny >= M) {
graph[idx][dd] = timeWallCellId[(t+3)%4][nx][0];
}
// κ·Έ μΈμ κ²½μ° νλ©΄λμ κ²½μ°μ²λΌ μ΄μ΄μ€λλ€
else {
graph[idx][dd] = timeWallCellId[t][nx][ny];
}
}
}
}
}
// μμͺ½ λ¨λ©΄λμ μνλ μ
λ€μ λμλλ λ²νΈμ μ μ μμ λν΄ κ°μ μ μΆκ°ν©λλ€
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
int idx = timeWallCellId[4][i][j];
for(int dd = 0; dd < 4; dd++) {
int nx = i + dx[dd];
int ny = j + dy[dd];
// λ²μλ₯Ό λ²μ΄λ κ²½μ° λμ΄κ°λλ€
if(nx < 0 || ny < 0 || nx >= M || ny >= M) continue;
// κ·Έλ μ§ μμ κ²½μ° μ΄μ΄μ€λλ€
graph[idx][dd] = timeWallCellId[4][nx][ny];
}
}
}
// μμͺ½ λ¨λ©΄λμ μΈμ ν λμͺ½, λ¨μͺ½, μμͺ½, λΆμͺ½ λ¨λ©΄λμ μ
λ€μ λν΄
// λμλλ λ²νΈμ μ μ μ μλ κ°μ μ μΆκ°ν©λλ€
// λμͺ½ λ¨λ©΄λμ μΈμ ν μ
λ€μ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][i][M-1]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[0][0][M-1-i]; // μΈμ ν λμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][0] = idy;
graph[idy][3] = idx;
}
// λ¨μͺ½ λ¨λ©΄λμ μΈμ ν μ
λ€μ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][M-1][i]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[1][0][i]; // μΈμ ν λ¨μͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][1] = idy;
graph[idy][3] = idx;
}
// μμͺ½ λ¨λ©΄λμ μΈμ ν μ
λ€μ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][i][0]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[2][0][i]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][2] = idy;
graph[idy][3] = idx;
}
// λΆμͺ½ λ¨λ©΄λμ κ²½μ°
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[4][0][i]; // μΈμ ν μμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
int idy = timeWallCellId[3][0][M-1-i]; // μΈμ ν λΆμͺ½ λ¨λ©΄λμ μ
μ λ²νΈ
graph[idx][3] = idy;
graph[idy][3] = idx;
}
// νλ©΄λμμ μκ°μ λ²½μ΄ μμνλ μ
μ ν λ²νΈ, μ΄ λ²νΈ
int timewallStartx = -1;
int timewallStarty = -1;
// νλ©΄λμμ μκ°μ λ²½μ΄ μμνλ μ
μ μμΉλ₯Ό νμ
outer:
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] == 3) {
timewallStartx = i;
timewallStarty = j;
break outer;
}
}
}
// νλ©΄λμ μΈμ νλ λ¨λ©΄λ μ
λ€μ λμλλ λ²νΈμ μ μ μ μλ κ°μ μΆκ°
// λμͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStarty + M < N) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[0][M-1][i];
int idy = spaceMapCellId[timewallStartx + (M - 1) - i][timewallStarty + M];
graph[idx][1] = idy;
graph[idy][2] = idx;
}
}
// λ¨μͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStartx + M < N) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[1][M-1][i];
int idy = spaceMapCellId[timewallStartx + M][timewallStarty + i];
graph[idx][1] = idy;
graph[idy][3] = idx;
}
}
// μμͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStarty > 0) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[2][M-1][i];
int idy = spaceMapCellId[timewallStartx + i][timewallStarty - 1];
graph[idx][1] = idy;
graph[idy][0] = idx;
}
}
// λΆμͺ½ λ¨λ©΄λμ κ²½μ°
if(timewallStartx > 0) {
for(int i = 0; i < M; i++) {
int idx = timeWallCellId[3][M-1][i];
int idy = spaceMapCellId[timewallStartx - 1][timewallStarty + (M-1) - i];
graph[idx][1] = idy;
graph[idy][1] = idx;
}
}
return;
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N, M, E; // λ―Έμ§μ 곡κ°μ ν λ³μ κΈΈμ΄, μκ°μ λ²½ ν λ³μ κΈΈμ΄, μκ° μ΄μ νμμ κ°μ
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
spaceMap = new int[N][N];
spaceMapCellId = new int[N][N];
timeWall = new int[5][M][M];
timeWallCellId = new int[5][M][M];
events = new AbnormalTimeEvent[E];
// 곡κ°μ νλ©΄λ μ
λ ₯
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < N; j++) {
spaceMap[i][j] = Integer.parseInt(st.nextToken());
}
}
// μκ°μ λ²½μ λμͺ½ λ¨λ©΄λ μ
λ ₯
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
timeWall[0][i][j] = Integer.parseInt(st.nextToken());
}
}
// μκ°μ λ²½μ μμͺ½ λ¨λ©΄λ μ
λ ₯
// ꡬνμ νΈμλ₯Ό μν΄μ TimeWall[2][][]μ μμͺ½ λ¨λ©΄λ μ 보 μ μ₯
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
timeWall[2][i][j] = Integer.parseInt(st.nextToken());
}
}
// μκ°μ λ²½μ λ¨μͺ½ λ¨λ©΄λ μ
λ ₯
// ꡬνμ νΈμλ₯Ό μν΄μ TimeWall[1][][]μ λ¨μͺ½ λ¨λ©΄λ μ 보 μ μ₯
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
timeWall[1][i][j] = Integer.parseInt(st.nextToken());
}
}
// μκ°μ λ²½μ λΆμͺ½ λ¨λ©΄λ μ
λ ₯
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
timeWall[3][i][j] = Integer.parseInt(st.nextToken());
}
}
// μκ°μ λ²½μ μμͺ½ λ¨λ©΄λ μ
λ ₯
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
timeWall[4][i][j] = Integer.parseInt(st.nextToken());
}
}
// μκ° μ΄μ νμμ λν μ 보 μ
λ ₯
for(int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
events[i] = new AbnormalTimeEvent();
events[i].xpos = Integer.parseInt(st.nextToken());
events[i].ypos = Integer.parseInt(st.nextToken());
events[i].direction = Integer.parseInt(st.nextToken());
events[i].cycle = Integer.parseInt(st.nextToken());
if(events[i].direction == 1) events[i].direction = 2;
else if(events[i].direction == 2) events[i].direction = 1;
events[i].alive = 1;
}
// 곡κ°μ μν©μ λμλλ κ·Έλν μμ±
build_graph(N, M);
// μμ±λ κ·Έλνμ μ μ μ κ°μλ
// N*N - M*M + 5*M*M = N*N + 4*M*M κ°
// -1λ‘ λ°°μ΄μ κ°μ μ΄κΈ°ννλ€.
// int maxNodes = N*N + 4*M*M + 1;
dist = new int[graph.length];
Arrays.fill(dist, -1);
// μ₯μ λ¬ΌμΈ κ²½μ° λλ¬ν μ μμΌλ―λ‘ λ―Έλ¦¬ μμ£Ό ν° κ°μΌλ‘ μΈν
ν©λλ€
// νλ©΄λμ μλ μ₯μ λ¬Όμ κ²½μ°
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] == 3) continue;
if(spaceMap[i][j] == 1) {
int idx = spaceMapCellId[i][j];
dist[idx] = INF;
}
}
}
// νλ©΄λμμ μκ° μ΄μ νμμ μμμ λ λλ¬ λΆκ°λ₯νλ―λ‘ μ₯μ λ¬Όκ³Ό κ°μ΄ μ²λ¦¬ν©λλ€
for(int i = 0; i < E; i++) {
int x = events[i].xpos;
int y = events[i].ypos;
int idx = spaceMapCellId[x][y];
dist[idx] = INF;
}
// λ¨λ©΄λ μμ μλ μ₯μ λ¬Όμ κ²½μ° μμ λκ°μ΄ μ²λ¦¬ν©λλ€
for(int t = 0; t < 5; t++) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
if(timeWall[t][i][j] == 1) {
int idx = timeWallCellId[t][i][j];
dist[idx] = INF;
}
}
}
}
// BFSλ₯Ό μ§νν ν
Deque<Integer> q = new ArrayDeque<Integer>();
int cell_start = -1;
int cell_end = -1;
// νμΆκ΅¬ μμΉ νμ
outer1:
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(spaceMap[i][j] == 4) {
cell_end = spaceMapCellId[i][j];
break outer1;
}
}
}
// νμλ¨Έμ μ μμμ νμ
outer2:
for(int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
if(timeWall[4][i][j] == 2) {
cell_start = timeWallCellId[4][i][j];
break outer2;
}
}
}
q.offer(cell_start);
dist[cell_start] = 0;
int runs = 1;
while(!q.isEmpty()) {
// νμ¬ ν΄μ νμ₯νλ μ΄μνμμ΄ μμΌλ©΄ μν₯μ λ°λ μ
μ μ
λ°μ΄νΈν©λλ€
for(int i = 0; i < E; i++) {
if(events[i] == null) continue;
if(events[i].alive == 0) continue; // λ μ΄μ νμ°νμ§ μλ μ΄μ νμμ΄λ©΄ λμ΄κ°λλ€
if(runs % events[i].cycle != 0) continue; // μ§κΈ ν΄μ νμ°νμ§ μμΌλ©΄ λμ΄κ°λλ€
// μ΄μ νμμ μμμ
int nx = events[i].xpos;
int ny = events[i].ypos;
// μ΄μνμμ λ°©ν₯μ λ°λΌ μν₯μ λ°λ μ
μ μ’νλ₯Ό ꡬν©λλ€
if(events[i].direction == 0) {
ny += (runs / events[i].cycle);
}
else if(events[i].direction == 1) {
nx += (runs / events[i].cycle);
}
else if(events[i].direction == 2) {
ny -= (runs / events[i].cycle);
}
else {
nx -= (runs / events[i].cycle);
}
if(nx < 0 || ny < 0 || nx >= N || ny >= N) {
events[i].alive = 0;
continue;
}
// μ΄μνμμ΄ μ₯μ λ¬Όμ΄λ νμΆκ΅¬, μκ°μ λ²½μ λ§λ κ²½μ°, νμ°νμ§ μμ΅λλ€
if(spaceMap[nx][ny] != 0) {
events[i].alive = 0;
continue;
}
// λμλλ μ
μ λ²νΈλ μ΄μ νμλ¨Έμ μ΄ λλ¬ν μ μμ΅λλ€
int idx = spaceMapCellId[nx][ny];
dist[idx] = INF;
}
// μ΄λ²ν΄μ λλ¬ κ°λ₯ν μ
λ€μ μ°Ύμ΅λλ€
int size = q.size();
for(int s=0;s<size;s++) {
int idx = q.pollFirst();
for(int i = 0; i < 4; i++) {
int idy = graph[idx][i];
if(idy == -1) continue; // ν΄λΉ λ°©ν₯μΌλ‘ μ΄λκ°λ₯ν μ
μλ κ²½μ° ν΅κ³Όν©λλ€
if(dist[idy] != -1) continue; // μ΄λ―Έ μ΅μ ν΄μ μλ₯Ό κ³μ°ν μ
μ κ²½μ° ν΅κ³Όν©λλ€
dist[idy] = runs;
q.offer(idy); // μ΄λ²μ μλ‘ λλ¬ κ°λ₯ν μ
μ λ²νΈλ₯Ό μΆκ°ν©λλ€
}
}
// νμΆκ΅¬μ κ°κΈ° μν΄ νμν μ΅μ ν΄μλ₯Ό ꡬνλ€λ©΄, μ’
λ£ν©λλ€.
if(dist[cell_end] != -1) {
break;
}
runs+= 1;
}
// μ λ΅μ μΆλ ₯ν©λλ€.
// λΆκ°λ₯νλ©΄ -1μ΄ μΆλ ₯λ©λλ€
if(dist[cell_end] == -1 || dist[cell_end] >= INF) {
System.out.println(-1);
} else {
System.out.println(dist[cell_end]);
}
}
}
'πμ½λ©ν μ€νΈ:CodingTest' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
PCCP λͺ¨μκ³ μ¬1ν 3λ² (0) | 2024.12.11 |
---|---|
λ©λμ¬μ μ μ¬λ€ (0) | 2024.12.04 |
μμ΄(nκ³Όm λ¬Έμ ) μ΅μ ν μν€κΈ° && μ‘°ν© (0) | 2024.10.29 |
[HSAT 4ν μ κΈ° μ½λ© μΈμ¦νκ° κΈ°μΆ] ν΅κ·Όλ²μ€ μΆλ° μμ κ²μ¦νκΈ° (0) | 2024.06.28 |
[HSAT 1ν μ κΈ° μ½λ© μΈμ¦νκ° κΈ°μΆ] λ‘λ΄μ΄ μ§λκ° κ²½λ‘ (0) | 2024.06.28 |