• 문제 링크
16113번: 시그널
zxcvber는 외계인을 연구하는 과학자다. 그는 지난 10년간 우주에서 오는 시그널를 연구했지만, 아무런 성과가 없었다. 그러던 어느 날, 갑자기 우주에서 이상한 시그널이 오기 시작했다. zxcvber는
www.acmicpc.net
• 풀이 과정
입력받은 문자열을 substring 함수로 5등분할 범위를 지정해 배열에 저장한다.
0~9까지 숫자가 형태에 맞게 배열에 표현되어 있으며 각각의 영역을 일렬로 나타내면 아래와 같다.
0 | ### #.# #.# #.# ### |
1 | # # # # # |
2 | ### ..# ### #.. ### |
3 | ### ..# ### ..# ### |
4 | #.# #.# ### ..# ..# |
... | ... |
이때 맨 상단이 "###" 인 경우는 0, 2, 3, 5, 6, 7, 8, 9
'#' 은 1, "#.#" 은 4이 해당된다.
기본적으로 공백을 나타내는 '.' 은 무시하고 "###" 인 경우들은 5 * 3 의 영역을 탐색하여 일치하는 숫자를 도출해내고
1, 4 의 경우는 아래에 근거해 판별한다.
. | # | . | # | . | # |
. | # | . | # | . | # |
. | # | . | # | # | # |
. | # | . | . | . | # |
. | # | . | . | . | # |
즉 각각의 영역의 3번째, 또는 4번째 인덱스의 행이 어떤 문자인지 구별하여 1 또는 4를 도출해내며
이렇게 판별한 모든 숫자를 한 줄에 출력한다.
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static char[][] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
arr = new char[5][n / 5];
for (int i = 0; i < 5; i++)
arr[i] = str.substring(n / 5 * i, n / 5 * (i + 1)).toCharArray();
for (int i = 0; i < n / 5; i++) {
if (arr[0][i] == '#') {
if (i + 2 <= n / 5 && arr[0][i + 1] == '#' && arr[0][i + 2] == '#') {
bw.write(search(i) + "");
i += 3;
if (i >= n / 5)
break;
continue;
}
if (arr[3][i] == '#')
bw.write(1 + "");
else {
bw.write(4 + "");
i += 3;
if (i >= n / 5)
break;
}
}
}
bw.flush();
}
private static int search(int col) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++)
sb.append(arr[i][j + col]);
}
return check(sb.toString());
}
private static int check(String signal) {
String[] num = new String[10];
num[0] = "####.##.##.####";
num[2] = "###..#####..###";
num[3] = "###..####..####";
num[5] = "####..###..####";
num[6] = "####..####.####";
num[7] = "###..#..#..#..#";
num[8] = "####.#####.####";
num[9] = "####.####..####";
int result = 10;
for (int i = 0; i < 10; i++) {
if (signal.equals(num[i])) {
result = i;
break;
}
}
return result;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 14890 경사로 - Implementation / Java (0) | 2022.06.26 |
---|---|
[백준] 14503 로봇 청소기 - Implementation / Java (0) | 2022.06.25 |
[백준] 14499 주사위 굴리기 - Implementation / Java (0) | 2022.06.23 |
[백준] 16506 CPU - Implementation / Java (0) | 2022.06.22 |
[백준] 14500 테트로미노 - Implementation / Java (0) | 2022.06.21 |
댓글