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
题意: 给你一个数n,代表所需蛇的数量。要求每条蛇的长度为其序号,比如1的长度就为1。然后要求把这些蛇放在一起,构成H*W的矩形,要求不自交,也不与其他蛇相交,且出1 2 特殊外,其他奇数蛇要有奇数个转折点,偶数蛇要有偶数个转折点。思路:矩形的大小很容易判断,可以把矩形想象成有很多单位为1的正方形组成,面积n*(n+1)/2,因此长和宽就很容易确定
矩形的大小固定后,就是填数了,然后画了几个之后,想了一下构造的思路,也就是由构造好的,推出未构造的。因为偶数的第一维和他前面奇数的第一维是相等的,故偶数可以非常轻松的由前一个奇数推出。只需在下图右边补足红色部分即可。
如:
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>
#include<iostream>
using namespace std;
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;
}