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
- 자바
- 프로그래머스
- 시뮬레이션
- 문제풀이
- DP
- oracle
- priorityqueue
- 코딩테스트
- 알고리즘
- BFS
- SQL
- dfs
- 이분탐색
- 문자열
- 백준
- SWEA
- MySQL
- COS PRO
- greedy
- BOJ
- 러닝일지
- 운동기록
- math
- db
- 건강
- 문제해결
- binary search
- 투포인터
- 코테
- Java
Archives
- Today
- Total
슈콩
[BOJ] 17144 미세먼지 안녕! 본문





[문제]
https://www.acmicpc.net/problem/17144
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int r,c;
static int[][] map,add;
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws Exception {
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());
map = new int[r+1][c+1];
boolean check = false;
int startR=0, startC=0, endR=0, endC=0;
for(int i=1;i<=r;i++) {
st = new StringTokenizer(br.readLine());
for(int j=1;j<=c;j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if(!check && map[i][j]==-1) {
startR = i; startC = j;
endR = i+1; endC = j;
check = true;
}
}
}
while(t-->0) {
spread();
blow(startR,startC,endR,endC);
}
int result = 0;
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
if(map[i][j]>0) result += map[i][j];
}
}
System.out.println(result);
}
private static void spread() {
add = new int[r+1][c+1];
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
if(map[i][j]>0) {
int cnt = 0;
for(int d=0;d<4;d++) {
int nr = i + dr[d];
int nc = j + dc[d];
if(nr<=0 || nr>r || nc<=0 || nc>c || map[nr][nc]==-1) continue;
add[nr][nc] += map[i][j] / 5;
cnt++;
}
add[i][j] -= (map[i][j] / 5 * cnt);
}
}
}
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
map[i][j] += add[i][j];
}
}
}
private static void blow(int startR,int startC,int endR,int endC) {
for(int i=startR-1;i>1;i--) {
map[i][startC] = map[i-1][startC];
}
for(int j=1;j<c;j++) {
map[1][j] = map[1][j+1];
}
for(int i=1;i<startR;i++) {
map[i][c] = map[i+1][c];
}
for(int j=c;j>2;j--) {
map[startR][j] = map[startR][j-1];
}
map[startR][2] = 0;
for(int i=endR+1;i<r;i++) {
map[i][endC] = map[i+1][endC];
}
for(int j=1;j<c;j++) {
map[r][j] = map[r][j+1];
}
for(int i=r;i>endR;i--) {
map[i][c] = map[i-1][c];
}
for(int j=c;j>2;j--) {
map[endR][j] = map[endR][j-1];
}
map[endR][2] = 0;
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 17140 이차원 배열과 연산 (0) | 2025.12.07 |
|---|---|
| [BOJ] 17143 낚시왕 (0) | 2025.12.06 |
| [BOJ] 16236 아기 상어 (0) | 2025.12.06 |
| [BOJ] 16235 나무 재테크 (0) | 2025.12.05 |
| [BOJ] 16234 인구 이동 (0) | 2025.12.04 |