2024. 7. 25. 00:07γCoding Test/BOJ
πλ¬Έμ 보λ¬κ°κΈ°
π§π»π»νμ΄ κ³Όμ
빑ꡬν λ¬Έμ λ μꡬμ¬ν λ³λ‘ λ©μλλ₯Ό μ λλ ꡬννλ κ² μ€μνλ€κ³ μκ°νλλ°, μ΄λ κ² ν΄λ μ€κ°μ λ‘μ§μ΄ μλͺ»λλ©΄ λλ²κΉ νκΈ°κ° μ λ§ μ΄λ ΅λ€μ. μ λ νμ΄μ΄λ³Όμ΄ κ°μ μμΉμ μλμ§ νμ νκΈ° μν 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();
}
}
'Coding Test > 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 |