HS_development_log

프로그래머스 카카오2019 - 길 찾기 게임 본문

Algorithm-프로그래머스

프로그래머스 카카오2019 - 길 찾기 게임

DevHyeonseong 2020. 1. 28. 15:10
반응형

문제

https://programmers.co.kr/learn/courses/30/lessons/42892?language=java

 

코딩테스트 연습 - 길 찾기 게임 | 프로그래머스

[[5,3],[11,5],[13,3],[3,5],[6,1],[1,3],[8,6],[7,2],[2,2]] [[7,4,6,9,1,8,5,2,3],[9,6,5,8,1,4,3,2,7]]

programmers.co.kr

트리 순회 문제.

BST 구현방식과 유사하다. BST와 같은방식으로 구현한뒤 순회

 

 

알고리즘

  1. x축,y축 그리고 노드번호를 갖는 Node 클래스를 만들고 배열을 생성한다

  2. y 좌표를 기준으로 내림차순으로 정렬한다

  3. x 좌표를 기준으로 작으면 왼쪽, 크면 오른쪽으로 재귀적으로 탐색해서 트리 구성

  4. 순회

  5. 결과 리턴

소스코드 및 설명

 

1,2번 알고리즘

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
class Node implements Comparable<Node>{
    int x;
    int y;
    int idx;
    Node(int x, int y, int idx){
        this.x = x;
        this.y = y;
        this.idx = idx;
    }
    @Override
    public int compareTo(Node o) {
        if(o.y==this.y) {
            return o.x-this.x;
        }
        else {
            return o.y-this.y;
        }
    }
}
class Solution{
    public int[][] solution(int[][] nodeinfo) {
        Node node[] = new Node[nodeinfo.length];
        for(int i=0;i<nodeinfo.length;i++) {
            node[i] = new Node(nodeinfo[i][0],nodeinfo[i][1],i+1);
        }
        Arrays.sort(node);
}
cs

 

3번 알고리즘

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
class TreeNode{
    int data;
    int x;
    TreeNode left;
    TreeNode right;
    TreeNode(int data,int x){
        this.data = data;
        this.x = x;
    }
}
class Tree{
    TreeNode root;
    Tree(int data,int x){
        root = new TreeNode(data,x);
    }
    public void add(int data, int x) {
        find(root, data, x);
    }
    public void find(TreeNode node, int data, int x) {
        if(node.x < x) {
            if(node.right == null) {
                node.right = new TreeNode(data,x);
            } else {
                find(node.right,data,x);
            }
        }
        else {
            if(node.left == null) {
                node.left = new TreeNode(data,x);
            } else {
                find(node.left,data,x);
            }
        }
    }
}
cs

 

4번 알고리즘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void preOrder(TreeNode node) {
    if(node==null)
        return;
    list1.add(node.data);
    preOrder(node.left);
    preOrder(node.right);
}    
public void postOrder(TreeNode node) {
    if(node==null)
        return;
    postOrder(node.left);
    postOrder(node.right);
    list2.add(node.data);
}
cs

 

전체 소스코드

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
/*
 * 2019 KAKAO 길 찾기 게임
 * 2020.01.28
 * Hyeonseong
 */
import java.util.*;    
public class KakaoBlind2019_5 {
    public static void main(String[] args) {
        int temp[][] = {{5,3},{11,5},{13,3},{3,5},{6,1},{1,3},{8,6},{7,2},{2,2}};
        Solution s = new Solution();
        s.solution(temp);
    }
}
class Node implements Comparable<Node>{
    int x;
    int y;
    int idx;
    Node(int x, int y, int idx){
        this.x = x;
        this.y = y;
        this.idx = idx;
    }
    @Override
    public int compareTo(Node o) {
        if(o.y==this.y) {
            return o.x-this.x;
        }
        else {
            return o.y-this.y;
        }
    }
}
class TreeNode{
    int data;
    int x;
    TreeNode left;
    TreeNode right;
    TreeNode(int data,int x){
        this.data = data;
        this.x = x;
    }
}
class Tree{
    TreeNode root;
    ArrayList<Integer> list1 = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    Tree(int data,int x){
        root = new TreeNode(data,x);
    }
    public void add(int data, int x) {
        find(root, data, x);
    }
    public void find(TreeNode node, int data, int x) {
        if(node.x < x) {
            if(node.right == null) {
                node.right = new TreeNode(data,x);
            } else {
                find(node.right,data,x);
            }
        }
        else {
            if(node.left == null) {
                node.left = new TreeNode(data,x);
            } else {
                find(node.left,data,x);
            }
        }
    }
    public void preOrder(TreeNode node) {
        if(node==null)
            return;
        list1.add(node.data);
        preOrder(node.left);
        preOrder(node.right);
    }
    public void postOrder(TreeNode node) {
        if(node==null)
            return;
        postOrder(node.left);
        postOrder(node.right);
        list2.add(node.data);
    }
}
class Solution{
    public int[][] solution(int[][] nodeinfo) {
        int[][] answer = new int[2][nodeinfo.length];
        for(int i=0;i<2;i++) {
            answer[i] = new int[nodeinfo.length];
        }
        Node node[] = new Node[nodeinfo.length];
        for(int i=0;i<nodeinfo.length;i++) {
            node[i] = new Node(nodeinfo[i][0],nodeinfo[i][1],i+1);
        }
        Arrays.sort(node);
        
        Tree t = new Tree(node[0].idx,node[0].x);
        for(int i=1;i<node.length;i++) {
            t.add(node[i].idx,node[i].x);
        }
        t.preOrder(t.root);
        t.postOrder(t.root);
        for(int i=0;i<t.list1.size();i++) {
            answer[0][i] = t.list1.get(i);
            answer[1][i] = t.list2.get(i);
        }
        return answer;
    }
}
cs

 

반응형