⨠ꡬν & BFS(μ΅λ¨κ±°λ¦¬)
https://www.acmicpc.net/problem/19238
19238λ²: μ€ννΈ νμ
첫 μ€μ N, M, κ·Έλ¦¬κ³ μ΄κΈ° μ°λ£μ μμ΄ μ£Όμ΄μ§λ€. (2 ≤ N ≤ 20, 1 ≤ M ≤ N2, 1 ≤ μ΄κΈ° μ°λ£ ≤ 500,000) μ°λ£λ 무νν λ§μ΄ λ΄μ μ μκΈ° λλ¬Έμ, μ΄κΈ° μ°λ£μ μμ λμ΄μ μΆ©μ λ μλ μλ€. λ€
www.acmicpc.net
π κ³ λ €ν΄μΌν μ
- νμμ μμΉμμλΆν° μΉκ°κΉμ§μ μ΅λ¨κ±°λ¦¬λ₯Ό ꡬνλ€. π μκ°μ΄κ³Ό μ£Όμ !
- μ΅λ¨κ±°λ¦¬κ° λμΌν μΉκ°λ€μ΄ μ‘΄μ¬ν κ²½μ° κ·Έ μ€ ν λ²νΈκ° κ°μ₯ μμ μΉκ°μ, κ·Έλ° μΉκ°λ μ¬λ¬ λͺ μ΄λΌλ©΄ μ΄ λ²νΈκ° κ°μ₯ μμ μΉκ°μκ² μ΄λνλ€. π PriorityQueue νμ©
- λ²½μ μν΄ μΉκ°μκ² κ°μ§ λͺ»νκ±°λ λͺ©μ μ§μ κ°μ§ λͺ»νλ μν© λν λ°μνλ€. π λͺ©μ μ§λ‘ μ΄λν λ κ³ λ €νμ§ μμ νλ Έμλ€..
- νμκ° μΉκ°μ λͺ©μ μ§λ‘ μ΄λνμμ λ κ·Έ μμΉλ λ€λ₯Έ μΉκ°μ μμμ μΌ μ μλ€. π μ΄λ΄ κ²½μ° νμλ κ΅³μ΄ μ μμ§μ¬λ λλ€ ! μ΄ μ‘°κ±΄ λν κ³ λ €νμ§ λͺ»ν΄μ νλ Έμλ€..
- μ°λ£κ° μ΄λ λμ€μ λ¨μ΄μ§λ©΄ -1μ μΆλ ₯νκ³ νλ‘κ·Έλ¨μ μ’ λ£νλ€.
- μ΄λνμ§ λͺ»νλ μν©μ flagμ ν΅ν΄ μ€κ°μ€κ° νμΈν΄μΌνλ€.
πΉοΈ νμ΄λ°©λ²
μ²μ ν λ 무λνλ€ μΆμλλ° μκ°μ΄κ³Όκ° λ°μνλ€. .
κ°κ°μ μΉκ°μ 거리λ₯Ό λͺ¨λ ꡬν ν PriorityQueueλ₯Ό μ¬μ©ν΄ 쑰건μ ν΄λΉνλ μ΄μ μλ₯Ό μ ννλ λ°©μμΌλ‘ μμ±νλλ° μ΄ νμ΄λ²μ BFSλ₯Ό μΉκ°μ μλ§νΌ κ³μ λκΈ° λλ¬Έμ μκ°μ΄κ³Όκ° λ°μν κ²μ΄λ€.
λ¨μνκ² μκ°νλ©΄ λλ€.
- boardμ μΉκ°μ λ²νΈ(1~M)μ κΈ°μ νκ³ , λ²½μ -1λ‘ λ체νλ€.
- νμ¬ νμμ μμΉλ‘λΆν° BFSλ₯Ό νλ©° μ΄λν μμΉκ° 0λ³΄λ€ ν¬λ€λ©΄ μ¦, μΉκ°μ΄ μμΉν΄μλ€λ©΄ PriorityQueueμ ν΄λΉνλ μμΉμ 거리λ₯Ό λ£λλ€.
- νμμ λ§μΉκ³ PriorityQueueλ₯Ό pollνλ©΄ 쑰건μ μ ν©ν μΉκ°μ΄ λμ¨λ€.
- κ·Έ μΉκ°μκ² μ΄λνκ³ , μΉκ°μ λͺ©μ μ§λ‘ μ΄λνλ€.
πΉ findPassenger() : μ΅λ¨κ±°λ¦¬μ μλ μΉκ° μ°ΎκΈ°
// #1. μ΅λ¨κ±°λ¦¬μ μλ μΉκ° μ°ΎκΈ° --> μΉκ°μ λ²νΈ λ°ν
static int findPassenger() {
// λ§μ½ νμ¬ νμ μμΉκ° μΉκ°μ μμμ μ΄λΌλ©΄ κ·Έ μΉκ°μ λ²νΈλ₯Ό λ°ννλ€. (μ΄λ X)
if(board[tx][ty]>0) return board[tx][ty];
PriorityQueue<Info> pq = new PriorityQueue<>();
Queue<Info> q = new LinkedList<>();
boolean[][] visit = new boolean[N][N];
q.add(new Info(tx, ty, 0));
int dist=0;
while(!q.isEmpty()) {
Info now = q.poll();
for(int i=0; i<4; i++) {
int nx = now.x+dx[i];
int ny = now.y +dy[i];
if(inRange(nx, ny) && !visit[nx][ny] && board[nx][ny]!=-1) {
visit[nx][ny]=true;
dist = now.d+1;
// ν΄λΉνλ μμΉμ μΉκ°μ΄ μλ€λ©΄ μΉκ°μ μ 보λ₯Ό μ°μ μμ νμ λ£λλ€.
if(board[nx][ny]>0) pq.add(new Info(nx, ny, dist));
q.add(new Info(nx, ny, dist));
}
}
}
// μΉκ°μκ² κ° μ μμ κ²½μ° μ°μ μμ νλ λΉμ΄μλ€. β flagλ‘ λνλ΄μ -1μ μΆλ ₯νλλ‘ νλ€.
if(pq.isEmpty()) {
flag= true;
return 0;
}
Info p = pq.poll();
// μΉκ°μκ² μ΄λνλλ° μ°λ£κ° λΆμ‘±νλ©΄ flagμ νμνκ³ -1μ μΆλ ₯νλλ‘ νλ€.
if(F<=p.d) flag=true;
// μΉκ°μκ² μ΄λν μ μλ€λ©΄ μ°λ£λ₯Ό μλͺ¨νλ€.
else F-=p.d;
// μΉκ°μ λ²νΈλ₯Ό λ°ννλ€.
return board[p.x][p.y];
}
πΉ moveToDest(int idx) : μΉκ°μ λͺ©μ μ§λ‘ μ΄λ
// #2. μΉκ°μ λͺ©μ μ§λ‘ μ΄λ
static int moveToDest(int idx) {
// idx = μΉκ°μ λ²νΈ
Info dest = destination[idx];
Queue<Info> q = new LinkedList<>();
boolean[][] visit = new boolean[N][N];
visit[tx][ty]= true;
int dist=0;
q.add(new Info(tx, ty, 0));
while(!q.isEmpty()) {
Info now = q.poll();
// μΉκ°μ λͺ©μ μ§μ λμ°©νλ€λ©΄ return
if(now.x==dest.x && now.y==dest.y) {
// λ§μ½ μ°λ£κ° λΆμ‘±νλ€λ©΄ -1μ λ°ν
if(F<now.d) return -1;
// μ΄λν μ μλ€λ©΄ μ°λ£λ₯Ό μλͺ¨νκ³ μΆ©μ νλ€.
else {
F-=now.d;
F+=(now.d*2);
return now.d;
}
}
for(int i=0; i<4; i++) {
int nx = now.x+dx[i];
int ny = now.y+dy[i];
if(inRange(nx, ny)&&!visit[nx][ny] && board[nx][ny]!=-1) {
visit[nx][ny] = true;
dist = now.d+1;
q.add(new Info(nx, ny, dist));
}
}
}
return -1;
}
πΉ simulate() : λͺ¨λ ν¨μ ν΅ν©
static void simulate() {
// μ΅λ¨κ±°λ¦¬μ μλ μΉκ°μ μ°Ύκ³ κ·Έ μΉκ°μ λ²νΈλ₯Ό λ°ννλ€.
int idx = findPassenger();
// νμ ~ μΉκ° κ±°λ¦¬κ° μ°λ£λ³΄λ€ ν¬κ±°λ κ°λ€λ©΄ μ΄λ X or λ²½μΌλ‘ μΈν΄ μΉκ°μκ² κ° μ μλ€λ©΄ λ
if(flag) {
resultFlag=true;
return;
}
// νμλ₯Ό μ΄μ μμκ² μ΄λμν€κΈ°
Info p = start[idx];
moveTaxi(p.x, p.y);
// μΉκ°μ λͺ©μ μ§λ‘ μ΄λνμ λμ 거리λ₯Ό λ°ννλ€.
int dist = moveToDest(idx);
// νμ ~ λͺ©μ μ§ κ±°λ¦¬κ° μ°λ£λ³΄λ€ ν¬λ©΄(= moveToDest()μ λ°νκ°μ΄ -1μ΄λΌλ©΄) μ΄λ λΆκ°
if(dist==-1) {
resultFlag=true;
return;
}
// νμλ₯Ό λͺ©μ μ§λ‘ μ΄λμν€κΈ°
Info pe = destination[idx];
moveTaxi(pe.x, pe.y);
// μΉκ°μ μ΄λμμΌ°μΌλ―λ‘ μΉκ° μ κ±°
board[p.x][p.y] = 0;
}
π©π» μ 체 μ½λ
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ_19238_μ€ννΈνμ {
static int N, M, F;
static int[][] board;
static int tx, ty; // νμμ μμΉ
static Info[] start;
static Info[] destination;
static boolean flag;
static boolean resultFlag;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {1, -1, 0, 0};
static class Info implements Comparable<Info>{
int x, y, d;
public Info(int x, int y, int d) {
this.x=x;
this.y=y;
this.d=d;
}
@Override
public int compareTo(Info o) {
if(d==o.d) {
if(x==o.x) return y-o.y;
else return x-o.x;
}else return d-o.d;
}
}
static void simulate() {
int idx = findPassenger();
// νμ ~ μΉκ° κ±°λ¦¬κ° μ°λ£λ³΄λ€ ν¬κ±°λ κ°λ€λ©΄ μ΄λ X or λ²½μΌλ‘ μΈν΄ μΉκ°μκ² κ° μ μλ€λ©΄ λ
if(flag) {
resultFlag=true;
return;
}
Info p = start[idx];
moveTaxi(p.x, p.y);
int dist = moveToDest(idx);
// νμ ~ λͺ©μ μ§ κ±°λ¦¬κ° μ°λ£λ³΄λ€ ν¬λ©΄ μ΄λ λΆκ°
if(dist==-1) {
resultFlag=true;
return;
}
Info pe = destination[idx];
moveTaxi(pe.x, pe.y);
board[p.x][p.y] = 0;
}
// #1. μ΅λ¨κ±°λ¦¬μ μλ μΉκ° μ°ΎκΈ° --> μΉκ°μ λ²νΈ λ°ν
static int findPassenger() {
if(board[tx][ty]>0) return board[tx][ty];
PriorityQueue<Info> pq = new PriorityQueue<>();
Queue<Info> q = new LinkedList<>();
boolean[][] visit = new boolean[N][N];
q.add(new Info(tx, ty, 0));
int dist=0;
while(!q.isEmpty()) {
Info now = q.poll();
for(int i=0; i<4; i++) {
int nx = now.x+dx[i];
int ny = now.y +dy[i];
if(inRange(nx, ny) && !visit[nx][ny] && board[nx][ny]!=-1) {
visit[nx][ny]=true;
dist = now.d+1;
if(board[nx][ny]>0) pq.add(new Info(nx, ny, dist));
q.add(new Info(nx, ny, dist));
}
}
}
if(pq.isEmpty()) {
flag= true;
return 0;
}
Info p = pq.poll();
if(F<=p.d) flag=true;
else F-=p.d;
return board[p.x][p.y];
}
// #2. νμκ° νΉμ μμΉ (μΉκ° μμΉ λλ λͺ©μ μ§)λ‘ μ΄λ
static void moveTaxi(int x, int y) {
tx = x;
ty = y;
}
// #3. μΉκ°μ λͺ©μ μ§λ‘ μ΄λ
static int moveToDest(int idx) {
Info dest = destination[idx];
Queue<Info> q = new LinkedList<>();
boolean[][] visit = new boolean[N][N];
visit[tx][ty]= true;
int dist=0;
q.add(new Info(tx, ty, 0));
while(!q.isEmpty()) {
Info now = q.poll();
if(now.x==dest.x && now.y==dest.y) {
if(F<now.d) return -1;
else {
F-=now.d;
F+=(now.d*2);
return now.d;
}
}
for(int i=0; i<4; i++) {
int nx = now.x+dx[i];
int ny = now.y+dy[i];
if(inRange(nx, ny)&&!visit[nx][ny] && board[nx][ny]!=-1) {
visit[nx][ny] = true;
dist = now.d+1;
q.add(new Info(nx, ny, dist));
}
}
}
return -1;
}
static boolean inRange(int x, int y) {
if(x>=0 && x<N && y>=0 && y<N) return true;
return false;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
F = Integer.parseInt(st.nextToken());
board = new int[N][N];
start = new Info[M+1];
destination = new Info[M+1];
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());
if(board[i][j]==1) board[i][j]=-1;
}
}
st = new StringTokenizer(br.readLine());
tx = Integer.parseInt(st.nextToken())-1;
ty = Integer.parseInt(st.nextToken())-1;
for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int sx = Integer.parseInt(st.nextToken())-1;
int sy = Integer.parseInt(st.nextToken())-1;
int ex = Integer.parseInt(st.nextToken())-1;
int ey = Integer.parseInt(st.nextToken())-1;
board[sx][sy] = i+1;
start[i+1] = new Info(sx, sy, 0);
destination[i+1]=new Info(ex, ey, 0);
}
for(int i=0; i<M; i++) {
simulate();
if(resultFlag) break;
}
if(resultFlag) bw.write(-1 + " ");
else bw.write(F + " ");
bw.flush();
bw.close();
}
}
'μκ³ λ¦¬μ¦ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 2169 λ‘λ΄ μ‘°μ’ νκΈ° - Java (1) | 2024.04.16 |
---|---|
[BOJ] 14503 λ‘λ΄μ²μκΈ° - Java (0) | 2024.04.11 |
[BOJ] 17837 μλ‘μ΄ κ²μ2 - Java (0) | 2024.03.19 |
[BOJ] 17485 μ§μ°μ λ¬ μ¬ν (Large) - Java (0) | 2024.03.12 |
[BOJ] 20058 λ§λ²μ¬ μμ΄μ νμ΄μ΄μ€ν° - Java (0) | 2024.03.01 |