⨠ꡬν
https://www.acmicpc.net/problem/17837
17837λ²: μλ‘μ΄ κ²μ 2
μ¬νμ΄λ μ£Όλ³μ μ΄ν΄λ³΄λ μ€ μ²΄μ€νκ³Ό λ§μ μ΄μ©ν΄μ μλ‘μ΄ κ²μμ λ§λ€κΈ°λ‘ νλ€. μλ‘μ΄ κ²μμ ν¬κΈ°κ° N×NμΈ μ²΄μ€νμμ μ§νλκ³ , μ¬μ©νλ λ§μ κ°μλ Kκ°μ΄λ€. λ§μ μνλͺ¨μμ΄κ³ , ν
www.acmicpc.net
π κ³ λ €ν΄μΌν μ
- ν μ’ν μμ μ¬λ¬ κ°μ λ§μ΄ λ€μ΄κ° μ μλ€. β μ΄λνλ €λ μΉΈμ λ§μ΄ μ‘΄μ¬νλ€λ©΄ κ·Έ μμ λ§μ΄ μ¬λΌκ°
- ν λ§μ΄ μ΄λν λ μμ μ¬λ €μ Έ μλ λ§κΉμ§ λͺ¨λ μ΄λνλ€.
- μ¬λ¬ κ°μ λ§μ΄ λ€μ΄κ° λ μ’νμ μκΉμ λ°λΌ μ λ ¬ κΈ°μ€μ΄ μ‘΄μ¬νλ€.
- μ’νμ λ²μλ₯Ό λ²μ΄λκ±°λ μ’ν μΉΈμ΄ νλμμΌ λμ 쑰건μ μ μν΄μΌνλ€.
πΉοΈ νμ΄κ³Όμ
μ΄λ€ μλ£κ΅¬μ‘°λ‘ νμ©ν μ§ μ νκΈ°λ§ νλ©΄ μ½κ² ν리λ λ¬Έμ μλ€.
- νλμ λ§μ΄ μ΄λν λ μμ μ¬λ €μ Έ μλ λ§κΉμ§ λͺ¨λ μ΄λν΄μΌνκΈ° λλ¬Έμ
ArrayList<Integer>[][] horseInfo
λ‘ μ μΈνλ€. - μ΄λν΄μΌν μΉΈμ΄ λΉ¨κ°μμ΄λΌλ©΄ λ§μ΄ μμ¬μλ μμλ₯Ό λ°λλ‘ λ°κΏμΌ νκ³ , ν°μμ΄λΌλ©΄ κ·Έλλ‘ μμμΌνλ€. π μμκ° μλ€λ‘ λ°κΎΈλ κ²½μ°κ° λ°μν κ²μ΄λ μμλ₯Ό μλ€μμ λͺ¨λ λ½μ μ μλ
Deque
λ₯Ό μ¬μ©νκΈ°λ‘ κ²°μ νλ€.
μκ³ λ¦¬μ¦ κ³Όμ μ λ€μκ³Ό κ°λ€.
- iλ²μ§Έμ λ§μ΄ μμΉν μΉΈμ μλ λͺ¨λ λ§λ€μ μμλλ‘ λ½μ Dequeμ μ μ₯νλ€.
- μμΉν μΉΈμ μλ λͺ¨λ λ§λ€μ μ κ±°νλ€.
- λ§λ€μ μ΄λμν€λ €λ μ’νμ μκΉμ΄ νλμμ΄κ±°λ λ²μλ₯Ό λ²μ΄λλ€λ©΄ iλ²μ§Έ λ§μ λ°©ν₯μ λ°λλ‘ λ°κΎΌλ€. π λ°κΎΈκ³ λμλ μ΄λνλ €λ μΉΈμ΄ λ νλμμ΄κ±°λ λ²μλ₯Ό λ²μ΄λλ€λ©΄ μμ§μ΄μ§ μλλ€.
- μ’νμ μκΉμ΄ λΉ¨κ°μμ΄κ±°λ ν°μμ΄λ©΄ μ΄λμν¨ ν μμλ₯Ό 쑰건μ λ§λλ‘ μ λ ¬νλ€. π
deque.pollFirst()
λλdeque.pollLast()
- μ΄ κ³Όμ μ λ°λ³΅νλ, ν μΉΈμ 4κ° μ΄μμ λ§μ΄ μ‘΄μ¬νκ±°λ turn νμκ° 1000λ²μ λμ΄κ°λ€λ©΄ λ°λ³΅λ¬Έμ μ’ λ£νλ€.
π©π» μ½λ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class BOJ_17837_μλ‘μ΄κ²μ2 {
static int N, K;
static int[][] board;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {1, -1, 0, 0};
static Info[] horse;
static ArrayList<Integer>[][] horseInfo;
static Deque<Integer> dq = new LinkedList<>();
static class Info{
int x, y, d, n;
public Info(int x, int y, int d, int n) {
this.x=x;
this.y=y;
this.d=d;
this.n=n;
}
}
static boolean simulate() {
for(int i=0; i<K; i++) {
Info now = horse[i];
int size = horseInfo[now.x][now.y].size();
if(size>=4) return true;
dq = new LinkedList<>();
// νμ¬ λ§ μμ μλ λ§λ€
for(int j=size-1; j>=0; j--) {
int idx = horseInfo[now.x][now.y].get(j);
dq.add(idx);
horseInfo[now.x][now.y].remove(horseInfo[now.x][now.y].size()-1);
if(idx==now.n) break;
}
int nx = now.x+dx[now.d];
int ny = now.y+dy[now.d];
// νλμμ΄κ±°λ λ²μλ₯Ό λ²μ΄λλ€λ©΄
if(nx<0 || ny<0 || nx>=N || ny>=N || board[nx][ny]==2){
// νμ¬ λ§μ λ°©ν₯μ λ°λλ‘
if(now.d==1 || now.d==3) now.d--;
else now.d++;
nx = now.x+dx[now.d];
ny = now.y+dy[now.d];
if(nx<0 || ny<0 || nx>=N || ny>=N || board[nx][ny]==2) {
while(!dq.isEmpty()) {
int n = dq.pollLast();
horseInfo[now.x][now.y].add(n);
}
continue;
}
}
// μ΄λν μΉΈμ΄ ν°μμ΄λΌλ©΄
if(board[nx][ny]==0) {
while(!dq.isEmpty()) {
int n = dq.pollLast();
horseInfo[nx][ny].add(n);
horse[n].x=nx;
horse[n].y=ny;
}
}
// λΉ¨κ°μμ΄λΌλ©΄
else if(board[nx][ny]==1) {
while(!dq.isEmpty()) {
int n = dq.pollFirst();
horseInfo[nx][ny].add(n);
horse[n].x=nx;
horse[n].y=ny;
}
}
if(horseInfo[nx][ny].size()>=4) return true;
}
return false;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
board = new int[N][N];
horse = new Info[K];
horseInfo = new ArrayList[N][N];
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
horseInfo[i][j] = new ArrayList<>();
}
}
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<N; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i=0; i<K; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
int d = Integer.parseInt(st.nextToken())-1;
horse[i] = new Info(x, y, d, i);
horseInfo[x][y].add(i);
}
int answer=0;
while(true) {
if(simulate())break;
answer++;
if(answer>1000)break;
}
if(answer>1000) System.out.println(-1);
else System.out.println(answer+1);
}
}
'μκ³ λ¦¬μ¦ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 14503 λ‘λ΄μ²μκΈ° - Java (0) | 2024.04.11 |
---|---|
[BOJ] 19238 μ€ννΈ νμ - Java (0) | 2024.04.11 |
[BOJ] 17485 μ§μ°μ λ¬ μ¬ν (Large) - Java (0) | 2024.03.12 |
[BOJ] 20058 λ§λ²μ¬ μμ΄μ νμ΄μ΄μ€ν° - Java (0) | 2024.03.01 |
[BOJ] 21609 μμ΄ μ€νκ΅ - Java (0) | 2024.02.26 |