문제 링크 : https://www.acmicpc.net/problem/17144
문제 접근
1. T번 동안 먼지를 확산시키고 공기청정기를 작동시키는걸 반복 한 후 남은 먼지의 양을 구해서 출력해주었다.
소스 코드
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import java.util.Scanner; public class Main { static int r, c, t, upY=-1, downY; static int map[][]; static int dy[] = { 0, 0, 1, -1 }; static int dx[] = { 1, -1, 0, 0 }; public static void cycle() { // 위쪽꺼 for(int i=upY-1; i>0; i--) { map[i][0] = map[i-1][0]; } for(int i=0; i<c-1; i++) { map[0][i] = map[0][i+1]; } for(int i=0; i<upY; i++) { map[i][c-1] = map[i+1][c-1]; } for(int i=c-1; i>1; i--) { map[upY][i] = map[upY][i-1]; } // 한칸 채우기 map[upY][1] = 0; //아래쪽꺼 for(int i = downY+1; i<r-1; i++) { map[i][0] = map[i+1][0]; } for(int i=0; i<c-1; i++) { map[r-1][i] = map[r-1][i+1]; } for(int i=r-1; i>downY; i--) { map[i][c-1] = map[i-1][c-1]; } for(int i=c-1; i>1; i--) { map[downY][i] = map[downY][i-1]; } map[downY][1] = 0; } public static void diffusion() { int tmpmap[][] = new int[r][c]; tmpmap[upY][0] = -1; tmpmap[downY][0] = -1; for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(map[i][j]<=0) continue; int difcnt = 0; int difsu = map[i][j] / 5; for(int d=0; d<4; d++) { int ny = i + dy[d]; int nx = j + dx[d]; if(ny<0 || nx<0 || ny>=r || nx>=c) continue; if(map[ny][nx] == -1) continue; tmpmap[ny][nx] += difsu; difcnt++; } tmpmap[i][j] += map[i][j] - (difcnt*difsu); } } for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { map[i][j] = tmpmap[i][j]; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); r = sc.nextInt(); c = sc.nextInt(); t = sc.nextInt(); map = new int[r][c]; for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { int tmpInt = sc.nextInt(); map[i][j] = tmpInt; if(tmpInt == -1) { if(upY == -1) { upY = i; } else { downY = i; } } } } for(int i=0; i<t; i++) { diffusion(); cycle(); } int sum = 0; for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(map[i][j]<=0) continue; sum += map[i][j]; } } System.out.println(sum); } } | cs |
'Algorithm > 백준' 카테고리의 다른 글
백준 2798번 블랙잭 (0) | 2021.08.22 |
---|---|
백준 16235번 나무재테크 (0) | 2019.05.03 |
백준 15685번 드래곤 커브 (0) | 2019.05.02 |
백준 14891번 톱니바퀴 (0) | 2019.04.30 |
백준 15683번 감시 (0) | 2019.04.30 |