https://www.acmicpc.net/problem/28217
28217번: 두 정삼각형
첫 번째 줄에는 $1$개의 수를, 두 번째 줄에는 $2$개의 수를, $\dots$, $N$번째 줄에는 $N$개의 수를 아래 그림과 같이 배치한 정삼각형 $A$, $B$가 주어진다. 각 위치에 있는 수는 $0$ 또는 $1$이다. 당신은
www.acmicpc.net
풀이
더보기
삼각형 A와 B를 비교해서
2개가 다르면 다른거 갯수를 전부 계산한 것 중 가장 작은 수를 표기
입력
첫번째 줄
N: 정삼각형 크기
N개 줄
A의 수
N개 줄
B의 수
가장 간단하게 하는 것은 A가 나올 수 있는 경우의 수를 모두 구한 후 전부 B랑 비교하면 해결된다.
1. 입력 정삼각형
2. A를 시계방향 rotate 1번 (120도)
3. 2번을 rotate 1번 (240도)
4. 1번을 좌우 반전
5. 4번을 시계방향 rotate 1번(120도)
6. 5번을 시계방향 rotate 1번(240도)
그리고 이 삼각형들을 B랑 일일히 비교하면 된다.
코드
package test28217_1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static int N;
static int[][] triangleA; // 정상, 왼쪽, 오른쪽, 대칭1, 대칭2, 대칭3
static int[][] triangleB;
static int[][] triangle1; // 오른쪽 120
static int[][] triangle2; // 오른쪽 240
static int[][] triangle3; // 좌우반전(정상)
static int[][] triangle4; // 반전 오른쪽 120
static int[][] triangle5; // 반전 오른쪽 240
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
MyScanner sc = new MyScanner(System.in);
N = sc.nextInt();
triangleA = new int[N][];
triangleB = new int[N][];
for (int i = 0; i < N; i++) {
triangleA[i] = new int[i + 1];
for (int j = 0; j <= i; j++) { // 정상
triangleA[i][j] = sc.nextInt();// 기본
}
}
int[][] triangle1 = rotate(triangleA); // 오른쪽 120
int[][] triangle2 = rotate(triangle1);// 오른쪽 240
int[][] triangle3 = change(triangleA);// 좌우반전(정상)
int[][] triangle4 = rotate(triangle3);// 반전 오른쪽 120
int[][] triangle5 = rotate(triangle4);// 반전 오른쪽 240
for (int i = 0; i < N; i++) {
triangleB[i] = new int[i + 1];
for (int j = 0; j <= i; j++) {
triangleB[i][j] = sc.nextInt();
}
}
int res1 = check1(triangleA);
int res2 = check1(triangle1);
int res3 = check1(triangle2);
int res4 = check1(triangle3);
int res5 = check1(triangle4);
int res6 = check1(triangle5);
int res = Math.min(res1, res2);
res = Math.min(res, res3);
res = Math.min(res, res4);
res = Math.min(res, res5);
res = Math.min(res, res6);
sc.write(res+"\n");
sc.close();
}
static int[][] rotate(int[][] triangle) {
int[][] res = new int[N][];
for (int i = 0; i < N; i++) {
res[i] = new int[i + 1];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
res[i][j] = triangle[N - 1 - j][i - j];
}
}
return res;
}
static int[][] change(int[][] triangle) {
int[][] res = new int[N][];
for (int i = 0; i < N; i++) {
res[i] = new int[i + 1];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
res[i][j] = triangle[i][i - j];
}
}
return res;
}
static int check1(int[][] triangle) {
int res = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
if (triangle[i][j] != triangleB[i][j]) {
res += 1;
}
}
}
return res;
}
static class MyScanner {
final BufferedReader reader;
final BufferedWriter writer;
static StringTokenizer tokenizer = null;
MyScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
writer = new BufferedWriter(new OutputStreamWriter(System.out));
}
String nextToken() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
String nextLine() throws IOException {
return reader.readLine();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
void write(String sb) throws IOException {
writer.write(sb);
}
void close() throws IOException {
reader.close();
writer.close();
}
}
}
'Algorithms > 2023 Pro 시험 준비' 카테고리의 다른 글
5549 행성 탐사 - 백준 (0) | 2023.08.30 |
---|---|
2169 로봇 조종하기 - 백준 (0) | 2023.08.29 |
2662 기업 투자 - 백준 (0) | 2023.08.22 |
3691 컴퓨터조립 - 백준 (0) | 2023.08.21 |
2166 다각형의 면적 - 백준 (0) | 2023.08.16 |