๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐ง๐ป๐ปํ์ด ๊ณผ์
๋นก๊ตฌํ ๋ฌธ์ ๋ ์๊ตฌ์ฌํญ ๋ณ๋ก ๋ฉ์๋๋ฅผ ์ ๋๋ ๊ตฌํํ๋ ๊ฒ ์ค์ํ๋ค๊ณ ์๊ฐํ๋๋ฐ, ์ด๋ ๊ฒ ํด๋ ์ค๊ฐ์ ๋ก์ง์ด ์๋ชป๋๋ฉด ๋๋ฒ๊น ํ๊ธฐ๊ฐ ์ ๋ง ์ด๋ ต๋ค์. ์ ๋ ํ์ด์ด๋ณผ์ด ๊ฐ์ ์์น์ ์๋์ง ํ์ ํ๊ธฐ ์ํ 2์ฐจ์ Queue ๋ฐฐ์ด, ํ์ฌ ๋งต์ ์กด์ฌํ๋ ํ์ด์ด๋ณผ๋ค์ ๊ด๋ฆฌํ๋ Queue ์ด๋ ๊ฒ 2๊ฐ๋ฅผ ์จ์ ํ์์ต๋๋ค. ํ์์๋ ํ์ด์ด๋ณผ ํด๋์ค ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๊ตฌ์.
class FireBall {
FireBall(int row, int column, int mass, int velocity, int direction) {
this.row = row;
this.column = column;
this.mass = mass;
this.velocity = velocity;
this.direction = direction;
}
int row, column, mass, velocity, direction;
}
BufferedReader๋ฅผ ํตํด ์ ๋ ฅ๊ฐ์ ๋ฐ์์ฃผ๊ณ , 2์ฐจ์ ํ ๋ฐฐ์ด๊ณผ ๊ด๋ฆฌ ํ๋ฅผ ์์ฑํด์ค๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ, ์ด, ์ง๋, ์๋, ๋ฐฉํฅ ์ ๋ณด๊ฐ์ ์ ๋ ฅ ๋ฐ์ ๊ด๋ฆฌ ํ์๋ค๊ฐ ๋ฃ์ด์ฃผ์์ฃ .
private static void initialize() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] args = br.readLine().split(" ");
N = Integer.parseInt(args[0]);
M = Integer.parseInt(args[1]);
K = Integer.parseInt(args[2]);
Field = new LinkedList[N][N];
FireBalls = new LinkedList<>();
// 1. 2์ฐจ์ ๋ฐฐ์ด ๊ฐ ์์๋ง๋ค ํ(Queue) ์์ฑ
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
Field[r][c] = new LinkedList<FireBall>();
}
}
// 2. ํ์ฌ ์กด์ฌํ๋ ํ์ด์ด๋ณผ ์ ๋ณด๋ฅผ ๋ด๋ FireBalls ํ์ ์ด๊ธฐ๊ฐ ๋ฃ๊ธฐ
for (int i = 0; i < M; i++) {
args = br.readLine().split(" ");
int row = Integer.parseInt(args[0]) - 1;
int column = Integer.parseInt(args[1]) - 1;
int mass = Integer.parseInt(args[2]);
int velocity = Integer.parseInt(args[3]);
int direction = Integer.parseInt(args[4]);
FireBalls.add(new FireBall(row, column, mass, velocity, direction));
}
br.close();
}
์ ๋ ฅ๊ฐ ๋ฐ๊ธฐ๋ฅผ ์๋ฃํ๋ค๋ฉด ํ์ด์ด๋ณผ์ ์ด๋์ํค๋ moveFireBalls()์ ๋ชจ๋ ์ด๋ํ ํ์ด์ด๋ณผ๋ค์ ์ฒ๋ฆฌํ๋ processFireBalls()๋ฅผ K๋ฒ ๋ฐ๋ณตํ ๊ฒ๋๋ค.
moveFireBalls()
ํ์ฌ ์กด์ฌํ๋ ํ์ด์ด๋ณผ์ ๊ฐ์(FireBalls.size())๋งํผ for ๋ฌธ์ ๋์ ๋ค์ ์์น๋ก ์ด๋์์ผ ์ค ๊ฒ๋๋ค. ์ด๋, ๋ฌธ์ ์์ ์ฃผ์ด์ง ๋งต์ ๋ฌดํ ๋งต์ด๋ฏ๋ก ๋งต ๋ฐ์ ๋์ด๊ฐ๋ ๋ค์ ๋ฐ๋ํธ์ผ๋ก ๋์์์ผ ํฉ๋๋ค. ์ด๋ฅผ ์ ์ฉํด์ฃผ๊ธฐ ์ํด ๋ชจ๋๋ฌ ์ฐ์ฐ(%)์ ์ ์ฉํ์ผ๋ฉฐ, ์์์ผ ๊ฒฝ์ฐ์๋ ๋ชจ๋๋ฌ ์ฐ์ฐ์ ์ ์ฉํด๋ ๊ทธ๋๋ก ์์์ธ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฏ๋ก N๋งํผ ์ถ๊ฐ๋ก ๋ํด์ฃผ์์ต๋๋ค.
private static void moveFireBalls() {
int size = FireBalls.size();
for (int i = 0; i < size; i++) {
FireBall fireBall = FireBalls.poll();
int nextRow = (fireBall.row + RowDirections[fireBall.direction] * fireBall.velocity) % N;
int nextColumn = (fireBall.column + ColumnDirections[fireBall.direction] * fireBall.velocity) % N;
if (nextRow < 0) nextRow += N;
if (nextColumn < 0) nextColumn += N;
fireBall.row = nextRow;
fireBall.column = nextColumn;
Field[nextRow][nextColumn].add(fireBall);
}
}
processFireBalls()
์ ์ฒด ๋งต์ ์ํํ๋ฉด์ ํ์ด์ด๋ณผ์ ๊ฐ์์ ๋ฐ๋ผ ๋ค๋ฅธ ์ฐ์ฐ์ ์ ์ฉํฉ๋๋ค.
- ํ์ด์ด๋ณผ์ ๊ฐ์๊ฐ 1๊ฐ์ธ ๊ฒฝ์ฐ : ๋งต์ ํ์ฌ ์์น์์ ํ์ด์ด๋ณผ์ ๋นผ๋ด์ด ๊ด๋ฆฌ ํ(FireBalls)์ ๋ฃ์ต๋๋ค.
- ํ์ด์ด๋ณผ์ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ธ ๊ฒฝ์ฐ : ํ์ด์ด๋ณผ์ ํ๋๋ก ํฉ์ณ 4๊ฐ๋ก ๋ถ๋ฆฌํ๋ ๊ณผ์ ์งํ → splitFireBalls()
private static void processFireBalls() {
for (int row = 0; row < N; row++) {
for (int column = 0; column < N; column++) {
// ๊ฐ์ ๊ณณ์ ํ์ด์ด๋ณผ ๊ฐ์๊ฐ 1๊ฐ์ธ ๊ฒฝ์ฐ -> ๊ด๋ฆฌ ํ์ ๋ฃ๊ธฐ
if (Field[row][column].size() == 1) {
FireBalls.add(Field[row][column].poll());
}
// ๊ฐ์ ๊ณณ์ ํ์ด์ด๋ณผ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ธ ๊ฒฝ์ฐ -> 4๊ฐ๋ก ๋ถ๋ฆฌ
else if (Field[row][column].size() > 1) {
splitFireBalls(row, column);
}
}
}
}
splitFireBalls() ๋ฉ์๋๋ ๊ฐ์ ๊ณณ์ ์๋ 2๊ฐ ์ด์์ ํ์ด์ด๋ณผ๋ค์ด ํ๋๋ก ํฉ์ณ์ ธ 4๊ฐ๋ก ๋ถ๋ฆฌ๋๋ ์๊ตฌ์ฌํญ์ ๊ทธ๋๋ก ๊ตฌํํ ๊ฒ์ ๋๋ค.
private static void splitFireBalls(int row, int column) {
int sumMass = 0, sumVelocity = 0, size = Field[row][column].size();
boolean isAllEven = true, isAllOdd = true;
while (!Field[row][column].isEmpty()) {
FireBall fireBall = Field[row][column].poll();
sumMass += fireBall.mass;
sumVelocity += fireBall.velocity;
if (fireBall.direction % 2 == 0) isAllOdd = false;
else isAllEven = false;
}
int newMass = sumMass / 5;
int newVelocity = sumVelocity / size;
if (newMass > 0) {
for (int i = 0; i < 4; i++) {
int newDirection = (isAllEven || isAllOdd) ? i * 2 : i * 2 + 1;
FireBalls.add(new FireBall(row, column, newMass, newVelocity, newDirection));
}
}
}
์ด๋ฌํ ๊ณผ์ ์ K๋ฒ ๋ฐ๋ณตํ ํ, ํ์ฌ ์กด์ฌํ๋ ํ์ด์ด๋ณผ๋ค์ ์ง๋ ํฉ์ ์ถ๋ ฅํด์ฃผ๋ฉด ์ ๋ต์ ๋๋ค.
private static int getFireBallMass() {
int result = 0;
for (FireBall fireBall : FireBalls) {
result += fireBall.mass;
}
return result;
}
์ ์ฒด ์์ค์ฝ๋๋ ์๋์ ์์ต๋๋ค.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Queue;
import java.util.LinkedList;
class FireBall {
FireBall(int row, int column, int mass, int velocity, int direction) {
this.row = row;
this.column = column;
this.mass = mass;
this.velocity = velocity;
this.direction = direction;
}
int row, column, mass, velocity, direction;
}
public class BOJ20056 {
private static int N, M, K;
private static int[] RowDirections = { -1, -1, 0, 1, 1, 1, 0, -1 };
private static int[] ColumnDirections = { 0, 1, 1, 1, 0, -1, -1, -1 };
private static Queue<FireBall>[][] Field;
private static Queue<FireBall> FireBalls;
public static void main(String[] args) throws Exception {
initialize();
for (int i = 0; i < K; i++) {
moveFireBalls();
processFireBalls();
}
int answer = getFireBallMass();
System.out.println(answer);
}
private static void moveFireBalls() {
int size = FireBalls.size();
for (int i = 0; i < size; i++) {
FireBall fireBall = FireBalls.poll();
int nextRow = (fireBall.row + RowDirections[fireBall.direction] * fireBall.velocity) % N;
int nextColumn = (fireBall.column + ColumnDirections[fireBall.direction] * fireBall.velocity) % N;
if (nextRow < 0) nextRow += N;
if (nextColumn < 0) nextColumn += N;
fireBall.row = nextRow;
fireBall.column = nextColumn;
Field[nextRow][nextColumn].add(fireBall);
}
}
private static void processFireBalls() {
for (int row = 0; row < N; row++) {
for (int column = 0; column < N; column++) {
// ๊ฐ์ ๊ณณ์ ํ์ด์ด๋ณผ ๊ฐ์๊ฐ 2๊ฐ ๋ฏธ๋ง์ธ ๊ฒฝ์ฐ -> ํ์ฌ ์กด์ฌํ๋ ํ์ด์ด๋ณผ์ ๊ด๋ฆฌํ๋ ํ(FireBalls)์ ๋ฃ์ด์ฃผ๊ธฐ
if (Field[row][column].size() == 1) {
FireBalls.add(Field[row][column].poll());
}
// ๊ฐ์ ๊ณณ์ ํ์ด์ด๋ณผ ๊ฐ์๊ฐ 2๊ฐ ์ด์์ธ ๊ฒฝ์ฐ -> 4๊ฐ๋ก ๋ถ๋ฆฌ
else if (Field[row][column].size() > 1) {
splitFireBalls(row, column);
}
}
}
}
private static void splitFireBalls(int row, int column) {
int sumMass = 0, sumVelocity = 0, size = Field[row][column].size();
boolean isAllEven = true, isAllOdd = true;
while (!Field[row][column].isEmpty()) {
FireBall fireBall = Field[row][column].poll();
sumMass += fireBall.mass;
sumVelocity += fireBall.velocity;
if (fireBall.direction % 2 == 0) isAllOdd = false;
else isAllEven = false;
}
int newMass = sumMass / 5;
int newVelocity = sumVelocity / size;
if (newMass > 0) {
for (int i = 0; i < 4; i++) {
int newDirection = (isAllEven || isAllOdd) ? i * 2 : i * 2 + 1;
FireBalls.add(new FireBall(row, column, newMass, newVelocity, newDirection));
}
}
}
private static int getFireBallMass() {
int result = 0;
for (FireBall fireBall : FireBalls) {
result += fireBall.mass;
}
return result;
}
private static void initialize() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] args = br.readLine().split(" ");
N = Integer.parseInt(args[0]);
M = Integer.parseInt(args[1]);
K = Integer.parseInt(args[2]);
Field = new LinkedList[N][N];
FireBalls = new LinkedList<>();
// 1. 2์ฐจ์ ๋ฐฐ์ด ๊ฐ ์์๋ง๋ค ํ(Queue) ์์ฑ
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
Field[r][c] = new LinkedList<FireBall>();
}
}
// 2. ํ์ฌ ์กด์ฌํ๋ ํ์ด์ด๋ณผ ์ ๋ณด๋ฅผ ๋ด๋ FireBalls ํ์ ์ด๊ธฐ๊ฐ ๋ฃ๊ธฐ
for (int i = 0; i < M; i++) {
args = br.readLine().split(" ");
int row = Integer.parseInt(args[0]) - 1;
int column = Integer.parseInt(args[1]) - 1;
int mass = Integer.parseInt(args[2]);
int velocity = Integer.parseInt(args[3]);
int direction = Integer.parseInt(args[4]);
FireBalls.add(new FireBall(row, column, mass, velocity, direction));
}
br.close();
}
}
'๐คAlgorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 8938๋ฒ | ์ฌ๋ฅ๊พผ (Java) (0) | 2024.08.01 |
---|---|
[BOJ] 2477๋ฒ | ์ฐธ์ธ๋ฐญ (Java) (0) | 2024.07.30 |
[BOJ] 12904๋ฒ | A์ B (C++) (0) | 2024.03.28 |
[BOJ] 14891๋ฒ | ํฑ๋๋ฐํด (C++) (0) | 2024.03.24 |
[BOJ] 2636๋ฒ | ์น์ฆ (C++) (1) | 2024.03.22 |