题目链接:https://codeforces.com/contest/1368/problem/C
题意
构造一个只含有灰、白块的网格,要求:
- 所有灰块为一个连通块
- 每个灰块与偶数个灰块相邻
- 恰有 $n$ 个灰块四周都有灰块
题解
即在 $n = 1$ 网格的基础上更新右上角三个点的坐标即可。
代码一
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << 7 + (n - 1) * 3 << " "; vector<pair<int, int>> v( { {0, 0}, {0, 1}, {1, 0}, {1, 1}, {1, 2}, {2, 1}, {2, 2} } ); for (int i = 0; i < 7; i++) { cout << v[i].first << ' ' << v[i].second << " "; } for (int i = 1; i < n; i++) { for (int j = 4; j < 7; j++) { cout << v[j].first + i << ' ' << v[j].second + i << " "; } } }
代码二
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << 3 * n + 4 << " "; cout << 0 << ' ' << 0 << " "; for (int i = 0; i <= n; i++) { cout << i << ' ' << i + 1 << " "; cout << i + 1 << ' ' << i << " "; cout << i + 1 << ' ' << i + 1 << " "; } }