-
Notifications
You must be signed in to change notification settings - Fork 0
/
스타트택시.java
165 lines (146 loc) · 3.84 KB
/
스타트택시.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// BOJ - 스타트택스 (19238번)
// BFS
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static int n, m, oil, people;
public static int[][] map;
public static int[][] cus_map;
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
public static boolean[] check;
public static int t_x, t_y;
public static class Node implements Comparable<Node>{
int x;
int y;
int dis;
public Node(int x, int y, int dis) {
this.x = x;
this.y = y;
this.dis = dis;
}
@Override
public int compareTo(Node o) {
if(this.dis == o.dis) {
if(this.x == o.x) {
return this.y - o.y;
}
return this.x - o.x;
}
return this.dis - o.dis;
}
}
public static int[][] cus;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
oil = Integer.parseInt(st.nextToken());
map = new int[n][n];
cus = new int[m+1][4];
cus_map = new int[n][n];
check = new boolean[m+1];
for(int i=0;i<n;i++) {
st = new StringTokenizer(br.readLine(), " ");
for(int j=0;j<n;j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine(), " ");
t_x = Integer.parseInt(st.nextToken())-1;
t_y = Integer.parseInt(st.nextToken())-1;
for(int i=1;i<=m;i++) {
st = new StringTokenizer(br.readLine(), " ");
for(int j=0;j<4;j++) {
int num = Integer.parseInt(st.nextToken())-1;
cus[i][j] = num;
}
cus_map[cus[i][0]][cus[i][1]] = i;
}
people = m;
boolean end = false;
while (true) {
if(people == 0) {
break;
}
// 손님 고르기
Node node = bfs1();
if(node == null) {
end = true;
break;
}
int cus_num = cus_map[node.x][node.y];
// 손님 데려다 주기
int cal1 = node.dis;
int cal2 = bfs2(node.x, node.y, cus[cus_num][2], cus[cus_num][3]);
if(oil < cal1+cal2 || cal2 == -1) {
end = true;
break;
}
oil -= (cal1+cal2);
oil += (cal2 * 2);
check[cus_num] = true;
cus_map[node.x][node.y] = 0;
t_x = cus[cus_num][2];
t_y = cus[cus_num][3];
people--;
}
if(end) System.out.println(-1);
else System.out.println(oil);
}
public static Node bfs1() {
boolean[][] visited = new boolean[n][n];
Queue<Node> q = new LinkedList<>();
PriorityQueue<Node> pq = new PriorityQueue<>();
visited[t_x][t_y] = true;
q.offer(new Node(t_x, t_y, 0));
while(!q.isEmpty()) {
Node node = q.poll();
if(cus_map[node.x][node.y] >= 1 && !check[cus_map[node.x][node.y]]) {
pq.add(new Node(node.x, node.y, node.dis));
}
for(int d=0;d<4;d++) {
int nx = node.x + dx[d];
int ny = node.y + dy[d];
if(0 <= nx && nx < n && 0 <= ny && ny < n) {
if(!visited[nx][ny] && map[nx][ny] != 1) {
visited[nx][ny] = true;
q.add(new Node(nx, ny, node.dis+1));
}
}
}
}
if(pq.isEmpty()) return null;
return pq.poll();
}
public static int bfs2(int s_x, int s_y, int e_x, int e_y) {
boolean[][] visited = new boolean[n][n];
Queue<Node> q = new LinkedList<>();
visited[s_x][s_y] = true;
q.offer(new Node(s_x, s_y, 0));
int dis = -1;
while(!q.isEmpty()) {
Node node = q.poll();
if(node.x == e_x && node.y == e_y) {
return node.dis;
}
for(int d=0;d<4;d++) {
int nx = node.x + dx[d];
int ny = node.y + dy[d];
if(0 <= nx && nx < n && 0 <= ny && ny < n) {
if(!visited[nx][ny] && map[nx][ny] != 1) {
visited[nx][ny] = true;
q.add(new Node(nx, ny, node.dis+1));
}
}
}
}
return dis;
}
}