import java.util.*;
public class main {
    public static void main(String[] args) {

        Solution s = new Solution();

        System.out.println(Arrays.toString(s.Chap06Exmaple01(3, new int[] {15,27,12})));
        System.out.println(Arrays.toString(s.Chap06Example02(2, new String[]{"홍길동","이순신"}, new int[]{95,77})));
        System.out.println(s.Chap06Example03(5,3, new int[]{1,2,5,4,3}, new int[]{5,5,6,6,5}));
    }
}
class Solution {

    public int[] Chap06Exmaple01(int n, int[] arr) {

        List<Integer> list = new ArrayList<>();

        for(int a : arr)
            list.add(a);

        Collections.sort(list);
        Collections.reverse(list);

        for(int i = 0; i<arr.length;++i)
            arr[i] = (int)list.get(i);

        return arr;
    }
    public String[] Chap06Example02(int n, String[] name, int[] score) {
        String[] answer = new String[n];
        List<Score> list = new ArrayList<>();

        for(int i = 0; i<n;++i)
            list.add(new Score(name[i], score[i]));

        Collections.sort(list);

        for(int i = 0; i<n;++i)
            answer[i] = list.get(i).name;

        return answer;
    }
    public int Chap06Example03(int n, int k, int[] arrA, int[] arrB) {
        int answer = 0;

        Arrays.sort(arrA);
        Arrays.sort(arrB);
        for(int i = 0; i<k; ++i) {
            if(arrA[i] < arrB[arrB.length-i-1]) {
                arrA[i] = arrB[arrB.length-i-1];
            }
            else
                break;
        }
        for(int a : arrA)
            answer += a;
        return answer;
    }
}
class Score implements Comparable<Score> {
    public int score;
    public String name;
    public Score(String name, int score) {
        this.score = score;
        this.name = name;
    }
    @Override
    public int compareTo(Score s) {
        if(s.score < score)
            return 1;
        else if(s.score > score)
            return -1;
        else
            return 0;
    }
}

---

두 개 이상의 값을 동시에 정렬하라고 하면 클래스 만들고 Comaparable 상속 받고 하는게 내 기준에선 가장 편하다.

정렬 문제가 나올 때 라이브러리를 못쓰게 하지 않으면 라이브러리 그대로 쓰는게 제일 좋기도 하고

책도 답안을 보니 직접 구현하지 않더라...

import java.util.*;
public class main {
    public static void main(String[] args) {

        Solution s = new Solution();

        System.out.println(s.Chap05Example10(15,14,new int[][]{
                {0,0,0,0,0,1,1,1,1,0,0,0,0,0},//1
                {1,1,1,1,1,1,0,1,1,1,1,1,1,0},//2
                {1,1,0,1,1,1,0,1,1,0,1,1,1,0},//3
                {1,1,0,1,1,1,0,1,1,0,0,0,0,0},//4
                {1,1,0,1,1,1,1,1,1,1,1,1,1,1},//5
                {1,1,0,1,1,1,1,1,1,1,1,1,0,0},//6
                {1,1,0,0,0,0,0,0,0,1,1,1,1,1},//7
                {0,1,1,1,1,1,1,1,1,1,1,1,1,1},//8
                {0,0,0,0,0,0,0,0,0,1,1,1,1,1},//9
                {0,1,1,1,1,1,1,1,1,1,1,0,0,0},//10
                {0,0,0,1,1,1,1,1,1,1,1,0,0,0},//11
                {0,0,0,0,0,0,0,1,1,1,1,0,0,0},//12
                {1,1,1,1,1,1,1,1,1,1,0,0,1,1},//13
                {1,1,1,0,0,0,1,1,1,1,1,1,1,1},//14
                {1,1,1,0,0,0,1,1,1,1,1,1,1,1}//15
                }));
        System.out.println(s.Chap05Example11(5,6, new int[][]{
                {1,0,1,0,1,0},
                {1,1,1,1,1,1},
                {0,0,0,0,0,1},
                {1,1,1,1,1,1},
                {1,1,1,1,1,1}
                }));


    }
}
class Solution {
    public int Chap05Example10(int m, int n, int[][] blocks) {
        int answer = 0;
        int[][] visited = new int[m][n];

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (visited[i][j] == 0 && blocks[i][j] == 0) {
                    Chap05Example10_DFS(i, j, blocks, visited);
                    answer++;
                }
            }
        }

        return answer;
    }

    public void Chap05Example10_DFS(int y, int x, int[][] blocks, int[][] visited) {
        int[][] move = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
        visited[y][x] = 1;
        int tmpY = 0;
        int tmpX = 0;
        for (int i = 0; i < move.length; ++i) {
            tmpY = y + move[i][0];
            tmpX = x + move[i][1];
            if (tmpY < blocks.length && tmpX < blocks[0].length && tmpY >= 0 && tmpX >= 0 && blocks[tmpY][tmpX] == 0 && visited[tmpY][tmpX] == 0) {
                Chap05Example10_DFS(tmpY, tmpX, blocks, visited);
            }
        }
    }

    public int Chap05Example11(int m, int n, int[][] map) {
        int[][] visited = new int[m][n];
        int[][] move = new int[][]{{-1,0}, {1,0}, {0,1}, {0,-1}};
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{0, 0, 1});

        while (!queue.isEmpty()) {
            int[] tile = queue.poll();
            visited[tile[0]][tile[1]] = 1;

            if (tile[0] == m-1 && tile[1] == n-1) {
                return tile[2];
            }
            for(int i = 0; i<move.length;++i) {
                int tmpY = tile[0]+move[i][0];
                int tmpX = tile[1]+move[i][1];
                if(tmpY >= 0 && tmpX >= 0 && tmpY < m && tmpX < n && visited[tmpY][tmpX] == 0 && map[tmpY][tmpX] == 1) {
                    queue.add(new int[]{tmpY, tmpX, tile[2]+1});
                }
            }
        }
        return 0;
    }
}

---

이제는 지겹도록 본 DFS, BFS

타일에서의 (-1, 0), (1, 0), (0, 1),(0, -1) 처럼 이동과 관련된 것은 배열에 미리 담아두고 푸는게 실수를 줄일 수 있다고 해서 의식해서 써보고 있다.

초창기에 준비했던 포트폴리오. 반절이 게임이다.

'호랑이 담배피던 시절에' 카테고리의 다른 글

2017.02.18.  (0) 2019.02.18
2014.03 ~ 2014. 06 2D그래픽디자인  (2) 2018.04.19
2011.04.04.  (0) 2018.03.26
[Prezi] CG를 꿈꾸다  (0) 2018.03.26

+ Recent posts