虽然没去北京赛,还是搞了几道题目来做。
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 i
th
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 2k
th
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 i
th
line contain 2i integers,
indicating the coordinates of the i
th
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:
看出来是题构造,想了两天才A出来= = 具体方法:偶数是奇数加上两排,偶数是和(n-3)有关,还有输出比较蛋疼。
#include<stdio.h>
#include<string.h>
void solve(int n)
{
if(n==2)
{
printf("1 1 \n1 2 1 3 \n");
return ;
}
if(n==3)
{
printf("1 2 \n1 3 2 3 \n1 1 2 1 2 2 \n");
return ;
}
if(n==5)
{
printf("2 2 \n1 1 1 2 \n2 1 3 1 3 2 \n1 3 1 4 2 4 2 3 \n1 5 2 5 3 5 3 4 3 3 \n");
return ;
}
int i,j;
if(n%2)
{
solve(n-3);
j=n-1;
for(i=1;i<=(n-2)/2+1;i++)
printf("%d %d ",i,j);
i=(n-3)/2+1;
for(j=n-2;j>=(n+1)/2;j--)
printf("%d %d ",i,j);
printf("\n");
i=(n-3)/2+1;
for(j=1;j<=(n-1)/2;j++)
printf("%d %d ",i,j);
i++;
for(j=(n-1)/2;j>=1;j--)
printf("%d %d ",i,j);
printf("\n");
j=n;
for(i=1;i<=(n+1)/2;i++)
printf("%d %d ",i,j);
i=(n+1)/2;
for(j=n-1;j>=(n+1)/2;j--)
printf("%d %d ",i,j);
printf("\n");
}
else
{
solve(n-1);
j=n;
for(i=1;i<=(n+1)/2;i++)
printf("%d %d ",i,j);
j++;
for(i=(n+1)/2;i>=1;i--)
printf("%d %d ",i,j);
printf("\n");
}
return ;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==1)
{
printf("1 1 \n1 1 \n");
continue;
}
if(n%2)
printf("%d %d \n",(n+1)/2,n);
else
printf("%d %d \n",(n+1)/2,n+1);
solve(n);
}
return 0;
}
路漫漫其修远兮,吾将上下而求索。