C++の練習を兼ねて, AtCoder Grand Contest 004 の 問題C (AND Grid) を解いてみた.
■感想.
1. 方針が見えなかったので, 解説を参照して実装したところ, 何とか, AC版となった.
2. 個人的には, 非常に面白い問題に感じた.
3. 時間を見つけて, 引き続き, 過去問を振り返っていきたいと思う.
本家のサイトAGC 004 解説をご覧下さい.
■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 |
// 解き直し. // https://img.atcoder.jp/data/agc/004/editorial.pdf #include <bits/stdc++.h> using namespace std; #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[555][555], r[555][555], b[555][555]; int main(){ // 1. 入力情報. int H, W; scanf("%d %d", &H, &W); rep(i, H){ char c[555]; scanf("%s", c); rep(j, W) if(c[j] == '#') board[i][j] = 1; // 紫色. } // 2. 事前準備 および 紫色箇所を, 赤色, 青色 で 着色. rep(i, H){ rep(j, W){ // 1列目 … 赤色. if(j == 0) r[i][j] = 1; // W列目 … 青色. if(j == W - 1) b[i][j] = 1; // それ以外 … 赤色 もしくは 青色. if(j > 0 && j < W - 1){ if(i & 1){ b[i][j] = 1; // 紫色あれば赤色も着色. if(board[i][j]) r[i][j] = 1; }else{ r[i][j] = 1; // 紫色あれば青色も着色. if(board[i][j]) b[i][j] = 1; } } } } // 3. 出力. rep(i, H){ rep(j, W) printf("%c", (r[i][j]) ? '#' : '.'); puts(""); } puts(""); rep(i, H){ rep(j, W) printf("%c", (b[i][j]) ? '#' : '.'); puts(""); } 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 |
[入力例] 5 5 ..... .#.#. ..... .#.#. ..... [出力例] ..... ##### #.... ##### ..... .###. .#.#. .#.#. .#.#. ..... ※AtCoderテストケースより ※ 但し, 上記のプログラムでは, 以下の内容が出力される. ####. ##.#. ####. ##.#. ####. ....# .#### ....# .#### ....# [入力例] 7 13 ............. .###.###.###. .#.#.#...#... .###.#...#... .#.#.#.#.#... .#.#.###.###. ............. [出力例] ............. .###########. .###.###.###. .###.###.###. .###.###.###. .###.###.###. ............. ............. .###.###.###. .#.#.#...#... .###.#...#... .#.#.#.#.#... .#.#########. ............. ※AtCoderテストケースより ※ 但し, 上記のプログラムでは, 以下の内容が出力される. ############. ####.###.###. ############. ####.#...#... ############. ##.#.###.###. ############. ............# .############ .#.#.#...#..# .############ .#.#.#.#.#..# .############ ............# [入力例] 27 31 ............................... ............................... ....................#.......... ...................#.#......... .................##...##....... ...................#.#......... ....................#.......... ....###........................ ...#..#####...............#.... ......#..#..............##..... .....#.................##.#.... .......................#....... ......................##.#..... ............#.................. ...........#.#.......###....... ..........#...#......#.#....... .........#.....#.....#.#....... ........#.......#....#.#....... .......#.........#.###.###..... ..#####...#####...#.......#.... ..#...#...#.#.#...#.......#.... .##...#...#.#.#...#.......#.... ..#...#...#.#.#...#........###. ..#...#...#####...#..........#. ..#...#...........#..........#. ............................... ............................... [出力例] ##############################. #.............................. ##############################. #..................#.#......... ##############################. #..................#.#......... ##############################. #...###........................ ##############################. #.....#..#..............##..... ##############################. #......................#....... ##############################. #...........#.................. ##############################. #.........#...#......#.#....... ##############################. #.......#.......#....#.#....... ##############################. #.#####...#####...#.......#.... ##############################. ###...#...#.#.#...#.......#.... ##############################. #.#...#...#####...#..........#. ##############################. #.............................. ##############################. ..............................# .############################## ....................#.........# .############################## .................##...##......# .############################## ....................#.........# .############################## ...#..#####...............#...# .############################## .....#.................##.#...# .############################## ......................##.#....# .############################## ...........#.#.......###......# .############################## .........#.....#.....#.#......# .############################## .......#.........#.###.###....# .############################## ..#...#...#.#.#...#.......#...# .############################## ..#...#...#.#.#...#........#### .############################## ..#...#...........#..........## .############################## ..............................# |
■参照サイト
AtCoder Grand Contest 004