๋ฐ์ํ
https://www.acmicpc.net/problem/19238
๋ฌธ์ ์ค๋ช
์คํํธ ํ์
- ์๋์ ๋ชฉ์ ์ง์ ๋ฐ๋ ค๋ค ์ฃผ๋ฉด ์ฐ๋ฃ += ์๋ชจ ์ฐ๋ฃ * 2
- ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ฅ ๊ฐ๊น์ด ์๋ ํฝ์
- ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ผ๋ฉด ๊ฐ์ฅ ์์ชฝ
- ๊ทธ๊ฒ๋ ๊ฐ์ผ๋ฉด ๊ฐ์ฅ ์ผ์ชฝ
- M๋ช ์ ์น๊ฐ์ ํ์ฐ๋ ๊ฒ์ด ๋ชฉํ
- ๋ฒฝ์ด ์๋ ์ํ์ข์ฐ๋ก ์ด๋ ๊ฐ๋ฅ (ํ ์นธ : ์ฐ๋ฃ -= 1)
- ์ด๋ ์ค ์ฐ๋ฃ ๋ฐ๋ฅ ๐ ์คํจ (-1)
- ๋ชจ๋ ์น๊ฐ์ ์ด๋ ๋ชป์ํค๋ ๊ฒฝ์ฐ ๐ ์คํจ (-1)
- ๋จ์ ์ฐ๋ฃ์ ์ ์ถ๋ ฅ
๋ฌธ์ ํ์ด
์์ธ ์ฒ๋ฆฌ
- ํ์ ๊ธฐ์ฌ๊ฐ ์น๊ฐ์ ์์น๊น์ง ๋๋ฌํ ์ ์๋ ๊ฒฝ์ฐ (๊ฑฐ๋ฆฌ = INF)
- ์น๊ฐ์ ๋์ฐฉ์ง๊น์ง ํ์ธ ์ ์๋ ๊ฒฝ์ฐ (๊ฑฐ๋ฆฌ = INF)
class Passenger(startX, startY, dist, toX, toY)
- list์์ ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก ์ญ์ ํด์ผํด์
equals
,hashCode
method๋ฅผ overriding - ์น๊ฐ ์ ๋ณด๋ค์ ๊ฐ์ง List ๐
passengerList
- ํ์๊ธฐ์ฌ ์ฃผ๋ณ ์น๊ฐ ์ ๋ณด ์
๋ฐ์ดํธ (
updatePassengerList()
)
passengerList
๋ฅผ ๋๋ฉด์ ๊ฐ ์น๊ฐ๋ณ ํ์๊ธฐ์ฌ-์น๊ฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐ์ ์์ํ์ ์ถ๊ฐgetDist(์ถ๋ฐ ์ขํ, ๋์ฐฉ ์ขํ)
๋ก ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ค
- ์ฐ์ ์์ํ์์ ๊ฐ์ฅ ์์ ์๋ ์น๊ฐ์ ๋ฝ๊ณ (dist๊ฐ ๊ฐ์ฅ ์์),
passengerList
์์ ํด๋น ์น๊ฐ์ ์ ๊ฑฐ - 1)
ํ์๊ธฐ์ฌ โก๏ธ ์น๊ฐ์์น ๊ฑฐ๋ฆฌ < ํ์ฌ ์ฐ๋ฃ๋
์ด๊ฑฐ๋๊ฑฐ๋ฆฌ == INF
์ด๋ฉด -1 ์ถ๋ ฅ ํ ์ข ๋ฃ
2)ํ์๊ธฐ์ฌ โก๏ธ ์น๊ฐ์์น ๊ฑฐ๋ฆฌ >= ํ์ฌ ์ฐ๋ฃ๋
์ด๋ผ๋ฉด,ํ์๊ธฐ์ฌ โก๏ธ ์น๊ฐ์์น ๊ฑฐ๋ฆฌ >= ํ์ฌ ์ฐ๋ฃ๋ - ํ์ฐ๋ ๊ฑฐ๋ฆฌ
์ด๋ฉด, ํ์ฌ ์ฐ๋ฃ += ํ์ด ๊ฑฐ๋ฆฌ- ํ์๊ธฐ์ฌ ์์น = ์น๊ฐ์ ๋์ฐฉ์ง
- ๊ทธ๋ ์ง ์๋ค๋ฉด, -1 ์ถ๋ ฅ ํ ์ข ๋ฃ
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_BOJ_19238_์คํํธ_ํ์ {
static class Passenger {
int fromX;
int fromY;
int dist;
int toX;
int toY;
public Passenger(int fromX, int fromY) {
this.fromX = fromX;
this.fromY = fromY;
}
public Passenger(int fromX, int fromY, int dist) {
this(fromX, fromY);
this.dist = dist;
}
public Passenger(int fromX, int fromY, int dist, int toX, int toY) {
this(fromX, fromY, dist);
this.toX = toX;
this.toY = toY;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Passenger passenger = (Passenger) o;
return fromX == passenger.fromX && fromY == passenger.fromY && toX == passenger.toX && toY == passenger.toY && dist == passenger.dist;
}
@Override
public int hashCode() {
return Objects.hash(fromX, fromY, toX, toY, dist);
}
@Override
public String toString() {
return "Passenger{" +
"fromX=" + fromX +
", fromY=" + fromY +
", dist=" + dist +
", toX=" + toX +
", toY=" + toY +
'}';
}
}
static int N, M, minDist;
static long fuel;
static boolean[][] map;
static final int INF = 1000;
static int[] dx = {-1, 0, 0, 1};
static int[] dy = {0, -1, 1, 0};
static Passenger driver = new Passenger(0, 0);
// static HashMap<Passenger, Boolean> passengerList = new HashMap<>();
static List<Passenger> passengerList = new ArrayList<>();
static PriorityQueue<Passenger> passengersNearBy = new PriorityQueue<>((o1, o2) -> {
if (o1.dist == o2.dist) {
if (o1.fromX == o2.fromX) {
return Integer.compare(o1.fromY, o2.fromY);
}
return Integer.compare(o1.fromX, o2.fromX);
}
return Integer.compare(o1.dist, o2.dist);
});
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());
fuel = Integer.parseInt(stringTokenizer.nextToken());
map = new boolean[N][N];
for (int i = 0; i < N; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(stringTokenizer.nextToken()) == 1 ? true : false;
}
}
stringTokenizer = new StringTokenizer(br.readLine());
driver.fromX = Integer.parseInt(stringTokenizer.nextToken()) - 1;
driver.fromY = Integer.parseInt(stringTokenizer.nextToken()) - 1;
for (int i = 0; i < M; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
int fromX = Integer.parseInt(stringTokenizer.nextToken()) - 1;
int fromY = Integer.parseInt(stringTokenizer.nextToken()) - 1;
int toX = Integer.parseInt(stringTokenizer.nextToken()) - 1;
int toY = Integer.parseInt(stringTokenizer.nextToken()) - 1;
// passengerList.put(new Passenger(fromX, fromY, 0, toX, toY), true);
passengerList.add(new Passenger(fromX, fromY, 0, toX, toY));
}
System.out.println(passengerList.size());
for (Passenger p : passengerList) {
System.out.println(p.toString());
}
while (M-- > 0) {
updatePassengerList();
for (Passenger p : passengersNearBy) {
System.out.println("u : " + p.toString());
}
int dist = passengersNearBy.peek().dist;
if (dist > fuel || dist == INF) {
fuel = -1;
break;
}
System.out.println("PickUp - dist : " + dist + " fuel : " + fuel);
fuel -= dist;
int fromX = passengersNearBy.peek().fromX;
int fromY = passengersNearBy.peek().fromY;
int toX = passengersNearBy.peek().toX;
int toY = passengersNearBy.peek().toY;
Passenger p = new Passenger(fromX, fromY, 0, toX, toY);
System.out.println("remove : " + p.toString());
passengerList.remove(p);
for (Passenger pa : passengerList) {
System.out.println("list : " + pa.toString());
}
minDist = INF;
dist = getDist(fromX, fromY, toX, toY);
System.out.println("Drop - dist : " + dist + " fuel : " + fuel);
if (dist > fuel || dist == INF) {
fuel = -1;
break;
}
fuel += dist;
driver.fromX = toX;
driver.fromY = toY;
}
System.out.println(String.valueOf(fuel));
}
private static void updatePassengerList() {
passengersNearBy.clear();
minDist = INF;
for (Passenger p : passengerList) {
int dist = getDist(driver.fromX, driver.fromY, p.fromX, p.fromY);
minDist = Math.min(minDist, dist);
passengersNearBy.offer(new Passenger(p.fromX, p.fromY, dist, p.toX, p.toY));
}
}
private static int getDist(int fromX, int fromY, int toX, int toY) {
boolean[][] isVisited = new boolean[N][N];
isVisited[fromX][fromY] = true;
Queue<Passenger> q = new LinkedList<>();
q.offer(new Passenger(fromX, fromY, 0));
while (!q.isEmpty()) {
Passenger now = q.poll();
int x = now.fromX;
int y = now.fromY;
int dist = now.dist;
if(minDist < dist) break;
if(x == toX && y == toY) return dist;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if(nx < 0 || ny < 0 || N <= nx || N <= ny || isVisited[nx][ny] || map[nx][ny]) continue;
isVisited[nx][ny] = true;
q.offer(new Passenger(nx, ny, dist + 1));
}
}
return INF;
}
}
728x90
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ๋ฐฑ์ค / BOJ 17822 ] ์ํ ๋๋ฆฌ๊ธฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.07 |
---|---|
[ BOJ / ๋ฐฑ์ค 20058 ] ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด์คํฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.05 |
[ ๋ฐฑ์ค / BOJ 17779 ] ๊ฒ๋ฆฌ๋ฉ๋๋ง_2 ( ์๋ฐ / JAVA ) (0) | 2022.02.02 |
[ ๋ฐฑ์ค / BOJ 17140 ] ์ด์ฐจ์ ๋ฐฐ์ด๊ณผ ์ฐ์ฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.02 |
[ ๋ฐฑ์ค / BOJ 17135 ] ์บ์ฌ ๋ํ์ค ( ์๋ฐ / JAVA ) (0) | 2022.01.31 |