C++の練習を兼ねて, AtCoder Regular Contest 112 の 問題D (Skate) を解いてみた.
■感想.
1. 問題D は, 方針が見えなかったので, 解説を参考に, AC版に到達できたと思う.
2. 個人的には, 問題D で, 幅優先探索 の 復習が出来たので, 非常に良かったと思う.
3. 引き続き, 時間を見つけて, 過去問の学習を進めていきたいと思う.
本家のサイト AtCoder Regular Contest 112 解説 の 各リンク を ご覧下さい.
■C++版プログラム(問題D/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 95 96 97 98 99 100 |
// 解き直し. // https://atcoder.jp/contests/arc112/editorial/719 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using vs = vector<set<int>>; #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--) int board[1010][1010]; int main(){ // 1. 入力情報. int h, w; scanf("%d %d", &h, &w); rep(i, h){ char c[1010]; scanf("%s", c); rep(j, w) board[i][j] = (c[j] == '#'); } // 2. スケートリンク四隅. board[0][0] = board[0][w - 1] = board[h - 1][0] = board[h - 1][w - 1] = 1; // 3. グラフ作成. // 3-1. 行方向. vs H(h); rep(j, w){ int b = -1; rep(i, h){ if(board[i][j]){ // 辺追加. if(b != -1){ H[b].insert(i); H[i].insert(b); } // 前回分更新. b = i; } } } // 3-2. 列方向. vs W(w); rep(i, h){ int b = -1; rep(j, w){ if(board[i][j]){ // 辺追加. if(b != -1){ W[b].insert(j); W[j].insert(b); } // 前回分更新. b = j; } } } // 4. bfs. // https://ja.wikipedia.org/wiki/幅優先探索 auto bfs = [&](vs &G, int s, int* c, int l){ // 空のキュー. queue<int> q; // 連結成分番号を設定. c[s] = l; // 探索地点 s をキュー q に追加. q.push(s); while(!q.empty()){ // キューから取り出す. int u = q.front(); q.pop(); // 隣接頂点をチェック. for(auto &e : G[u]) if(!c[e] && e != s) c[e] = l, q.push(e); } }; // 5. 連結成分への分解. // 5-1. 行方向. int hc[h], hIdx = 0; rep(i, h) hc[i] = 0; rep(i, h) if(!hc[i]) bfs(H, i, hc, ++hIdx); // 5-2. 列方向. int wc[w], wIdx = 0; rep(i, w) wc[i] = 0; rep(i, w) if(!wc[i]) bfs(W, i, wc, ++wIdx); // 6. 出力. printf("%d\n", min(hIdx, wIdx) - 1); 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 |
[入力例] 3 9 ......... ......... ......... [出力例] 1 ※AtCoderテストケースより [入力例] 10 10 .......... #...#..... .......... .......... .......... ....#..... .#......#. .......... .......... .......... [出力例] 6 ※AtCoderテストケースより [入力例] 2 2 .. .. [出力例] 0 [入力例] 3 3 ... ... ... [出力例] 1 [入力例] 8 7 ....... ..#.... .##.... ..#.... ..#.... ..#.... ...#..# ....... [出力例] 1 [入力例] 8 13 ............. ............. ..#.......#.. ............. ....#...#.... ............. ......#...... ............. [出力例] 6 [入力例] 15 28 ............................ ............................ ..####...###..####...####... ......#.#...#.....#......#.. ......#.#...#.....#......#.. ......#.#...#.....#......#.. ......#.#...#.....#......#.. ...###..#...#..###...#####.. ..#.....#...#.#..........#.. ..#.....#...#.#..........#.. ..#.....#...#.#..........#.. ..#.....#...#.#..........#.. ...####..###...####..####... ............................ ............................ [出力例] 3 [入力例] 27 75 ........................................................................... ........................................................................... ........................................................................... .......#.......#######.......#####........##.........##......########...... ......#.#......#......#.....#.....#......#.#........#.#..............#..... ......#.#......#.......#...#.......#....#..#.......#..#..............#..... .....#...#.....#......#....#...............#..........#..............#..... .....#...#.....#.#####.....#...............#..........#.......#######...... ....#.###.#....#..#........#...............#..........#......#............. ....#.....#....#...#.......#.......#.......#..........#......#............. ...#.......#...#....##......#.....#........#..........#......#............. ...#.......#...#......##.....#####.....#########..#########...########..... ........................................................................... ........................................................................... ........................................................................... ....########.......##..........##........#####......#######......#......... ...#...............#.#.........#.#......#.....#....#......#.....#.#........ ...#...............#..#........#..#....#.......#..#.......#.....#.#........ ...#...............#...........#...............#...#......#....#...#....... ....#######........#...........#...............#....#####.#....#...#....... ...........#.......#...........#...............#.......#..#...#.###.#...... ...........#.......#...........#.......#.......#......#...#...#.....#...... ...........#.......#...........#........#.....#.....##....#..#.......#..... ...########....#########...#########.....#####.....##.....#..#.......#..... ........................................................................... ........................................................................... ........................................................................... [出力例] 8 |
■参照サイト
AtCoder Regular Contest 112