알고리즘/백준
[C++] 백준 16981 봄버맨
이영재의오른발
2023. 1. 22. 20:47
반응형
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <iostream> #include <string> using namespace std; int r,c,n; int time = 0; string now[201]; bool check[201][201]; bool rem_check[201][201]; int dx[] = {0,0,0,-1,1}; int dy[] = {0,-1,1,0,0}; void input() { cin >> r >> c >> n; for(int i=0; i<r; i++) { cin >> now[i]; } for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(now[i][j] == 'O') { check[i][j] = true; } } } } void fillAll() { for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { now[i][j] = 'O'; } } } void bombNow() { for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(check[i][j]) { for(int k=0; k<5; k++) { int nx = dx[k] + i; int ny = dy[k] + j; if(nx < 0 || ny < 0 || nx >= r || ny >= c) { continue; } rem_check[nx][ny] = true; } } } } } void initCheck() { for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { check[i][j] = false; } } } void initNow() { for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { now[i][j] = '.'; } } } void initRemCheck() { for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { rem_check[i][j] = false; } } } void copyNow() { initNow(); for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(check[i][j]) { now[i][j] = 'O'; } } } } void checkNextBomb() { initCheck(); for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(!rem_check[i][j]) { check[i][j] = true; } } } initRemCheck(); } void printNow() { for(int i=0; i<r; i++) { cout << now[i] << "\n"; } cout << '\n'; } void solve() { time++; if(time == n) { for(int i=0; i<r; i++) { cout << now[i] << "\n"; } return; } while(1) { time++; for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(now[i][j] == 'O') { check[i][j] = true; } } } fillAll(); if(time == n) { break; } time++; bombNow(); checkNextBomb(); copyNow(); if(time == n) { break; } } for(int i=0; i<r; i++) { cout << now[i] << "\n"; } } int main() { input(); solve(); } | cs |
문제에서 제시하는대로 따라가면 되는데 생각할 것들이 꽤 많은 문제
배열 두개를 선언하지 않으면 순차적으로 폭탄을 터뜨릴때 설치된 폭탄이 못 터지는 경우가 생길 수 있으니 배열 두개를 이용하는 것이 포인트!
728x90
반응형