알고리즘 문제/BOJ
16918번 봄버맨
parkit
2019. 3. 15. 00:21
728x90
반응형
https://www.acmicpc.net/problem/16918
2가지 풀이 방법이 있다.
1. 구조체에 bool 활용
2. vector 활용
1. 구조체에 bool 활용
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 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; typedef struct info { char c; int time; bool bomb; }info; int R, C, N; int dy[5] = { 0, 0, 1, 0, -1 }; int dx[5] = { 0, 1, 0, -1, 0 }; info Map[202][202]; info temp; void printMap() { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { printf("%c", Map[i][j]); } printf("\n"); } } void explosion() { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { if (Map[i][j].bomb) { Map[i][j].c = '.'; Map[i][j].time = 0; Map[i][j].bomb = false; } } } } void simulation() { while (N--) { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { if (Map[i][j].c == '.') { Map[i][j].c = 'O'; Map[i][j].time = 0; } else if (Map[i][j].c == 'O') { ++Map[i][j].time; if (Map[i][j].time == 3) { for (int d = 0; d < 5; d++) { int y = i + dy[d]; int x = j + dx[d]; if (y < 0 || y >= R || x < 0 || x >= C) continue; /* Map[y][x].time = 0;을 여기에 쓰면 안 된다. 원래 폭발해야할 것이 이중 for문 순서에 78번 째 줄에서 1이 되어버리고, 그래서 80번 째 if 조건문에 걸리지 않는다. */ Map[y][x].bomb = true; } } } } } explosion(); } } int main(void) { char c; scanf("%d %d %d", &R, &C, &N); --N; for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { cin >> c; temp.c = c; if (c == 'O') temp.time = 1; else temp.time = 0; temp.bomb = false; Map[i][j] = temp; } } simulation(); printMap(); return 0; } | cs |
2. vector 활용
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 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; typedef struct info { char c; int time; }info; int R, C, N; int dy[5] = { 0, 0, 1, 0, -1 }; int dx[5] = { 0, 1, 0, -1, 0 }; info Map[202][202]; info temp; vector<pair<int, int> > v; void printMap() { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { printf("%c", Map[i][j]); } printf("\n"); } } void explosion() { for (int i = 0; i < v.size(); i++) { Map[v.at(i).first][v.at(i).second].time = 0; Map[v.at(i).first][v.at(i).second].c = '.'; } } void simulation() { while (N--) { for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { if (Map[i][j].c == '.') { Map[i][j].c = 'O'; Map[i][j].time = 0; } else if (Map[i][j].c == 'O') { ++Map[i][j].time; if (Map[i][j].time == 3) { for (int d = 0; d < 5; d++) { int y = i + dy[d]; int x = j + dx[d]; if (y < 0 || y >= R || x < 0 || x >= C) continue; v.push_back({ y, x }); } } } } } explosion(); v.clear(); } } int main(void) { char c; scanf("%d %d %d", &R, &C, &N); --N; for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { cin >> c; temp.c = c; if (c == 'O') temp.time = 1; else temp.time = 0; Map[i][j] = temp; } } simulation(); printMap(); return 0; } | cs |
728x90
반응형