[Programmers] ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น(2020 KAKAO BLIND RECRUITMENT) JAVA
๋ฌธ์ : ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น(2020 KAKAO BLIND RECRUITMENT)
๋ฌธ์ ์ค๋ช
N*N
์ ๋ฒฝ๋ฉด์ด ์ฃผ์ด์ง๊ณ ์ด ๋ฒฝ๋ฉด์ ๊ธฐ๋ฅ๊ณผ ๋ณด๋ฅผ ์ค์น, ์ญ์ ํ๋ ๋ฌธ์ ์
๋๋ค.
๊ธฐ๋ฅ์ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์, ํน์ ๋ณด์ ์ ๋ ์ชฝ์ ์ค์น๋ ์ ์์ต๋๋ค.
๋ณด๋ ์ ๋ ์ ์ค ํ์ ์ ๊ธฐ๋ฅ์ด ์๊ฑฐ๋, ์ ๋ ์ ์ ๋ณด๊ฐ ์ค์น๋์ด ์์ ์ ์ค์น๋ ์ ์์ต๋๋ค.
์
๋ ฅ์ผ๋ก ์ํ๋ ๊ธฐ๋ฅ๊ณผ ๋ณด์ ์ค์น, ์ญ์ ์ ๋ฌด์ ์ขํ๊ฐ ์ฃผ์ด์ง๋ฉฐ, ์ถ๋ ฅ์ผ๋ก ์ค์น, ์ญ์ ๊ฐ ๊ฐ๋ฅํ ๋ ์คํํ ๋ค์ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ์ด ๋ฌธ์ ์
๋๋ค.
Solution
๊ตฌํ, ์์ ํ์
์
๋ ฅ๊ฐ์ ๋ฐ์ ๋ค, ์ด ์
๋ ฅ๊ฐ์ ์ต์ข
๋ฐฐ์ด์ ์ฝ์
ํฉ๋๋ค.
๊ทธ ํ ์ฝ์
ํ ๊ฒฐ๊ณผ(์ญ์ ๋ ์ค์น)๊ฐ ๊ธฐ๋ฅ๊ณผ ๋ณด์ ๊ท์น์ ์๋ฐํ์ง ์๋๋ค๋ฉด ์ต์ข
๋ฐฐ์ด์ ๊ทธ๋๋ก ๋๊ณ , ๊ท์น์ ์๋ฐํ๋ค๋ฉด ๋ค์ ์ค์น๋ ์ญ์ ๋ฅผ ํด์ค์ผ ํฉ๋๋ค.
๊ท์น์ ์งํค๋ ๊ฒ์ ๋ํ ํ์ธ์ ์ต์ข
๋ฐฐ์ด์ ์์ ํ์ํ๋ฉด์ ํ์ธํด์ผ ํฉ๋๋ค.
์ต์ข
๋ฐฐ์ด์ ๋จ์ ๊ธฐ๋ฅ๊ณผ ๋ณด๋ฅผ ๋ฌธ์ ์ ๊ท์น๋๋ก ์ ๋ ฌํ ๋ค 2์ฐจ์ ๋ฐฐ์ด์ ์ฐจ๋ก๋๋ก ์ฝ์
ํด์ return ํด์ค๋๋ค.
Description
- ์์ชฝ์ ๋ณด์ ์ค์น ์ ๋ฌด๋ ๋ ๊ฐ์ง boolean ๋ณ์ le,ri๋ก ํ์ ํ์ต๋๋ค.
- ๊ฐ๊ฐ์ ๊ธฐ๋ฅ๊ณผ ๋ณด๋ ArrayList๋ฅผ ๋ง๋ค๊ณ ์ธ ๊ฐ์ง ๋ณ์๋ฅผ ๋ฃ์ด ๋ง๋ค์์ต๋๋ค. (x์ขํ, y์ขํ, ๋ณด์ธ์ง ๊ธฐ๋ฅ์ธ์ง)
์์ค์ฝ๋
import java.util.ArrayList;
import java.util.Collections;
class Node implements Comparable<Node>{
private int x;
private int y;
private int article;
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getArticle() {
return article;
}
public Node(int x, int y, int article){
this.x = x;
this.y = y;
this.article = article;
}
@Override
public int compareTo(Node other) {
if(this.x==other.x && this.y == other.y) return Integer.compare(this.article,other.article);
else if(this.x == other.x) return Integer.compare(this.y, other.y);
else return Integer.compare(this.x, other.x);
}
}
class Solution {
public static boolean chkPossible(){
for(int i=0;i<result.size();i++){
ArrayList<Integer> nowNode = result.get(i);
int x = nowNode.get(0);
int y = nowNode.get(1);
int a = nowNode.get(2);
if(a==0){
boolean chk = false;
if(y==0) chk = true;
for(int j=0; j<result.size();j++){
ArrayList<Integer> otherNode = result.get(j);
int otherX = otherNode.get(0);
int otherY = otherNode.get(1);
int otherA = otherNode.get(2);
if(otherA==0){
if(otherX==x && otherY==y-1) chk = true;
}
else {
if(otherX==x && otherY==y) chk = true;
else if(otherX==x-1 && otherY==y) chk = true;
}
}
if(chk==false) return false;
}
else if(a==1){
boolean chk = false;
boolean le = false;
boolean ri = false;
if(y==0) chk = true;
for(int j=0; j<result.size();j++){
ArrayList<Integer> otherNode = result.get(j);
int otherX = otherNode.get(0);
int otherY = otherNode.get(1);
int otherA = otherNode.get(2);
if(otherA==0){
if(otherX==x && otherY == y-1) chk = true;
if(otherX==x+1 && otherY == y-1) chk = true;
}
else {
if(otherX==x-1 && otherY == y) le = true;
if(otherX==x+1 && otherY == y) ri = true;
}
}
if(le&&ri) chk = true;
if(chk==false) return false;
}
}
return true;
}
public static ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
public int[][] solution(int n, int[][] build_frame) {
for(int i=0;i<build_frame.length;i++){
int x = build_frame[i][0];
int y = build_frame[i][1];
int a = build_frame[i][2];
int b = build_frame[i][3];
if(b==1){
ArrayList<Integer> nowNode = new ArrayList<>();
nowNode.add(x);
nowNode.add(y);
nowNode.add(a);
result.add(nowNode);
if(!chkPossible()) result.remove(result.size()-1);
}
else if(b==0){
int idx = 0;
for(int j=0;j<result.size();j++){
if(x==result.get(j).get(0)&&y==result.get(j).get(1)&&a==result.get(j).get(2)){
idx = j;
break;
}
}
ArrayList<Integer> nowNode = result.get(idx);
result.remove(idx);
if(!chkPossible()) result.add(nowNode);
}
}
ArrayList<Node> list = new ArrayList<>();
for(int i = 0; i<result.size();i++){
list.add(new Node(result.get(i).get(0),result.get(i).get(1),result.get(i).get(2)));
}
Collections.sort(list);
int[][] answer = new int[list.size()][3];
for(int i=0;i<list.size();i++){
answer[i][0] = list.get(i).getX();
answer[i][1] = list.get(i).getY();
answer[i][2] = list.get(i).getArticle();
}
return answer;
}
}
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] ์๊ธฐ ์์ด(16236) (0) | 2022.03.15 |
---|---|
[BOJ] ํ์ฑ ํฐ๋(2887) (0) | 2022.03.15 |
[BOJ] ๋ฑ(3190) (0) | 2022.03.15 |
[BOJ] ์์ ์ด๋ 2์ ๊ฑฐ๋ญ ์ ๊ณฑ์ ์ข์ํด! ์์ ์ด๋ 2์ ๊ฑฐ๋ญ ์ ๊ณฑ์ ์ข์ํด!(20153) (0) | 2022.03.15 |
[BOJ] ์๋ฌธ๋ ์น ๊ณต์ฃผ(1941) (0) | 2022.03.14 |