๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/BOJ

[Programmers] ์ž๋ฌผ์‡ ์™€ ์—ด์‡ (2020 KAKAO BLIND RECRUITMENT)

[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;
    }
}