Tags
- ์ํ
- ๊ตฌํ
- Dynamic Programming
- Java
- greedy
- ์ ๋ ฌ
- SpringBoot
- Brute Force Algorithm
- Study
- ๊ต์ฌ
- ๋ฐฑํธ๋ํน
- ๊น์ด ์ฐ์ ํ์
- ์๋ฎฌ๋ ์ด์
- ๊ทธ๋ํ ์ด๋ก
- BOJ
- stack
- Python
- LV2
- DP
- ๋๋น ์ฐ์ ํ์
- ์ ์๋ก
- dfs
- ๊ทธ๋ํ ํ์
- BFS
- ์๋ฃ๊ตฌ์กฐ
- sort
- CodingTest
- queue
- PGM
- ๋ฌธ์์ด
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_17144 : ๋ฏธ์ธ๋จผ์ง ์๋ ! ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- ๊ณต๊ธฐ์ฒญ์ ๊ธฐ๋ก ์ธํ ์์ชฝ ๊ณต๊ธฐ ์ํ๊ณผ ์๋์ชฝ ๊ณต๊ธฐ ์ํ์ด ์ผ์ด๋๋ค.
- ๋จผ์ง๋ ๋งค ์ด ํ์ฐํ๋ค.
- T์ด ํ ๋ฐฉ์ ๋จ์์๋ ๋ฏธ์ธ๋จผ์ง์ ํฉ์ ์ถ๋ ฅํ๋ค.
๐ธ ์ฝ๋ ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int R, C, top, bottom;
private static int[][] room;
private static int[] dx = {-1, 0, 1, 0}; // ์ ์ข ํ ์ฐ
private static int[] dy = {0, 1, 0, -1};
private static void dust() { // ๋จผ์ง ํ์ฐ
int[][] temp = new int[R][C]; // ๋จผ์ง ๋์
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (room[i][j] > 0) { // ํ์ฐํ ๋จผ์ง๊ฐ ์๋ค๋ฉด
int cnt = 0; // ํ์ฐ ์
for (int k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
// ๋ฐฉ ๋ฒ์ ์์ ์์ผ๋ฉฐ, ๊ณต๊ธฐ์ฒญ์ ๊ธฐ์ ์๊ฒน์น๋ฉด ๋จผ์ง ํ์ฐ
if (0 <= x && x < R && 0 <= y && y < C && !(y==0&&(x==top||x==bottom))) {
cnt++;
temp[x][y] += room[i][j]/5;
}
}
temp[i][j] += room[i][j] - (room[i][j]/5)*cnt;
}
}
}
room = temp;
}
private static void top_wind() { // ๋ฐ์๊ณ ๋ฐ๋ ์ํ
if (top > 0) {
for (int i = top-2; i >= 0; i--) {
room[i+1][0] = room[i][0];
}
for (int i = 1; i < C; i++) {
room[0][i-1] = room[0][i];
}
for (int i = 1; i <= top; i++) {
room[i-1][C-1] = room[i][C-1];
}
for (int i = C-2; i > 0; i--) {
room[top][i+1] = room[top][i];
}
room[top][1] = 0;
}
}
private static void bottom_wind() { // ์๊ณ ๋ฐ๋ ์ํ
if (bottom < R) {
for (int i = bottom+2; i < R; i++) {
room[i-1][0] = room[i][0];
}
for (int i = 1; i < C; i++) {
room[R-1][i-1] = room[R-1][i];
}
for (int i = R-2; i >= bottom; i--) {
room[i+1][C-1] = room[i][C-1];
}
for (int i = C-2; i > 0; i--) {
room[bottom][i+1] = room[bottom][i];
}
room[bottom][1] = 0;
}
}
private static int sum_Dust() {
int sum = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (room[i][j] > 0) {
sum += room[i][j];
}
}
}
return sum;
}
private static void print() {
System.out.println("============================");
room[top][0] = -1;
room[bottom][0] = -1;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
System.out.print(room[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
// Input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
int T = Integer.parseInt(st.nextToken());
room = new int[R][C];
top = -1; bottom = -1;
for (int i = 0; i < R; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < C; j++) {
room[i][j] = Integer.parseInt(st.nextToken());
if (room[i][j] == -1) {
if (top == -1) top = i;
else bottom = i;
}
}
}
// ์๊ฐ ์งํ
for (int t = 0; t < T; t++) {
// print();
dust(); // ๋จผ์ง ํ์ฐ
top_wind(); // ๋ฐ์๊ณ ๋ฐ๋ ์ํ
bottom_wind(); // ์๊ณ ๋ฐ๋ ์ํ
// print();
}
// Output
System.out.println(sum_Dust());
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- T์ด ๋์ ๊ณ์ฐ์ ๋ฐ๋ณตํ๋ค.
- ๋จผ์ง๊ฐ ํ์ฐ๋๋ค
- ์ธ์ 4์นธ์ผ๋ก ํ์ฐ๋๋๋ฐ, temp ๋ฐฐ์ด์ ํ์ฐ๋ ๋จผ์ง๋ฅผ ๋์ ํ๋ค.
- ์๋ณธ ๋ฐฉ ์ํ์ธ room ๋ฐฐ์ด์ ๊ฐ์ temp๋ฐฐ์ด์ ๊ฐ์ผ๋ก ๋ณ๊ฒฝํ๋ค.
- ์์ชฝ์ ๋ฐ์๊ณ, ์๋์ชฝ์ ์๊ณ ๋ฐฉํฅ์ผ๋ก ๋ฐ๋์ด ์ํํ๋ค.
- ๊ณต๊ธฐ์ฒญ์ ๊ธฐ ๋์ด์ ๋ฐ๋ผ์ ์ํ ์ฌ๊ฐํ์ ํฌ๊ธฐ๊ฐ ๋ฌ๋ผ์ง๋ค.
- ๋จผ์ง๊ฐ ํ์ฐ๋๋ค
- ๋จ์ ๋จผ์ง ์๋ฅผ ์ถ๋ ฅํ๋ค.
๐ธ end ๐ธ
- ๋ฐ๋ ์ํ ๋ถ๋ถ์ด ๊ตฌํํ๊ธฐ์ ํท๊น๋ฆด ๋ถ๋ถ์ด ๋ง์ ๊ฒ ๊ฐ์์, ๋ฐ๋ก ๋ฉ์๋๋ฅผ ๋นผ์ ๊ตฌํํ๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_17070 : ํ์ดํ ์ฎ๊ธฐ๊ธฐ 1 (0) | 2023.04.07 |
---|---|
BOJ_14889 : ์คํํธ์ ๋งํฌ (0) | 2023.04.07 |
BOJ_1068 : ํธ๋ฆฌ (0) | 2023.04.07 |
BOJ_13460 : ๊ตฌ์ฌ ํ์ถ 2 (0) | 2023.04.07 |
BOJ_16236 : ์๊ธฐ ์์ด (0) | 2023.04.07 |