题目:传送门。
题意:构造出一个矩阵,使得矩阵含有n条蛇,每条蛇的长度是1到n,并且奇数长度的蛇有奇数个拐弯,偶数长度
的蛇有偶数个拐弯.
奇数和偶数分开构造,奇数可以是:
1357
3357
5557
7777
这样一直构造下去,偶数可以这样:
2266
4466
4466
8888
8888
不断的放右边和放下面.
然后预处理每一个蛇的每一个坐标,然后考虑一下偶数的是放在奇数部分的右边还是下面.
#include <bits/stdc++.h> using namespace std; #define maxn 1111 struct point { int x, y; }; vector <point> p[maxn]; int n; void init () { for (int i = 1; i <= 1000; i++) { p[i].clear (); } for (int i = 1; i <= 1000; i++) { if (i&1) { int x = (i+1)/2; int y = x; for (int j = 1; j <= x; j++) { p[i].push_back ((point) {x, j}); } for (int j = x-1; j >= 1; j--) { p[i].push_back ((point) {j, y}); } } else { if (i == 2) { p[2].push_back ((point) {1, 1}); p[2].push_back ((point) {1, 2}); } else if ((i>>1)&1) { int y = i>>1; for (int j = 1; j <= y; j++) { p[i].push_back ((point) {j, y}); } for (int j = y; j >= 1; j--) { p[i].push_back ((point) {j, y+1}); } } else { int x = i>>1; for (int j = 1; j <= x; j++) { p[i].push_back ((point) {x, j}); } for (int j = x; j >= 1; j--) { p[i].push_back ((point) {x+1, j}); } } } } } int main () { init (); while (scanf ("%d", &n) == 1) { if (((n+1)/2)&1) {//偶数放在右边 printf ("%d %d\n", (n+1)>>1, (n/2)+(n/2)+1); for (int i = 1; i <= n; i++) { for (int j = 0; j < p[i].size (); j++) { if (i&1) { printf ("%d %d%c", p[i][j].x, p[i][j].y, j == p[i].size()-1 ? ‘\n‘:‘ ‘); } else { printf ("%d %d%c", p[i][j].x, p[i][j].y+((n+1)>>1), j == p[i].size()-1 ? ‘\n‘:‘ ‘); } } } } else {//偶数放在下面 printf ("%d %d\n", (n/2)+(n/2)+1, (n+1)>>1); for (int i = 1; i <= n; i++) { for (int j = 0; j < p[i].size (); j++) { if (i&1) { printf ("%d %d%c", p[i][j].x, p[i][j].y, j == p[i].size()-1 ? ‘\n‘:‘ ‘); } else { printf ("%d %d%c", p[i][j].x+((n+1)>>1), p[i][j].y, j == p[i].size()-1 ? ‘\n‘:‘ ‘); } } } } } return 0; }
时间: 2024-10-02 02:34:54