Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/CCR

 

algospot.com :: CCR

문자 인식 문제 정보 문제 DCinside에 알고리즘 갤러리가 생겼다! 그러나 알고리즘 갤러리는 수많은 수갤러스(수능 갤러리 유저)들의 공격을 받곤 했는데, 수학 I 교과목에서 가장 쉬운 부분이 알

www.algospot.com

 

풀이입니다. 🤔

 

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Scanner;
 
public class Main {
    private static final char STAR = '*', BLANK = '_';
    private static final char[][] board = new char[25][81];
    private static final int[] PLACE = {00}, TOP = {-10}, RIGHT = {01}, BOTTOM = {10}, LEFT = {0-1};
    private static final int[][] directions = new int[][]{TOP, RIGHT, BOTTOM, LEFT};
    private static int r, c;
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            r = scanner.nextInt();
            c = scanner.nextInt();
            for (int i = 0; i < r; ++i) {
                String line = scanner.next();
                for (int j = 0; j < c; ++j) board[i][j] = line.charAt(j);
            }
 
            int ret = 0;
            for (int y = 0; y < r; ++y) {
                for (int x = 0; x < c; ++x) {
                    if (board[y][x] != STAR) continue;
                    if (!hasStar(y, x, RIGHT)) ret += get14(y, x);
                    else {
                        if (hasStar(y, x, BOTTOM)) ret += get05689(y, x);
                        else ret += get237(y, x);
                    }
 
                    clearStar(y, x);
                }
            }
 
            System.out.println(ret);
        }
    }
 
    private static int getEndOfStarCount(int y, int x, int[] direction) {
        int ret = 0;
        for (int ny = y, nx = x; hasStar(ny, nx, direction); ny += direction[0], nx += direction[1]) {
            ++ret;
        }
        return ret;
    }
 
    private static boolean hasStar(int y, int x, int[] direction) {
        int ny = y + direction[0], nx = x + direction[1];
        return ny >= 0 && ny < r && nx >= 0 && nx < c && board[ny][nx] == STAR;
    }
 
    private static void clearStar(int y, int x) {
        board[y][x] = BLANK;
        for (int[] direction : directions) {
            int ny = y + direction[0], nx = x + direction[1];
            if (hasStar(ny, nx, PLACE)) clearStar(ny, nx);
        }
    }
 
    private static int get05689(int y, int x) {
        int endOfRightCount = getEndOfStarCount(y, x, RIGHT);
        int ny = y + RIGHT[0* endOfRightCount, nx = x + RIGHT[1* endOfRightCount;
        if (hasStar(ny, nx, BOTTOM)) {
            return get089(y, x, y + getEndOfStarCount(ny, nx, BOTTOM));
        } else {
            return get56(y, x);
        }
    }
 
    private static int get089(int y, int x, int endOfY) {
        int ny = y, nx = x, rightCount = 0;
        while (hasStar(ny, nx, BOTTOM)) {
            ny += BOTTOM[0]; nx += BOTTOM[1];
            if (hasStar(ny, nx, RIGHT)) ++rightCount;
        }
 
        if (ny != endOfY) return 9;
        else {
            switch (rightCount) {
                case 1return 0;
                case 2return 8;
                defaultthrow new IllegalArgumentException();
            }
        }
    }
 
    private static int get56(int y, int x) {
        int ny = y, nx = x, rightCount = 0;
        while (hasStar(ny, nx, BOTTOM)) {
            ny += BOTTOM[0]; nx += BOTTOM[1];
            if (hasStar(ny, nx, RIGHT)) ++rightCount;
        }
 
        switch (rightCount) {
            case 1return 5;
            case 2return 6;
            defaultthrow new IllegalArgumentException();
        }
    }
 
    private static int get237(int y, int x) {
        int endOfRightCount = getEndOfStarCount(y, x, RIGHT);
        int ny = y + RIGHT[0* endOfRightCount, nx = x + RIGHT[1* endOfRightCount, leftCount = 0;
        while (hasStar(ny, nx, BOTTOM)) {
            ny += BOTTOM[0]; nx += BOTTOM[1];
            if (hasStar(ny, nx, LEFT)) ++leftCount;
        }
 
        switch (leftCount) {
            case 0return 7;
            case 1return 2;
            case 2return 3;
            defaultthrow new IllegalArgumentException();
        }
    }
 
    private static int get14(int y, int x) {
        int ny = y, nx = x;
        while (hasStar(ny, nx, BOTTOM)) {
            ny += BOTTOM[0]; nx += BOTTOM[1];
            if (hasStar(ny, nx, RIGHT) || hasStar(ny, nx, LEFT)) return 4;
        }
 
        return 1;
    }
}
cs

 

문제의 내용 중 주의할 내용은 아래와 같습니다. 

 

문자들은 바로 옆에 인접하지 않으므로 연속된 검은 색 격자칸들의 집합이 어떠한 문자도 표현하지 않는 잘못된 경우는 존재하지 않는다.

 

숫자를 인식하기 위해 모든 특징에 맞춰 격자칸들의 집합을 순회하는 것이 아닌, 문자에 존재하는 특이점을 이용해서 인식하도록 한다면 쉽게 문제를 해결해볼 수 있습니다. 
특이점의 예시는 다음과 같습니다.

- 1 과 4 의 경우는 시작점의 오른쪽에는 격자가 존재하지 않습니다. 
- 2, 3 그리고 7은 시작점의 오른쪽에는 격자가 존재하지만 아래쪽에는 존재하지 않습니다.
- 5 와 6 의 시작점으로 오른쪽 끝 격자로부터 아래칸은 존재하지 않습니다.
- ....

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[QUADTREE] 쿼드 트리 뒤집기  (0) 2022.01.14
[JOSEPHUS] 조세푸스 문제  (0) 2022.01.09
[SPETIAL] Spatial Concepts Test  (0) 2022.01.09
[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MVP] Most Valuable Programmer  (0) 2022.01.07
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/SPETIAL

 

algospot.com :: SPETIAL

Spatial Concepts Test 문제 정보 문제 The Flathead Testing Corporation (FTC) supplies various tests for Human Resources departments at many companies. One type of test they supply includes spatial concepts questions such as: When the following figure

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.io.*;
 
public class Main {
    // [1] 이 (x - 1)이고, [1] 의 orientation 1 이 (y-1)일 때, [2] 의 Face 와 [1] 과 접촉 orientation
    private static final int[][][] ROTATIONS = new int[][][]{
            {{10}, {40}, {30}, {20}},
            {{41}, {03}, {23}, {53}},
            {{11}, {02}, {33}, {50}},
            {{21}, {01}, {43}, {51}},
            {{31}, {00}, {13}, {52}},
            {{12}, {22}, {32}, {42}}
    };
 
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Face[] cube = new Face[6];
        Face[] answers = new Face[3];
        boolean[] ret = new boolean[5];
        int testCases = Integer.parseInt(reader.readLine());
        for (int testCase = 1; testCase <= testCases; ++testCase) {
            readFaces(cube, reader.readLine());
 
            int answerCount = 0;
            for (int i = 0, len = ret.length; i < len; ++i) {
                readFaces(answers, reader.readLine());
                ret[i] = isAnswer(cube, answers);
                if (ret[i]) ++answerCount;
            }
 
            System.out.printf("%d %d %c %c %c %c %c\n",
                    testCase,
                    answerCount,
                    getTrueOrFalse(ret[0]),
                    getTrueOrFalse(ret[1]),
                    getTrueOrFalse(ret[2]),
                    getTrueOrFalse(ret[3]),
                    getTrueOrFalse(ret[4])
            );
        }
    }
 
    private static void readFaces(Face[] buffer, String fold) {
        for (int i = 0, len = fold.length(); i < len; i+=2) {
            buffer[i / 2= new Face(fold.charAt(i), fold.charAt(i + 1- '1');
        }
    }
 
    private static boolean isAnswer(Face[] cube, Face[] answer) {
        boolean ret = false;
        for (int i = 0, faceLength = cube.length; i < faceLength && !ret; ++i) {
            // [1] 영역 검증 및 회전횟수 연산
            if (cube[i].shape != answer[0].shape) continue;
            // cube 의 orientation 과 answer 의 orientation 비교하여 [1] 의 1위치를 구한다.
            // f(cube, answer) = rotation
            // f(0, 0) = 0, f(0, 1) = 3, f(0, 2) = 2, f(0, 3) = 1
            // f(2, 2) = 0, f(2, 3) = 3, f(2, 0) = 2, f(2, 1) = 1
            int rotation = (4 + cube[i].orientation - answer[0].orientation) % 4;
 
            // [2] 영역 검증
            int[] right = ROTATIONS[i][rotation];
            if (cube[right[0]].shape != answer[1].shape) continue;
            if ((cube[right[0]].orientation - right[1+ 4) % 4 != answer[1].orientation) continue;
 
            // [3] 영역 검증
            int[] left = ROTATIONS[i][(rotation + 1) % 4];
            if (cube[left[0]].shape != answer[2].shape) continue;
            if ((cube[left[0]].orientation + 3 - left[1]) % 4 != answer[2].orientation) continue;
 
            // 모든 검증이 통과한다면, 정답 처리
            ret = true;
        }
 
        return ret;
    }
 
    private static char getTrueOrFalse(boolean ret) {
        return ret ? 'Y' : 'N';
    }
 
    private static final class Face {
        final char shape;
        final int orientation;
 
        public Face(char shape, int orientation) {
            this.shape = shape;
            this.orientation = orientation;
        }
    }
}
cs

 

문제가 특별한 알고리즘 기술을 요구하는 것은 아니지만, 펼쳐진 직육면체에 대해 보여지는 단면을 나타내도록 하는 것이 쉽지 않았습니다. 
어려운 문제를 분리해서 각 단계마다 해결해나가는 것이 중요한 문제라 생각합니다. 

 

1. [1] 면에 대한 후보 탐색 및 회전 횟수 구하기

 

기준이 되는 [1] 면을 찾기 위해, 직육면체의 면(cube)들과 정답의 첫번째 면(answer[0])모양(shape)이 동일한 것을 탐색합니다. 

동일한 모양을 찾았다면, 현재의 회전 상태를 [1] 면의 1번을 기준으로 연산합니다. 

예를들어 Cube F3E4E2D3C2F3 가 입력이 되었을 때 예상되는 정답 C2D2F2 가 입력되었다면, 첫번째 정답 면 C2 의 모양 C 는 Cube 의 5번째 면 C2 와 동일하기 때문에 [1] 은 5번째 면이 됨을 알 수 있습니다. 

첫번째 예상되는 정답 면은 정위는 2, 5번째 면의 정위는 2 이기 때문에 현재 직육면체는 5번째 면의 1 을 기준으로 앞에 표시되고 있습니다. 
입력된 직육면체의 면이 3이고, 예상되는 정답의 면이 4이면 어떨까요?

시계방향으로 한번 회전했기 때문에 [1] 의 1 은 4를 기준으로 표시됩니다.

입력된 직육면체의 면이 3이고, 예상되는 정답의 면이 1이면 어떨까요?

시계방향으로 두번 회전했기 때문에 [1] 의 1 은 3를 기준으로 표시됩니다.

 

수학적 귀납법으로 직육면체 면의 정위가 x, 예상되는 정위가 y 라 할 때 [1] 의 1 은 어떤 기준으로 나타낼지 알아볼 수 있습니다.

이는 다음과 같은 식으로 정의 됩니다. 

 

f(x, y) = (4 + x - y) % 4

 

 

2. 회전 횟수에 근거하여, [2] 면에 대한 정보 검증 

 

앞에서, [1] 면의 후보와 표시되는 정위를 구할 수가 있었습니다. 

이를 바탕으로, [2] 면의 표시되는 정보를 알 수 있습니다. 
미리 선언한 배열 ROTATIONS 은 [1] 면이 x 이고 정위 1 이 y 로 표시될 때 [2] 면의 직육면체면의 번호와 x면과 접촉하는 정위입니다. 

예를들어 Cube F3E4E2D3C2F3 가 입력이 되었을 때 예상되는 정답 C2D2F2 가 입력되었고, [1]면이 5번째(C2), 정위 1을 기준으로 표시하고 있기 때문에 [2] 면은 4번째(D3)이며 5번째 면과 접촉된 정위는 2입니다.

 

ROTATIONS[4][0] = {3, 1}

 

배열에 존재하는 직육면체 번호를 이용하여 [2] 의 모양 검증 거친 후, [1] 과 접촉된 정위를 [2] 의 정위 1 이 되도록 회전시킵니다. 

앞의 예시에서 4번째 접촉면은 2 이며, [2] 의 1 은 2를 기준으로 표시하고 있습니다. 

 

 

3. 회전 횟수에 근거하여, [3] 면에 대한 정보 검증

 

마지막 단계는 서술된 2번의 단계와 유사합니다.

[3] 의 면은 [2] 의 면 옆에 있기 때문에 앞단계에서 선택된 ROTATIONS 의 다음이라 생각할 수 있습니다. 

예를들어 앞단계에서 ROTATIONS[4][0] 이 선택되었는데 [3] 면은 [2] 면의 옆에 있으니, ROTATIONS[4][1] 로 볼 수 있습니다.

 

이 후 단계는 서술된 2의 단계와 동일하지만, 표시하는 상단 기준이 1에서 4로 변경되었기 때문에 이 부분만 조금 변경하면 정답 검증을 해볼 수 있습니다. 

예를들어 Cube F3E4E2D3C2F3 가 입력이 되었을 때 예상되는 정답 C2D2F2 가 입력되었고, [1]면이 5번째(C2), 정위 1을 기준으로 표시하고 있고 때문에 [2] 면은 4번째(D3)이며 5번째 면과 접촉된 정위는 2일 때, [3]면은 1번째(F3)이며 5번째 면과 접촉된 정위는 1입니다.
접촉된 정위 1 을 [3] 의 4 를 기준으로 맞춰 검증해봅니다.

 

 


문제 해결을 위해 머릿속으로만 구상을 해보면, 쉽지가 않은 문제였습니다. 

A4 용지와 펜을 준비하여 직접 정육면체를 그려보고, 회전을 시켜보며 각각의 단계에 해당하는 요구사항을 만들어 일반화를 시킨다면, 충분히 이해하고 풀 수 있을 것이라 생각합니다. 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[JOSEPHUS] 조세푸스 문제  (0) 2022.01.09
[CCR] 문자 인식  (0) 2022.01.09
[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MVP] Most Valuable Programmer  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/WORDLENGTH

 

algospot.com :: WORDLENGTH

단어 길이 재기 문제 정보 문제 (주의: 이 문제는 TopCoder SRM 202 Div 1 Easy 의 번역입니다) 단어들의 길이는 어떤 문장이 어렵게 쓰여진 문장인지, 쉬운 문장인지를 가르는 데 좋은 척도가 됩니다. 예

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    private static final char HYPHEN = '-';
    private static final char BLANK = ' ';
 
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int cases = Integer.parseInt(in.readLine());
        while (cases-- > 0) {
            StringBuilder builder = new StringBuilder();
            int n = Integer.parseInt(in.readLine());
            for (int i = 0; i < n; ++i) {
                String line = calibrateInputString(in.readLine());
                for (int j = 0, len = line.length(); j < len; ++j) {
                    int curSize = builder.length();
                    char c = calibrateCharAt(line, j, i + 1 == n);
                    switch (c) {
                        case BLANK:
                        case HYPHEN:
                            if (curSize != 0) {
                                if (isAlphabet(builder.charAt(curSize - 1))) builder.append(c);
                                else builder.setCharAt(curSize - 1, BLANK);
                            }
                            break;
 
                        default:
                            if (curSize == 0 || builder.charAt(curSize - 1!= HYPHEN) builder.append(c);
                            else builder.setCharAt(curSize - 1, c);
                    }
                }
            }
 
 
            String[] words = builder.toString().trim().split(String.valueOf(BLANK));
            double ret = 0;
            if (words.length != 0) {
                for (String word : words) ret += word.length();
                ret /= words.length;
            }
 
            System.out.printf("%.3f\n", ret);
        }
 
        in.close();
    }
 
    private static String calibrateInputString(String line) {
        // line 의 마지막이 Alphabet 이라면, 단어의 끝으로 봐야한다.
        return (!line.isEmpty() && isAlphabet(line.charAt(line.length() - 1))) ? line + BLANK : line;
    }
 
    private static char calibrateCharAt(String str, int index, boolean isEndOfLine) {
        char c = str.charAt(index);
        if (c != HYPHEN) return c;
        return (index + 1 == str.length() && !isEndOfLine) ? HYPHEN : BLANK;
    }
 
    private static boolean isAlphabet(char c) {
        return c >= 'a' && c <= 'z';
    }
}
cs

 

어려운 문제는 아니지만, 주의해야할 테스트 케이스가 있습니다. 
기본 예제와 더불어 아래 테스트 케이스도 통과하는지 살펴볼 필요가 있습니다.

 

5
hello-$
there-$
world$
4
a-$
-$
-$
b$
3
i am ver-$
y sleepy arent-$
 you$
1
jong-man rules$
4
a$ 
b-$
c-$
d$
// [a, bcd]

(주의 : 위 입력의 $ 기호는 줄바꿈을 보여주기 위해 추가한 것이며, 실제 입력에는 존재하지 않습니다.  )

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[CCR] 문자 인식  (0) 2022.01.09
[SPETIAL] Spatial Concepts Test  (0) 2022.01.09
[MVP] Most Valuable Programmer  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
[DIAMOND] 다이아몬드  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/MVP

 

algospot.com :: MVP

Most Valuable Programmer 문제 정보 문제 The season of professional programming league has just ended and it’s time for the MVP(Most Valuable Programmer) voting! There are n newspapers reporters who can vote, and the number of candidate programmers

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        int[][] prefers = new int[100][10];
        int[] votes = new int[10], positions = new int[100];
        boolean[] eliminated = new boolean[10];
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int n = scanner.nextInt(), m = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    prefers[i][j] = scanner.nextInt() - 1;
                }
            }
 
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    int prefer = -1;
                    while (positions[j] < m && prefer == -1) {
                        // 이 부분 구현을 위해 Queue 를 사용해도 좋다.
                        // 이 코드에서는 제거된 프로그래머를 제외하기 위해 prefers 의 Position 만을 제어하고 있다.
                        if (eliminated[prefers[j][positions[j]]]) ++positions[j];
                        else prefer = prefers[j][positions[j]];
                    }
 
                    if (prefer != -1++votes[prefer];
                }
 
                int minVote = Integer.MAX_VALUE, minCount = 0, clearedCount = 0;
                for (int j = 0; j < m; ++j) {
                    if (eliminated[j]) ++clearedCount;
                    else {
                        if (minVote == votes[j]) ++minCount;
                        else if (minVote > votes[j]) {
                            minCount = 1;
                            minVote = votes[j];
                        }
                    }
                }
                if (m - clearedCount != minCount) {
                    for (int j = 0; j < m; ++j) if (votes[j] == minVote) eliminated[j] = true;
                }
 
                Arrays.fill(votes, 0);
            }
 
            List<Integer> answers = new ArrayList<>();
            for (int i = 0; i < m; ++i) if (!eliminated[i]) answers.add(i + 1);
            for (int i = 0, size = answers.size(); i < size; ++i) {
                System.out.printf("%d%s", answers.get(i), (i + 1 == size) ? "\n" : " ");
            }
 
            Arrays.fill(eliminated, false);
            Arrays.fill(positions, 0);
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[SPETIAL] Spatial Concepts Test  (0) 2022.01.09
[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
[DIAMOND] 다이아몬드  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/MISPELL

 

algospot.com :: MISPELL

Mispelling 문제 정보 문제 Misspelling is an art form that students seem to excel at. Write a program that removes the nth character from an input string. 입력 The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the numb

www.algospot.com

 

풀이입니다. 🤔

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        for (int i = 1; i <= cases; ++i) {
            int number = scanner.nextInt();
            String character = scanner.next();
 
            System.out.printf("%d ", i);
            for (int j = 0, size = character.length(); j < size; ++j) {
                if (j + 1 != number) System.out.print(character.charAt(j));
            }
            System.out.println();
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MVP] Most Valuable Programmer  (0) 2022.01.07
[DIAMOND] 다이아몬드  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
[CANDLESTICK] Candlestick Charts  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/DIAMOND

 

algospot.com :: DIAMOND

다이아몬드 문제 정보 문제 ......#............................ ..#.######......................... .##########..........###........... ..#.#########......#######......... ......#............#######......... ....................####.#......... ...

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    private static final char SHARP = '#';
    private static final char[][] board = new char[50][50];
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int h = scanner.nextInt(), w = 0;
            for (int i = 0; i < h; ++i) {
                String line = scanner.next();
                w = line.length();
                for (int j = 0; j < w; ++j) {
                    board[i][j] = line.charAt(j);
                }
            }
 
            int ret = 0;
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    ret = Math.max(ret, getMaxDiamondLength(i, j, h, w));
                }
            }
 
            System.out.println(ret);
        }
    }
 
    private static int getMaxDiamondLength(int y, int x, int h, int w) {
        int ret = 0, safety = 0;
        for (int goal = 1; goal <= h; goal += 2) {
            int center = goal / 2;
            for (int distance = safety; distance < goal; ++distance) {
                if (y + distance >= h
                        || !hasLine(y + distance, x, w, (distance > center) ? goal - distance - 1 : distance)) {
                    return ret;
                }
            }
 
            ret = goal;
            safety = center; // 다이아몬드가 있는 것이 증명되었다면, 다음 회차에서 Center 까지는 정확한 # 이 존재함을 보장한다.
        }
        return ret;
    }
 
    private static boolean hasLine(int y, int x, int w, int distance) {
        int startX = x - distance, endX = x + distance;
        if (startX < 0return false;
        if (endX >= w) return false;
 
        for (int i = startX; i <= endX; ++i) if (board[y][i] != SHARP) return false;
        return true;
    }
}
cs이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[MVP] Most Valuable Programmer  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
[CANDLESTICK] Candlestick Charts  (0) 2022.01.06
[KAKURO1] Kakuro I  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/ENCODING

 

algospot.com :: ENCODING

Encoding 문제 정보 문제 Chip and Dale have devised an encryption method to hide their (written) text messages. They first agree secretly on two numbers that will be used as the number of rows (R) and columns (C) in a matrix. The sender encodes an int

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    private static final char EMPTY = 0;
    private static final Map<Character, String> binaries = new HashMap<>();
    private static final int[][] directions = new int[][]{{01}, {10}, {0-1}, {-10}};
 
    static {
        char[] buffer = new char[]{'0''0''0''0''0'};
 
        // 케이스마다 문자를 이진수로 변환할 시, 중복계산을 없애기 위해 초기화 시 기록을 한다.
        binaries.put(' 'new String(buffer));
        for (char c = 'A'; c <= 'Z'++c) {
            int i = buffer.length - 1;
            do {
                if (buffer[i] == '0') {
                    buffer[i] = '1';
                    break;
                }
                else buffer[i--= '0';
            } while (i >= 0);
 
            binaries.put(c, new String(buffer));
        }
    }
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[][] board = new char[20][20];
 
        int cases = scanner.nextInt();
        for (int n = 1; n <= cases; ++n) {
            int r = scanner.nextInt(), c = scanner.nextInt(), y = 0, x = 0, directionIndex = 0;
            String line = scanner.nextLine().trim();
            for (char character : line.toCharArray()) {
                for (char digit : binaries.get(character).toCharArray()) {
                    board[y][x] = digit;
 
                    int[] direction = directions[directionIndex];
                    if (outOfBoard(y + direction[0], r)
                            || outOfBoard(x + direction[1], c)
                            || board[y + direction[0]][x + direction[1]] != EMPTY) {
                        directionIndex = (directionIndex + 1) % directions.length;
                        direction = directions[directionIndex];
                    }
                    y += direction[0];
                    x += direction[1];
                }
            }
 
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < r; ++i) {
                for (int j = 0; j < c; ++j) {
                    builder.append(board[i][j] == EMPTY ? '0' : board[i][j]);
                    board[i][j] = EMPTY;
                }
            }
 
            System.out.printf("%d %s\n", n, builder);
        }
    }
 
    private static boolean outOfBoard(int value, int max) {
        return value < 0 || value >= max;
    }
}
cs


이 문제는 아래 문제와 짝을 이루는 문제로, 안내에 따라 문자를 Encoding 합니다. 

 

[DECODE] Decoding


주요 살펴볼 사항은 총 두가지입니다. 

 

1. 제한된 이진수 변환을 빠르게 하기 위해, 초기화 과정을 수행했습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;
 
public class Main {
   static {
        char[] buffer = new char[]{'0''0''0''0''0'};
 
        // 케이스마다 문자를 이진수로 변환할 시, 중복계산을 없애기 위해 초기화 시 기록을 한다.
        binaries.put(' 'new String(buffer));
        for (char c = 'A'; c <= 'Z'++c) {
            int i = buffer.length - 1;
            do {
                if (buffer[i] == '0') {
                    buffer[i] = '1';
                    break;
                }
                else buffer[i--= '0';
            } while (i >= 0);
 
            binaries.put(c, new String(buffer));
        }
    }
}
cs

 

 

2. 나선모양으로 변경하기 위해, 4가지 방향에 대한 상대좌표를 이용하도록 하였습니다.

 

1
    private static final int[][] directions = new int[][]{{01}, {10}, {0-1}, {-10}};
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[MISPELL] Mispelling  (0) 2022.01.06
[DIAMOND] 다이아몬드  (0) 2022.01.06
[CANDLESTICK] Candlestick Charts  (0) 2022.01.06
[KAKURO1] Kakuro I  (0) 2022.01.06
[FESTIVAL] 록 페스티벌  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/CANDLESTICK

 

algospot.com :: CANDLESTICK

Candlestick Charts 문제 정보 문제 A candlestick chart, often abbreviated to candle chart, is a variation of a bar chart. It is mostly used to describe the price of a stock over time. You are using a simple candle chart which looks like above. Each ba

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] intervals = new int[2000][2];
 
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                intervals[i][0= scanner.nextInt();
                intervals[i][1= scanner.nextInt();
            }
 
            int ret = 0;
            for (int i = 0; i < n; ++i) {
                int min = -1, max = Integer.MAX_VALUE;
                for (int day = 0, maxDay = n - i; day < maxDay; ++day) {
                    min = Math.max(min, intervals[i + day][0]);
                    max = Math.min(max, intervals[i + day][1]);
 
                    ret = Math.max(ret, (day + 1* (max - min));
                }
            }
            System.out.println(ret);
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[DIAMOND] 다이아몬드  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
[KAKURO1] Kakuro I  (0) 2022.01.06
[FESTIVAL] 록 페스티벌  (0) 2022.01.06
[NOTE] Note  (0) 2022.01.06
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/KAKURO1

 

algospot.com :: KAKURO1

Kakuro I 문제 정보 문제 카쿠로는 흔히 십자말풀이 수학 버전이라고 불리는 논리 퍼즐이다. 카쿠로는 위와 같이 생긴 정사각형의 게임판을 가지고 하는 퍼즐로, 이 게임판의 각 칸은 흰 칸이거나,

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Answer> answers = new ArrayList<>();
        int[][] board = new int[20][20], directions = new int[][] {{01}, {10}};
 
        int cases = scanner.nextInt();
        System.out.println(cases);
        while (cases-- > 0) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    board[i][j] = scanner.nextInt();
                }
            }
            for (int i = 0, len = directions.length; i < len; ++i) {
                int[] direction = directions[i];
                for (int j = 0; j < n; ++j) {
                    for (int k = 0; k < n; ++k) {
                        if (board[j][k] != 0continue;
 
                        int ret = 0, y = j + direction[0], x = k + direction[1];
                        while (y < n && x < n && board[y][x] != 0) {
                            ret += board[y][x];
                            y += direction[0];
                            x += direction[1];
                        }
                        if (ret != 0) answers.add(new Answer(j + 1, k + 1, i, ret));
                    }
                }
            }
 
            System.out.println(n);
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    System.out.printf("%d%c", (board[i][j] == 0) ? 0 : 1, (j == n - 1) ? '\n' : ' ');
                }
            }
            System.out.println(answers.size());
            for (Answer answer : answers) System.out.println(answer);
            answers.clear();
        }
    }
 
    private static final class Answer {
        final int y, x, direction, value;
 
        public Answer(int y, int x, int direction, int value) {
            this.y = y;
            this.x = x;
            this.direction = direction;
            this.value = value;
        }
 
        @Override
        public String toString() {
            return String.format("%d %d %d %d", y, x, direction, value);
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[ENCODING] Encoding  (0) 2022.01.06
[CANDLESTICK] Candlestick Charts  (0) 2022.01.06
[FESTIVAL] 록 페스티벌  (0) 2022.01.06
[NOTE] Note  (0) 2022.01.06
[MAGICPOWER] 마력  (0) 2022.01.05
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/FESTIVAL

 

algospot.com :: FESTIVAL

록 페스티벌 문제 정보 문제 커다란 공연장을 빌려서 록 페스티벌을 개최하려고 합니다. 이 페스티벌은 여러 날 동안 진행되며, 하루에 한 팀의 밴드가 공연장에서 콘서트를 하게 됩니다. 전체

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        int[][] caches = new int[1000][1000];
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int n = scanner.nextInt(), l = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                caches[0][i] = (i == 0) ? scanner.nextInt() : caches[0][i - 1+ scanner.nextInt();
            }
 
            for (int i = 1; i < n; ++i) {
                for (int j = i; j < n; ++j) {
                    // i일부터 (j-i+1)일 연속 공연했을 때의 총 비용
                    caches[i][j] = caches[i - 1][j] - caches[i - 1][i - 1];
                }
            }
 
            double ret = Double.MAX_VALUE;
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    int len = j - i + 1;
                    if (len >= l) ret = Math.min(ret, ((double) caches[i][j]) / len);
                }
            }
 
            System.out.printf("%.8f\n", ret);
        }
    }
}
cs

 

최소 평균 비용을 알기 위해, 모든 경우의 수를 탐색해 보는 것을 고민해봤습니다. 

i일부터 연속 (n-i+1)일 간의 총 비용을 구한 뒤, 최소 평균을 구하도록 되어 있습니다. 

해당 요구사항은 파스칼의 삼각형과 같이 처음부터 모든 덧셈을 하는 것이 아닌 일련의 점화식을 만들면 제곱 시간안에 구해볼 수 있습니다.


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[CANDLESTICK] Candlestick Charts  (0) 2022.01.06
[KAKURO1] Kakuro I  (0) 2022.01.06
[NOTE] Note  (0) 2022.01.06
[MAGICPOWER] 마력  (0) 2022.01.05
[DECODE] Decoding  (0) 2022.01.05
Posted by N'