C++の練習を兼ねて, AtCoder Grand Contest 014 の 問題C (Closed Rooms) を解いてみた.
■感想.
1. 問題Cは, 方針が見えなかったので, 解説を参考に実装して, AC版に到達出来た.
2. 幅優先探索の復習が出来たので, 非常に良かったと思う.
3. 引き続き, 時間を見つけて, 過去問の学習を進めていきたいと思う.
本家のサイト AtCoder Grand Contest 014 解説 を ご覧下さい.
■C++版プログラム(問題C/AC版).
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 |
// 解き直し. // https://img.atcoder.jp/agc014/editorial.pdf // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using vs = vector<string>; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) #define pb push_back constexpr int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int MAX = 808 * 808; int main(){ // 1. 入力情報. int H, W, K, sh = -1, sw = -1; scanf("%d %d %d", &H, &W, &K); vs G; rep(i, H){ char c[808]; scanf("%s", c); string s(c); rep(j, W){ if(s[j] == 'S'){ sh = i; sw = j; s[j] = '.'; } } G.pb(s); } // 2. bfs. // https://ja.wikipedia.org/wiki/幅優先探索 auto bfs = [&](vs &G, int s, int* d) -> void { // 空のキュー. queue<int> q; // 探索地点 s をキュー q に追加. q.push(s); while(!q.empty()){ // キューから取り出す. int v = q.front(); q.pop(); // 取り出した要素を処理. // x: 列方向, y: 行方向 で考える. int nx, ny, n; int cx = v % W , cy = v / W; rep(i, 4){ nx = cx + dx[i]; ny = cy + dy[i]; n = nx + ny * W; // 訪問不可能なマス であれば, 処理をスキップ. if(s == n || nx < 0 || nx >= W || ny < 0 || ny >= H) continue; // 訪問先のマスで, 訪問可能 かつ 未訪問 であれば, 最短距離 を 設定. if(G[ny][nx] == '.' && !d[n]) d[n] = d[v] + 1, q.push(n); } } }; // 3. 今いる部屋から, 各部屋までの距離を計算. int d[MAX], s = sw + sh * W; rep(i, MAX) d[i] = 0; bfs(G, s, d); // 4. 最小回数は? int magic = 2020202020; rep(i, H * W){ // 閉じられている部屋は, Skip. if(d[i] > K) continue; // 今いる部屋以外で, 距離 0 の 部屋を Skip. if(!d[i] && (i != s)) continue; // 到達先. int t = 2020202020, cx = i % W , cy = i / W; // 1行目, 1列目, H行目, W列目. t = min({t, cy, cx, H - 1 - cy, W - 1 - cx}); // 最小値更新. magic = min(magic, 1 + (t + K - 1) / K); } // 5. 出力. printf("%d\n", magic); return 0; } |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
[入力例] 3 3 3 #.# #S. ### [出力例] 1 ※AtCoderテストケースより [入力例] 3 3 3 ### #S# ### [出力例] 2 ※AtCoderテストケースより [入力例] 7 7 2 ####### ####### ##...## ###S### ##.#.## ###.### ####### [出力例] 2 ※AtCoderテストケースより [入力例] 20 22 5 ###################### ###################### ###################### ###################### ###########.########## ##########.#.######### #########.###.######## ########.#####.####### #######.#.....#.###### ######.##.#.#.##.##### #####.###..S..###.#### ######.##.#.#.##.##### #######.#.....#.###### ########.#####.####### #########.###.######## ##########.#.######### ###########.########## ###################### ###################### ###################### [出力例] 3 [入力例] 55 50 5 .#..####.#.#.####.#.#..######..##...#...#..###..#. ..###.#...#.###.##..###..##.##..##.#.#.####...###. .#######..#.#.####.###...##..###..#.........#....# ###.......#...#.##.#..#.##.#..#.#.#.#.##.##.#.#### ###.##.#####.#.##.##.##.###.######.#....####.....# #####.....####.......#.##.#..#..##.##.#...#.#.##.# ###.#.#..##..#....#..##...##..###..##.##.#.....#.# ..##.##.#..#.###..#......####.##....#..#.##..#...# ...#..####.#######.#..###..#.###.#..#.##.#...#..#. #.###.##...#.###.#..####.#.##.....##...#.#.#..#..# #.##.#########.###..#.##...#..#..#..#.##.####..#.. .##..#.#.....#######.###.#########..#.#.....#.##.# .....##.#....##.###..........#.#..##..###.##.#.##. #######..##.#..#...#..#...##.##.##.#.####.#.#.#... ...#####.#.#...#..##...#.##..##.#...##.#...#.....# .#.#.##...#..##...#..##.##.##..#.###...#....#....# ....#.#.##...##...#.###.##..#..##.#..#..##.##....# ..##.###.#...##.#.#....#.##.####.#####.####..##### #..#..##.###..#...##.#.#......#.#....##.#.###.##.. #..##..#.###.....####.##..#..#.#.##.#.##.###.###.. ##...#....#.###..#.#..#.......###.#.#..##....#..#. .####.##...####..####....#....#.#.#...##.##.#.#..# ..##...##.#..###..######..##.##.##.#...##....###.. .#.#..#.#.###.##.###..#.#.##.##..##..#..#.###..#.# .#.#.#.######.#.#...#.....##.....##.##...#.#.##..# ....#.#..####..###.####...##.#.#..#.#.####..#.###. ##..##...#.......###...#.##.#......#.#.##...##.#.. ##...#.#.#.#..##.......#.#.#.#.#.#...#.##.#....... ...####.##.#.#...#.#####S.########.#......###.#### ....####...#.....#.....#....#.#.#.#..#.#..####..## #.........#.#.##.#.##..#.##.##......#.#.#....#..## #..#..####...###.###.#......#...##.#.....####.#... ##.#.##..#..####...#..####.##..####...##..###....# ..######.##.#####...##..###..#.#........##.#.###.# ....###.#.#..#####..#.####.##.#.##..#.###..##.#### .....#..#.##.#.###..#..###.###.#.#...#.#####.#.... ..#.#..###...##.##.#.#..##......###..###.#.#...#.. #####..##.##..#..#.#......####.##..##..#####.#.#.. ...#.#.###..#.##.#...###...#.#...##...#..#.####... ##.###.#.#.#.....###.#.#.##.##..##..#.#.#.#.#..#.# ##.##.....#....#...#.#...#..##...##..#..##..#...#. ...###...#.###...#.#####..####....#..#.#...#..#.## ##.##...####..#.#####..#.##.#.....#.####.#...#.... #..#.####.#.#.##...#.##....#######..###..####..##. ##.#.###.###..##....####..#...#..#.#.###.###....## .##...#.###.###..##.##...####..##.....#....#.####. #.#...####.#...##.#.###...#...##..#.###.##...#..#. ....#.###.#.#..#..##.####.#..##.#..##....#.#...#.. ..##..#.##..#..#.########.#.#.#.####..#.##...#.##. .##...#...#.#....####.##..##..#...###.#####..##..# ##..###.####..#####.#.#.#...##..##..#....##.....#. ....#..##.##..#..#..####..####..##...###.##.#####. #....##....###..#.####.##....#..#.....#####..##.#. #.#..####....##..#..#.#.#.###.###.#.###.###..##.## #.#.#.#.##.#####..#######..#.#...#.#...###.#..#.## [出力例] 6 [入力例] 110 100 5 ......#.#...###...####..#.#..###..#.##.#.#.##.##.##..##...#..#.###.#.##.#..#.###.###..##.....#...#.. #...##.#.#...####..#.#..#.##.##.###...#....##....##....####.#.###....#.##.##.#.##.##.#...#.#####..## ..#..##.#.##...#..#.#..###.###..#.#.####....#..#.###.###.#.#..#.#...##.#...#...##.#.###..#.#.#....## #...#.###.###.#.####.#..##.#..#...##.####.#.#.###.....#.####..##.....#####.###..#.#.#.......#...###. ...##.####.#.#.#.#########.#......##.###..#..###..#..#....###.##....#.############.###..##..#......# #.##..##...######.##..#...####..##.####..#.#.###.#.###.#####..#..#..####..##...#..######..#.##...#.. #.#.....###..##.####.#.#...#.#.#......####.....##......#..#...##..##..#######..###....#...##.#.###.. #.#.####..#...#.##...####.#.#....#.#.....#..##..###...#.#######..####..##.#....##.#.######....#...#. ##.##...##..#.####...#....#..#..#....#.####.#####.#...##.#...##..##.....#.#.#..#.#..#.#.#.#.##...#.. #.##.#......#.####...#.##.#.#..#...###.#...#.#.##.#.#...##..#.#.#.#.#####..###.####.#...##.#.....#.. ..##.#....##.#.###.###..###.##.####.#.#....#..##.##.#.#######...###########.####.#...#.####.....###. #.#.#####.#.##.###..#.####.#...#..###.#..#.#..#.##.##....#...###.#...#...###.##.#.#.#.###...#.#..#.# .###....##..#...###.#...#.######....#.#.....##.###.####.#.##...###.#.##.##.###.#####..#.......#.###. ..###..#.#.#.##......##....##.##......#.#.#..#...##.....##.#.#..##.##..#############.#.###....#####. #.#.####.###.##.....##.####.#.##....#.#..#.#.##.###...##.###.....###....##.##....###.##.#.#...##.#.. ...##.#.#.........##..#.###.#.#....#...#..#.....#..#.#.....##....##.##..#..#...##.#.###...###.###### ###.#...#..#.#....##.#.##.##.####.###.#....##.#..#..####...##..##..###...#.###....##.......##.#.##.. ..##.########.#.##..##.####..#.#.##..###..###.###..#...##..#...##..#####.....##..#......#.#....###.. ###..##.##.###..#####...###...#..#..#....#.#..##.###.##..##.##.##....#..........##..#######..#...... ##..##.....#.....#..#####.##.#.###...#...#...######.......##...##....##.#..#.#########..##.###.###.. .###.##.##..#.#.#..#..#..####...#....#...##..##..#...#..#....##..#.#.....#....#.##.#.###...##.#....# #..#.#.#.##.......##.#.#.........##..#.............###.###.#.#.####..##.##.####.##.##.##....#.#.##.. #.#...#.....###.#...#.#.##...##.###.#.#####.#..########.......#..#......##...##.###.#..#.##.....#... .#......##..#..#...#...#..###.#.#...###....####..###.#.......#.#.#.....##.#....#######.....#.....#.. #.##..####..#.#.#.#....#.##..##########.##..#.#.#.##.##.#.#.#.#####.##..#####.##....#..###..####.#.# #....###...#.#.##.#.####.##.#..#.#.#.####......###.....#.#.#....#.#.##.....####.####.........##.#..# #.#..#.#....##..#.#.###...#.#.....###.#.#.####.##########....##..#....#...###.###.####.####.#.##..#. .#..#.###...##.#....####.#.##.#.###..##.....##..#...##...##.####..##...#####.#####...##..#.#.#...### ##.#...##....#..#..#..##.#....#.##...#.#.#..##.#..#.###.....##.#.####..##.#.#.#####.....#####.####.. ...#.#.#.#####..#..##...##.#.#...#.##.#..#.#..####.#.....###.#.##..###.#...#...#.....#####.#..##.#.. ##......#.#.#.#..#..######...#####.##..#..#..#.#.#.###......#.####..##....#.#..###......##.##.#..#.# ####.##.#######..#.#....#.#...###.###.#.#....#..#.###...###..#.#..##.##.#.##....###...#...##.#.#...# ..######.#.#...#..##.##.####...#####...####..#####..#.#...#.#...#.###.#.#.####...#..##.#.##.#..#.... #####..#.####.#..#...##.#....##....####.#...#...##.#.#####.####..#.####..##..###..##...###..#.##.#.. #..###.........##.#.#...#..##.##..##...#.##....##...#......#.#..#..#.#.#.##...##.##..#####...###...# ##.##.##...####...#.#####....#.####..#.###...#.###.#.##..##..####.##...#..#.#####..##.#..####...#... ..###.....##.#.#####.###..##.#..##..##.#.###.##.#.#..#..#.#.###..##.###.##.##...######.##.#...##.#.. ...#.#....#.#.#.....####.##..###..####.#.#..#.#..#.##.#.#.#..#...#.#.#.#.##..##......####.######.#.. #.#...#..#######....##.#.#######.#.##.##.....#...#.####..#.##..#..##.#.#.#...#..#.####..#.#.#####..# ##.#.....##...#...#.###...###.####.######.###.##..###..#...##..###...#..###..#..###..###..#..#...#.# .#.##..####.#.#....##.#..#..##...##......####.#####.##...#########..#..##...##.#..##...#...#.#####.. .#.#####.#...#...#..##.#.......####.###.##.#.#....##...###..#...#.#.#.#.#...##.#....#..#.....##..#.. ...##..#####.#..........#..##.#.##.#..###.####.#.##..###.###...#####.##.#....#####.#.##..#.##.#....# #....##.##..#....##..#.#.##.#.#.#..#..###.####.#.###.#.##.####..##..#..#..#..#..#.##...####..#.##.#. #..#..#..#..####.#.#.#.#.##..##...#..#........#...#.#..#.##.......#.##...##..#.#.##....#....#..###.# .#.#.##...###...##....###....##..#.#..#.##.##..####..#.##.#.....#..#####......#...##....#..##.#...## #...#..##......##......#....##..#.#....#..##..#.##..#.##.###.###.##..#..#....##...#.#....##.##..##.# ##...#..##..#..#.#.##.###.##.#..###.##.#####.##.####..#..##.#..#.#.#....#.####.....##.##.######.#.#. ####..#..#.##..#.###.##.###.##.##..#.#.#..#..#####.#..#.#...##..#...#.##.##....##....#.#..#..####.#. ##...#.###..#..###.###...#.#..##.#....###.##..#.#.#S#.#...###.#.###.###.##.#.#.....#.......#....#..# ..##.#.###..###...###.#....#...#...#.##.#.....##.#..##..#.####..##.##..##...#.##.#.#...#....#.#..... ####.###..##.##....##.##...##..###..###..#.#..##..####..#.#.....###.#.##...##..#.########..#.####..# ..#..######.##.###.####..#.#.##.#...##.....#..........##.##.#.....#.###.###..#.....##.#..#...####... #.#.....####..##..#..#..#...###.##..#...#...########.#.###.#.....#####...#...#..##......###.##..#... ###.#.....##..#..##.....##.##.#######...#.....##....##.##....#.##..##.##.#..##.##.###.#.#.#...#.#### ..#.#####.#...#.#.###....#.###..##..##.####.....##.####.##.#..###################.#.#.####.....###.# #..####.##.#..##...#..###..#####.#..#...#.#.#...#...####.#.#.##.##.#...#...##..###.#..#...#.#.#.#### #####.##.#..###..#...#..#.#...#.###.##..#.#.#......#..#.#.#.#...##.###.##.####...###.##.####...#...# #....#.##.#..#..###.#.######...##.###..####.##.#.#.##.#.##.##.###.##...###...##..#.#.#.#....##..#..# .##.....#######...#....#...#####...#.#####.##..#...#..#....##..#####..##.##.#...#..##.###.#####...#. #.#.##..#.#.#.####.###.#.##...#.####.....##..###..#.#..##..#..##.#..#.#######.###.#...##..#####..#.. ..#.....###...#..#.#...#..##...##.#.......#...#...#..#.#.#....#..#....##.#..##.##..#.##..##..####.## ###.##.#.##..##..#..###.##.#.####..#..#.##....#.###.#.#...#..#..#..##...##.##..#.##...####...####..# ..##......#.##.###.#...#.########.....#####.###.###.#.#..#.##.#..##..####.#..#...#..#.#..#.###.##... ...####.......###.###.....##...#.#.####.....#########.#..#..#.####..###......#....#.##.....###..###. .#...#.#..##.###.#.###...###..#.###..####.##.##...#.#.#.....#.#.#..#####....########.#.#.##.#....#.. ###.......##.##.#..####.###.##.#####.#.#.....#..####..##..##.######.#...#..#.#.....#....#...#..##..# .#..#....###.#....###..##...###...###..#.#..#.#.#####.#...#.##..#.#...##.###.####..#.#..#.#..#.#.### ....###....#..#.##...#####........##..##.#...####..##......#.#......#.#.##.###..####.#...##..#...#.. #######...#.........###.####.#.#...###.#.#.###.###.##..#.#....##.###..##...##.##..###..####.#.##.##. .####.#.##.#..#...#.##.#..#.#.##.#...##...###.#.##...#....#####.#.##..#.#.####...#..#.#####.#.####.# ..###.#...#..#######.####.###.#..###.#.#.#####...##.###.#..#..#.....#.###.##.##...##..#.#.##.#.#...# #....#..###.##...##.#.#.###.#..##..#.##.#...##.......#.#..#.#...##.#...#..#.#....#...#...#......##.. .#.....####.#.####..#..#####.####..######.#..#...##..#######.#########.#.##....#.#.....##.####.#..## #....##..###.##..##...##....######..###..#.#.#.#...#.##..###.#..#.######.#####.#.####..#.##..#.##..# ....#..#..###.#.#..##.####.#####.####.######..#####.##.#.#.#...#..##..##.##.#.##.....#.#.#.##.##..#. #.#.##..###.####......#....#.#.#..#.....###.#..##........##.##.########.#.##.###.#..#.##.#.#..#.#### #..###..#.....##...##....###..#.##.#.#..#....###.##..######.....#.#.##.##.#..##...#...#.....###.#### #...#.#..#.#.......#........###.####...#.#.#...#...######...#.####.#...##..###....##.###...###.#...# #....#..###.#.###.##.#..####..#..#######..#.##..##.#.#####.#..##.#.##..##...#.######..###.#..#...##. .#.#..###..#..##..###.....##..#.#.#.#..#.#.####.#..####....#...#..######.###.##..##.#.##.###.##..##. ..##.##..##..#....##..##.........#######.#.#.#..##.#.#....####.#.#.##.#.####.#..###.####..####.#..#. ..#.#..####.#..##..........#..###..####..#.######.#.#.#.####.#####..######.##.#.####....###.##..#..# ##.....#..##.##.###..#.###########.#.#.#..#.####.##....##..##.##...#..#####...#...#######....#....#. ...#.##.....##.#..##...#.....#.#..####....#.#.#...#.###.#.##..###...#...#.##.#..####..###.###...#.## ###.#..#.##...#.##.##...##...##.###.#.#######.#....#.#.#.#.....#..#..#..##..##..#.#..#.#.##.#...##.. .##.#.#.#.#.#.##.##.#.###.....#.#..#.###.#.####...#.##...#..###.#.###..###.....#...#..#.######.#..#. .#...#.#..###.##.#.#..####.#..##.###.##...#.##.##..#...###..#.....#...#.##.#....#..#.####..#.#.##..# #.##.....###.#...##.....#..#.#..#.##.....####.#.##.##...###...#.##.#.#.##.###..#####.###.##...#####. ........###..####....##.##.#.......#..###.##....#..###.#.##.##.#####.#.##.#....###.....##.#.##...### ##..##..##.##.#.#.#.#########.#.##..#.#.##..##..############..#..#.#.##..##.###.#.#####...#####...#. .#..##.#...###..##...###.#####.##..#######.####.#..#.##.###.##.....##.###.....##.####...##.#.#.#.#.# ...#...####.#......##.####..##.#.#.###.#.#.##...#..##.#.##.##....##..#..##.#....#######.#.###..#.#.. ..###.##.###..#.####.##.##....##.##.##.#.##.#####...###...##...##.#.#..#.##..........#..##.#..#....# ###.#..#...####.###.#.######......#...#....##.#.##..#.#..######.##.###.#.#.#....##.##.##...#.....### .#.##..#..###.....#.###...#.#...#.#.#.#..........##.##....##.###.##.###.###..#...#..####..####..##.# ###..####.#..#..#......#.##..#...#...#.....###.#..##..#....##..###.#..##.......#.###.#.....#.#####.# .#.#..#..#....#...#..#####...##.#.#....#....##..####..###..###...#.#..#...######.#..#.##..#..#...### .#.##.#......#..####....#..#.#.#.#..#.#...#.....###.##.##..##..#...##........##.##..#.#####.##.##.#. ..#.#.##.##.####.#.###..#######..####.#..####....#.##.#.....#.#..######....##.#..#.##.#.####.######. ####.##....#...####..#######..##.#.#..##.##.###..#.#.##..###.##.#......#.....#....#.#..###.....##... ...#..#..#......##.#.##.#.#######...####.#..#.#.#.#.##..#####.....##....#.#..#..#..#.##.#.#.####..#. .###.##..##..###..#..#.#####.##...########..#.#.#...#######.#..#.###.#.#.#...#.#.#.###.#.#######.... ..###...##..#.#.##..#..#..#####..#..#...##..#.#....#...#..#..#..#..##.#.#.#.#.##....#.#...#.#####.## .#....#....#.##.###..#..##...#....##..#.####.##.#..##.##..#.##.....##..#...#.#......#..###..##..##.. ##..##..#...#.###.#.#.##.####.#...##..#..##.###.##..#.#...##....#.######.#..#.....#..#..#....####### .####.###.#######.#.#####..###.#####.##...##.###...#..#.#......##.....#####.#..#..#.#####.#..###.### .##.#..#.#####..#.#.####...#.#..#....#..#...##...###..#........######...#..####.#..#.#.#.#...####.#. ....#.#.##...#.#..###...#....###.###.#.#..#.#....#.#.####.....####..#.#####.....#...#.##.####.##.... #.#..#####.....#.###..#.#######.###......##.##.#...##.##..##....#....#...####..###.#.#.####...#.#.## [出力例] 11 |
■参照サイト
AtCoder Grand Contest 014