Algorithm
[코드트리 조별과제] 2 Days - TIL
김병찬
2024. 8. 4. 23:25
Topics
- 구간, 사각형 칠하기
- dy, dx techinique
📏 구간, 사각형 칠하기
Simulation 1, 2 과정을 이번주에는 진행하였는데, 그 중에서 의미있다고 생각하는 부분과 어려웠다고 생각하는 부분들을 가져와보았다.
우선, 구간과 사각형을 칠하는 것은 의미가 있다고 판단하여 푼 문제 중에서 내가 푼 방식들을 설명하고자 한다.
1. 흰검 칠하기
import java.util.Scanner;
public class Main {
public static final int MAX_K = 100000;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 명령의 수 n을 입력받음
int n = input.nextInt();
int[] arr = new int[2 * MAX_K + 1]; // 색을 저장할 배열
int[] cntB = new int[2 * MAX_K + 1]; // 각 위치의 검은색 타일의 개수 저장
int[] cntW = new int[2 * MAX_K + 1]; // 각 위치의 흰색 타일의 개수 저장
int b = 0, w = 0, g = 0; // b는 최종 검은색 타일의 개수, w는 흰색 타일의 개수, g는 회색 타일의 개수 저장하는 변수로 타일의 개수를 카운트할 때 사용됨.
int cur = MAX_K; // 배열의 중앙을 시작 위치로 설정
// 초기 위치 설정
for (int i = 0; i < n; i++) {
int x = input.nextInt(); // 위치를 이동할 변수
char c = input.next().charAt(0); // 방향을 결정하는 변수
if (c == 'L') {
// x만큼 왼쪽으로 이동
while (x-- > 0) {
arr[cur] = 1;
cntW[cur]++; // 왼쪽으로 이동하면서 x칸의 타일을 흰색으로 연속하게 칠함
if (x > 0) cur--;
}
} else if (c == 'R') {
while (x-- > 0) {
arr[cur] = 2;
cntB[cur]++; // 오른쪽으로 이동하면서 x칸의 타일을 검은색으로 연속하게 칠함
if (x > 0) cur++;
}
} else {
System.out.println("잘못 입력하였습니다(L or R)");
i--;
}
}
// 타일 색상 카운트 계산
for (int i = 0; i <= 2 * MAX_K; i++) {
if (cntB[i] >= 2 && cntW[i] >= 2) g++;
else if (arr[i] == 2) b++;
else if (arr[i] == 1) w++;
}
// 결과 출력
System.out.println(w + " " + b + " " + g);
input.close();
}
}
각 색깔의 타일의 개수를 저장하는 배열을 따로 저장하여 진행하였다. 처음 접근은 리스트를 이용해서 메모리를 절약하려고 하였으나, 실력 부족...으로 우선 메모리를 포기하고 배열을 선언하여 진행하였다.
🖥️ dy, dx technique
거울에 레이저 쏘기 2
import java.util.Scanner;
public class Main {
public static int n;
public static final int MAX_N = 1000;
public static char[][] matrix = new char[MAX_N][MAX_N];
public static int x;
public static int y;
public static int dir;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 격자의 개수 입력
n = input.nextInt();
input.nextLine(); // 개행 문자
for (int i = 0; i < n; i++) {
String mirror = input.nextLine(); // '/' or '\' 거울
for (int j = 0; j < mirror.length(); j++) {
matrix[i][j] = mirror.charAt(j);
}
}
int k = input.nextInt(); // 레이저를 쏘는 위치 (0-based index)
initialize(k);
int moveNum = simulate();
System.out.println(moveNum);
}
// 격자 범위 내에 있는지 확인하는 함수
public static boolean inRange(int x, int y) {
return (0 <= x && x < n && 0 <= y && y < n);
}
//
public static int simulate() {
int moveNum = 0;
while (inRange(x, y)) {
if (matrix[x][y] == '/') {
move(dir ^ 1);
} else {
move(3 - dir);
}
moveNum += 1;
}
return moveNum;
}
// 시작 위치와 방향 구하기
public static void initialize(int k) {
if (k <= n) {
x = 0; y = k - 1; dir = 0;
} else if (k <= 2 * n) {
x = k - n - 1; y = n - 1; dir = 1;
} else if (k <= 3 * n) {
x = n - 1; y = n - (k - 2 * n); dir = 2;
} else {
x = n - (k - 3 * n); y = 0; dir = 3;
}
}
public static void move(int nextDir) {
int[] dx = {1, 0, -1, 0}; // 동 - 남 - 서 - 북
int[] dy = {0, -1, 0, 1}; // 동 - 남 - 서 - 북
x += dx[nextDir];
y += dy[nextDir];
dir = nextDir;
}
}
위의 그림처럼 규칙을 찾고, 서로 다른 거울에 따라 어떻게 달라지는지를 파악하여 진행하였다.
- 후기
Simulation 1, 2 를 진행하면서 굉장히 많은 부족함을 느꼈다. 부분부분 막히는 것도 존재하였고 현재까지도 모르는 부분이 존재한다. 앞으로 더 많은 발전을 위해 시행착오를 거치면서 알고리즘 실력을 늘려야 겠다.