[KAKURO1] Kakuro I
개발이야기/알고스팟2022. 1. 6. 11:44
Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.
문제 : https://www.algospot.com/judge/problem/read/KAKURO1
풀이입니다. 🤔
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[][] {{0, 1}, {1, 0}};
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] != 0) continue;
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 |