UVALive 7269 Snake Carpet (构造)

题目:传送门

题意:构造出一个矩阵,使得矩阵含有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 = ; i <= ; i++) {
p[i].clear ();
}
for (int i = ; i <= ; i++) {
if (i&) {
int x = (i+)/;
int y = x;
for (int j = ; j <= x; j++) {
p[i].push_back ((point) {x, j});
}
for (int j = x-; j >= ; j--) {
p[i].push_back ((point) {j, y});
}
}
else {
if (i == ) {
p[].push_back ((point) {, });
p[].push_back ((point) {, });
}
else if ((i>>)&) {
int y = i>>;
for (int j = ; j <= y; j++) {
p[i].push_back ((point) {j, y});
}
for (int j = y; j >= ; j--) {
p[i].push_back ((point) {j, y+});
}
}
else {
int x = i>>;
for (int j = ; j <= x; j++) {
p[i].push_back ((point) {x, j});
}
for (int j = x; j >= ; j--) {
p[i].push_back ((point) {x+, j});
}
}
}
}
} int main () {
init ();
while (scanf ("%d", &n) == ) {
if (((n+)/)&) {//偶数放在右边
printf ("%d %d\n", (n+)>>, (n/)+(n/)+);
for (int i = ; i <= n; i++) {
for (int j = ; j < p[i].size (); j++) {
if (i&) {
printf ("%d %d%c", p[i][j].x, p[i][j].y, j == p[i].size()- ? '\n':' ');
}
else {
printf ("%d %d%c", p[i][j].x, p[i][j].y+((n+)>>), j == p[i].size()- ? '\n':' ');
}
}
}
}
else {//偶数放在下面
printf ("%d %d\n", (n/)+(n/)+, (n+)>>);
for (int i = ; i <= n; i++) {
for (int j = ; j < p[i].size (); j++) {
if (i&) {
printf ("%d %d%c", p[i][j].x, p[i][j].y, j == p[i].size()- ? '\n':' ');
}
else {
printf ("%d %d%c", p[i][j].x+((n+)>>), p[i][j].y, j == p[i].size()- ? '\n':' ');
}
}
}
}
}
return ;
}
上一篇:UVaLive 7269 Snake Carpet (找规律,模拟)


下一篇:JavaScript面向对象编程小游戏---贪吃蛇