[Programmers] μ§ν μ΄λ JAVA
λ¬Έμ : μ§ν μ΄λ
λ¬Έμ μ€λͺ
N * N
ν¬κΈ°μ μ§νμ΄ μκ³ μ§νλ§λ€ κ°κ°μ λμ΄κ° μμ΅λλ€.
μ§νμ λμ΄μ°¨κ° μ ν΄μ§ κ° μ΄νμ΄λ©΄ μμ λ‘κ² μ΄λν μ μμ§λ§, μ ν΄μ§ κ° λ³΄λ€ μ°¨μ΄κ° λμΌλ©΄ μ¬λ€λ¦¬λ₯Ό μ€μΉν΄μΌ μ΄λν μ μμ΅λλ€.
μ΄λ μ¬λ€λ¦¬μ λμ΄λ μΈμ ν μ§νμ λμ΄ μ°¨μ΄κ³ , μ¬λ€λ¦¬μ μλ μ νμ΄ μμ§λ§ μ¬λ€λ¦¬λ€μ λμ΄ν©μ΄ μ΅μνμ΄ λλλ‘ μ€μΉνμ¬ λͺ¨λ μ§νλΌλ¦¬ μ°κ²°λκ² ν΄μΌ νλ λ¬Έμ μ
λλ€.
Solution
bfs, ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦
μ°μ bfsλ‘ μλ‘ μ¬λ€λ¦¬ μμ΄ μ΄λν μ μλ μ§νλ€μ ꡬλ³ν΄ μ£Όκ³ κ°κ° λ²νΈ(μ¬λ€λ¦¬ μμ΄ μ΄λν μ μλ μ§μλ³ λ²νΈ)λ₯Ό ν λΉν΄ μ€λλ€.
μ΄λ μΈμ ν μ§νμ λμ΄μ°¨κ° μ£Όμ΄μ§ κ° λ³΄λ€ μ»€μ μ΄λμ λͺ»νλ μ§νμμλ ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦μ μννκΈ° μν μ½λλ₯Ό μμ±ν΄ μ£Όμ΄μΌ ν©λλ€.
λ§μ½ λμ΄μ°¨κ° ν¬κ² μ°¨μ΄ λλ μ§νμ΄ λ°©λ¬Έν μ μλ(μ§μλ³ λ²νΈκ° μλ κ³³)μ΄λΌλ©΄ 무μνκ³ continue ν΄μ€λλ€.
λμ΄μ°¨κ° ν¬κ² λλ μ§νμ΄ μ§μλ³ λ²νΈκ° ν λΉλΌμλ μ§νμ΄λΌλ©΄ νμ¬ μ§νμ μ§μ λ²νΈ, μ¬λ€λ¦¬κ° μμ΄μΌ κ° μ μλ μ§νμ μ§μ λ²νΈ, λμ΄μ°¨
μΈ κ°μ§ κ°μ λμ΄μ°¨λ₯Ό κΈ°μ€μΌλ‘ μ°μ μμ νμ λ΄μμ£Όμμ΅λλ€.
λͺ¨λ μ§νμ μννλ©΄μ μ¬λ€λ¦¬κ° νμν κ³³μ μ°μ μμ νμ λͺ¨λ λ΄μ쀬λ€λ©΄, λμ΄μ°¨κ° κ°μ₯ μ μ μ§μμ ν©μ³μ£Όλ©΄μ μ λμ¨ νμΈλλ₯Ό μ΄μ©ν΄ ν¬λ£¨μ€μΉΌ μκ³ λ¦¬μ¦μ μ€νν©λλ€.
μ΄λ κ² μ΄μ΄μ§ μ΅μ μ μ₯ νΈλ¦¬λ₯Ό λ§λ€λ©΄μ μ¬μ©ν μ¬λ€λ¦¬μ μ΄ν©μ return ν΄μ€λλ€.
Description
- μ°μ μμ νμ λ΄μμΌ νλ
νμ¬ μ§νμ μ§μ λ²νΈ, μ¬λ€λ¦¬κ° μμ΄μΌ κ° μ μλ μ§νμ μ§μ λ²νΈ, λμ΄ μ°¨
λ₯ΌGuard
λΌλ ν΄λμ€λ‘ λ΄μμ΅λλ€. vis[][]
λ°°μ΄μ μ¬λ€λ¦¬ μμ΄ μ΄λν μ μλ μ§μλ³ λ²νΈλ₯Ό 1μ© μ¦κ°μν€λ©΄μ λ΄μμ΅λλ€.
μμ€μ½λ
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
class Guard implements Comparable<Guard>{
private int st;
private int ed;
private int value;
public int getValue() {
return value;
}
public int getEd() {
return ed;
}
public int getSt() {
return st;
}
public Guard(int st, int ed, int value){
this.st = st;
this.ed = ed;
this.value = value;
}
@Override
public int compareTo(Guard o) {
return Integer.compare(this.value, o.value);
}
}
class Node{
private int x;
private int y;
public int getY() {
return y;
}
public int getX() {
return x;
}
public Node(int x, int y){
this.x = x;
this.y = y;
}
}
class Solution {
public static int[] parent;
public static int find(int idx){
if(idx==parent[idx]) return idx;
else return parent[idx] = find(parent[idx]);
}
public static void merge(int a, int b){
a = find(a);
b = find(b);
if(a==b) return;
if(a>b) parent[a] = b;
else parent[b] = a;
}
public static PriorityQueue<Guard> pq = new PriorityQueue<>();
public static int[][] vis;
public static int[][] map;
public static int n;
public static int len;
public static int[] dx = {0, 0, -1, 1};
public static int[] dy = {1, -1, 0, 0};
public static int cnt;
public void bfs(int nowX, int nowY){
vis[nowX][nowY] = cnt;
Queue<Node> q = new LinkedList<>();
q.offer(new Node(nowX,nowY));
while(!q.isEmpty()){
Node node = q.poll();
int x = node.getX();
int y = node.getY();
for(int i=0; i<4; i++){
int nx = x+dx[i];
int ny = y +dy[i];
if(nx<0||ny<0||nx>=n||ny>=n) continue;
if(Math.abs(map[x][y]-map[nx][ny])> len) {
if(vis[nx][ny] != 0){
pq.offer(new Guard(vis[x][y],vis[nx][ny],Math.abs(map[x][y]-map[nx][ny])));
}
continue;
}
if(vis[nx][ny]!=0) continue;
else{
vis[nx][ny] = cnt;
q.offer(new Node(nx,ny));
}
}
}
}
public int solution(int[][] land, int height) {
n = land.length;
map = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
map[i][j] = land[i][j];
}
}
len = height;
vis = new int[n][n];
for(int i =0; i<n; i++){
for(int j=0; j<n; j++){
if(vis[i][j]==0){
cnt++;
bfs(i,j);
}
}
}
parent = new int[cnt+1];
int res = 0;
for(int i=0; i<=cnt; i++) parent[i] = i;
while (!pq.isEmpty()){
Guard guard = pq.poll();
int st = guard.getSt();
int ed =guard.getEd();
int value =guard.getValue();
if(find(st)==find(ed)) continue;
else if(st==0||ed==0) continue;
else {
merge(st,ed);
res += value;
}
}
return res;
}
}
'Algorithm > Programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers] λΈλ‘ μ΄λνκΈ°(2020 KAKAO BLIND RECRUITMENT) (0) | 2022.03.16 |
---|---|
[Programmers] μλ¬Όμ μ μ΄μ (2020 KAKAO BLIND RECRUITMENT) (0) | 2022.03.15 |