题目链接:http://hihocoder.com/problemset/problem/1255
题面:
Problem I. Snake Carpet
Description :
In school of EECS of Peking University, there is a homework for all freshman -- the contest of AI snakes. This contest is ended today. Bacchus has got a very good result, so he decides to make a carpet full of snakes as a souvenir, and lays it over the floor in his room.As his room is square, a square carpet is needed. A H×W carpets is made up of H×W units(each unit is 1×1). Snakes can have different length, but all snakes' width is 1 unit. For some reason, He hopes that N special snakes are drawn on the carpet: the length of the ith snake should be i, which can be seen as i connected units(Two units that share an edge are considered connected). Except the first snake, the (2k−1)th snake should have positive odd number of turning points; except the second snake, the 2kth snake should have an positive even number of turning points. i and k both start from 1. Each snake should not intersect with itself, nor with other snakes. All units of the carpet must be covered by snakes.But the question is whether there is a solution.
Input:
Multiple test cases. There will be up to 25 cases.For each test case: one line contains one integer N, indicating the number of snakes.(1≤N≤500)
Output:
For each test case:If the solution does not exist, output one line "0 0", otherwise output N+1 lines: The first line contains two integers H and W, indicating the height and the width of the carpet. You should guarantee that H×W=1+2+⋯+N. For the next N lines, the ith line contain 2i integers,indicating the coordinates of the ith snake in order. The coordinate of top-left corner unit is(1,1) and the coordinate of bottom-right corner unit is (H,W)
Sample Input:
3
4
5
Sample Output :
2 3
1 2
1 3 2 3
1 1 2 1 2 2
2 5
1 4
1 5 2 5
1 1 2 1 2 2
1 2 1 3 2 3 2 4
3 5
3 4
1 4 1 5
2 4 2 5 3 5
2 2 2 3 3 3 3 2
3 1 2 1 1 1 1 2 1 3
Hint:This problem is special judged, and the solutions for the sample input are:
3 1 2
3 3 2
3 4 4 1 2
3 3 4 4 2
5 5 5 2 2
5 4 4 3 3
5 4 4 1 3
吐槽:先吐槽下PDF,每次拷过来都会乱,真是无语。
题目大意:
给你一个数n,代表所需蛇的数量。要求每条蛇的长度为其序号,比如1的长度就为1。然后要求把这些蛇放在一起,构成H*W的矩形,要求不自交,也不与其他蛇相交,且出1 2 特殊外,其他奇数蛇要有奇数个转折点,偶数蛇要有偶数个转折点。
解题:
一开始自己画,发现矩形可以是(n+1)/2 和 (n+1)*n/2/((n+1)/2) 此中除为整除。
如: 2 1*3
3 2*3
4 2*5
5 3*5
6 3*7
7 4*7
8 4*9
......
矩形的大小固定后,就是填数了,一开始直接填,发现就算手工填出来了,代码也不太可能实现。想想构造题的套路,应该是有一种固定的构造模式。也就是说由构造好的,推出未构造的。因为偶数的第一维和他前面奇数的第一维是相等的,故偶数可以非常轻松的由前一个奇数推出。只需在下图右边补足红色部分即可。
如:
2 2 3 4 4 1 2 2 3 5 6 6
1 3 3 4 4 4 4 3 3 5 6 6
4 4 5 5 5 6 6
而奇数好像就不好推了,因为奇数需要拐弯,直接由前一个无法推出,但是我们可以继续尝试前两个,直到前三个,我们可以找到一种固定的构造方案。
如:
x x x x x n-2 n
x x x x x n-2 n
n-1 n-1 n-1 n-2 n-2 n-2 n
n-1 n-1 n-1 n n n n
这样就可以往下递归输出了。(实现详见代码)
坑点:输出要求按蛇的组织顺序输出,被坑了好久。
代码:
#include<cstdio>
#include<cstring>
int map[510][510];
struct node
{
int x[505],y[505];
}p[505];
int cnt[505];
//画函数
void f(int n)
{
int x,y,i,lie,num,hang;
x=(n+1)/2;
y=n*(n+1)/2;
y=y/x;
//特判,递归终点
if(n==2)
{
printf("1 1\n");
printf("1 2 1 3\n");
return;
}
if(n==3)
{
printf("2 1\n");
printf("1 1 1 2\n");
printf("1 3 2 3 2 2\n");
return;
}
//奇数
if(n%2)
{
//往前三个递归
f(n-3);
//子图的的列数和行数
lie=(n-3)*(n-2)/2/((n-2)/2);
hang=(n-2)/2;
num=(n-1)/2;
//先输出n-2
for(int i=1;i<=num;i++)
printf("%d %d ",i,lie+1);
for(int i=0;i<num-1;i++)
printf("%d %d ",hang+1,lie-i);
printf("\n");
//n-1部分
for(int i=1;i<=(n-1)/2;i++)
printf("%d %d ",hang+1,i);
for(int i=(n-1)/2;i>=1;i--)
printf("%d %d ",hang+2,i);
printf("\n");
//n部分
for(int i=1;i<=num+1;i++)
printf("%d %d ",i,lie+2);
for(int i=0;i<num;i++)
printf("%d %d ",hang+2,lie-i+1);
printf("\n");
}
//偶数
else
{
//递归前一个奇数
f(n-1);
lie=(n-1)*n/2/(n/2);
//右侧补足n
for(int i=1;i<=n/2;i++)
printf("%d %d ",i,lie+1);
for(int i=n/2;i>=1;i--)
printf("%d %d ",i,lie+2);
printf("\n");
}
}
int main()
{
int n,i,j,x,y;
while(~scanf("%d",&n))
{
//1特判
if(n==1)
{
printf("1 1\n1 1 \n");
continue;
}
x=(n+1)/2;
y=n*(n+1)/2;
y=y/x;
printf("%d %d\n",x,y);
f(n);
}
return 0;
}