Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 객체지향
- OOP
- docker
- 동시성 문제
- JWT
- 로그인
- 스프링부트
- Spring Security
- AWS
- redis
- EC2
- Java
- 네트워크
- JMeter
- 형상관리
- 인메모리
- Spring
- 성능 개선
- 프로토콜
- OSI 7계층
- 4-way-handshake
- 3-way-handshake
- 백준
- 트러블 슈팅
- 코딩테스트
- TCP/IP
- 알고리즘
- RDS
- N+1 문제
Archives
- Today
- Total
열공스토리
[백준] 23288 - 주사위 굴리기2 (JAVA 객체지향적으로 설계해 보기) 본문
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int N;
private static int M;
private static final int[] nextN = {0, 1, 0, -1};
private static final int[] nextM = {1, 0, -1, 0};
static class Dice {
private int diceNet[];
private int n;
private int m;
private int direction;
Dice() {
this.diceNet = new int[]{1, 3, 4, 5, 2, 6}; // 위, 동, 서, 남, 북, 아래
this.n = 0;
this.m = 0;
this.direction = 0; // 0 - 동, 1 - 남, 2 - 서, 3 - 북
}
void move() {
n += nextN[direction];
m += nextM[direction];
diceNetAfterEastMove();
diceNetAfterWestMove();
diceNetAfterSouthMove();
diceNetAfterNorthMove();
}
void diceNetAfterEastMove() {
if (direction == 0) {
int temp = diceNet[0];
diceNet[0] = diceNet[2];
diceNet[2] = diceNet[5];
diceNet[5] = diceNet[1];
diceNet[1] = temp;
}
}
void diceNetAfterWestMove() {
if (direction == 2) {
int temp = diceNet[0];
diceNet[0] = diceNet[1];
diceNet[1] = diceNet[5];
diceNet[5] = diceNet[2];
diceNet[2] = temp;
}
}
void diceNetAfterSouthMove() {
if (direction == 1) {
int temp = diceNet[0];
diceNet[0] = diceNet[4];
diceNet[4] = diceNet[5];
diceNet[5] = diceNet[3];
diceNet[3] = temp;
}
}
void diceNetAfterNorthMove() {
if (direction == 3) {
int temp = diceNet[0];
diceNet[0] = diceNet[3];
diceNet[3] = diceNet[5];
diceNet[5] = diceNet[4];
diceNet[4] = temp;
}
}
void decideDirection(ScoreMap scoreMap) {
rotateClockwise(scoreMap);
rotateCounterClockwise(scoreMap);
rotateReserve(scoreMap);
}
void rotateClockwise(ScoreMap scoreMap) {
int number = diceNet[5];
if (scoreMap.isHigherThan(number, n, m)) {
direction = (direction + 1) % 4;
}
}
void rotateCounterClockwise(ScoreMap scoreMap) {
int number = diceNet[5];
if (scoreMap.isLowerThan(number, n, m)) {
direction = (direction + 3) % 4;
}
}
void rotateReserve(ScoreMap scoreMap) {
int moveN = n + nextN[direction];
int moveM = m + nextM[direction];
if (scoreMap.isPositionOutOfBounds(moveN, moveM)) {
direction = (direction + 2) % 4;
}
}
}
static class ScoreMap {
private final int map[][];
ScoreMap(int[][] map) {
this.map = map;
}
int getScore(int n, int m) {
return map[n][m] * getSquare(n, m, new boolean[N][M]);
}
int getSquare(int n, int m, boolean[][] visited) {
visited[n][m] = true;
int score = 1;
for (int i = 0; i < 4; i++) {
int moveN = n + nextN[i];
int moveM = m + nextM[i];
if (moveN < 0 || moveM < 0 || moveN >= N || moveM >= M) continue;
if (map[moveN][moveM] != map[n][m] || visited[moveN][moveM]) continue;
score += getSquare(moveN, moveM, visited);
}
return score;
}
boolean isHigherThan(int diceNumber, int n, int m) {
return diceNumber > map[n][m];
}
boolean isLowerThan(int diceNumber, int n, int m) {
return diceNumber < map[n][m];
}
boolean isPositionOutOfBounds(int n, int m) {
return n < 0 || m < 0 || n >= N || m >= M;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[][] arr = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
ScoreMap scoreMap = new ScoreMap(arr);
Dice dice = new Dice();
int score = 0;
while (K-- > 0) {
dice.move();
score += scoreMap.getScore(dice.n, dice.m);
dice.decideDirection(scoreMap);
}
System.out.println(score);
}
}
'백준' 카테고리의 다른 글
[백준] 18808 - 스티커 붙이기 (JAVA 객체지향적으로 설계해 보기) (2) | 2023.11.20 |
---|---|
[백준] 15562 - 톱니바퀴2 (JAVA 객체지향적으로 설계해 보기) (0) | 2023.11.17 |