Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์๋ฃ๊ตฌ์กฐ
- ํฌํฌ์ธํฐ
- ๋ฐฑํธ๋ํน
- BFS
- ์ฐ์ ์์ํ
- ์์ด
- ํ์๊ฐ์
- Django
- ํธ๋ผ์ด
- LowerBound
- ๊ตฌํ
- dp
- ๋ถ๋ถ์งํฉ
- dfs
- ์กฐํฉ
- ํ๋ก์ด๋์์
- PriorityQueue
- ์๋ฎฌ๋ ์ด์
- ์ด์งํธ๋ฆฌ
- ์ฌ๊ท
- ์ธ๊ทธ๋จผํธํธ๋ฆฌ
- ๋นํธ๋ง์คํน
- Dijkstra
- Union Find
- upperbound
- ๊ทธ๋ฆฌ๋
- ์ด๋ถํ์
- Floyd
- ๋ค์ต์คํธ๋ผ
- ์นด์นด์ค
Archives
- Today
- Total
J
[JAVA] Programmers ๋ฏธ๋ก ํ์ถ ๋ช ๋ น์ด ๋ณธ๋ฌธ
๐ Problem Solving/๐ซ Programmers
[JAVA] Programmers ๋ฏธ๋ก ํ์ถ ๋ช ๋ น์ด
snowball๐ฐ 2023. 5. 8. 13:57ํ์ด ๋ฐฉ๋ฒ
- PriorityQueue๋ฅผ ์ฌ์ฉํ์ฌ String์ด ์ฌ์ ์์ผ๋ก ๊ณ์ ์ ๋ ฌ๋๊ฒ ํ๋ค.
- ๊ฐ์ง์น๊ธฐ๋ฅผ ํ๋ฉด์ poll ๊ฐ์ ์กฐ์ฌํ ์ง ๋ง์ง ๊ฒฐ์ ํ๋ค.
- ๋์ฐฉ ์์น์ ํ์ฌ ์์น ๊ฑฐ๋ฆฌ๊ฐ ๋จ์ ๊ฑฐ๋ฆฌ๋ณด๋ค ํฌ๋ฉด ์ ๋ ๋์ฐฉํ ์ ์๋ค.
- (ํ์ฌ ์์น์ ๋์ฐฉ ์์น ์ฌ์ด ๊ฑฐ๋ฆฌ)์ ๋จ์ ๊ฑฐ๋ฆฌ์ ํ์ง์ ๊ฐ์์ผ ํ๋ค.
import java.io.*;
import java.util.*;
class Solution {
static class load{
int x;
int y;
int time;
String route;
load(int x, int y, int time, String route){
this.x = x; this.y = y; this.time = time; this.route = route;
}
public String toString(){
return route;
}
}
static int[] dx = {1, 0, 0, -1}, dy = {0, -1, 1, 0};
public String solution(int n, int m, int x, int y, int r, int c, int k) {
PriorityQueue<load> q = new PriorityQueue<>((o1, o2) -> o1.route.compareTo(o2.route));
q.offer(new load(x-1, y-1, 0, ""));
boolean[][] v = new boolean[n][m];
v[x-1][y-1] = true;
while(!q.isEmpty()){
load now = q.poll();
if(now.x == r-1 && now.y == c-1 && now.time == k) return now.route;
if(now.time > k) continue;
if(!ispossible(now.x, now.y, r-1, c-1, k-now.time)) continue;
for(int d=0; d<4; d++){
int nx = now.x+dx[d];
int ny = now.y+dy[d];
if(nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
String Nroute = "";
if(d == 0) Nroute = now.route+"d";
else if(d == 1) Nroute = now.route+"l";
else if(d == 2) Nroute = now.route+"r";
else Nroute = now.route+"u";
q.offer(new load(nx, ny, now.time+1, Nroute));
}
}
return "impossible";
}
boolean ispossible(int x, int y, int r, int c, int time){
int rest = Math.abs(r-x)+Math.abs(c-y);
if(rest > time) return false;
if((rest % 2) - (time % 2) == 0) return true;
return false;
}
}
'๐ Problem Solving > ๐ซ Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] Programmers ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ (0) | 2023.05.09 |
---|---|
[JAVA] Programmers ํ ๋ณํฉ (0) | 2023.05.08 |
Comments