hihoCoder 1257

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).
Hint: This problem is special judged, and the solutions for the
sample input are on the right:
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


简单构造

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=505;
struct node
{
    int x,y;
    void init(int x1,int y1)
    {
        x=x1,y=y1;
    }
};
node g[maxn][maxn];
void init()
{
    memset(g,0,sizeof(g));
    g[1][1].init(3,4),g[2][1].init(1,4),g[2][2].init(1,5);
    g[3][1].init(2,4),g[3][2].init(2,5),g[3][3].init(3,5);
    g[4][1].init(2,2),g[4][2].init(2,3),g[4][3].init(3,3),g[4][4].init(3,2);
    g[5][1].init(3,1),g[5][2].init(2,1),g[5][3].init(1,1),g[5][4].init(1,2),g[5][5].init(1,3);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        if(n==1)
        {
            printf("1 1\n1 1\n");
            continue;
        }
        if(n==2)
        {
            printf("1 3\n1 1\n1 2 1 3\n");
            continue;
        }
        if(n==3)
        {
            printf("2 3\n1 2\n1 3 2 3\n1 1 2 1 2 2\n");
            continue;
        }
        if(n==4)
        {
            printf("2 5\n1 4\n1 5 2 5\n1 1 2 1 2 2\n1 2 1 3 2 3 2 4\n");
            continue;
        }
        if(n==5)
        {
            printf("3 5\n3 4\n1 4 1 5\n2 4 2 5 3 5\n2 2 2 3 3 3 3 2\n3 1 2 1 1 1 1 2 1 3\n");
            continue;
        }
        for(int x=6; x<=n; x++)
        {
            int h=(x+1)/2,w=(x+1)*x/2/h;
            int h1=x/2,w1=(x-1)*x/2/h1;
            int cnt=1;
            if(x%2==0)
            {
                for(int i=1; i<=h; i++)
                {
                    g[x][cnt].init(i,w1+1);
                    cnt++;
                }
                for(int i=h;i>=1;i--)
                    g[x][cnt].init(i,w),cnt++;
            }
            else
            {
                for(int i=1; i<=x-1; i++)
                {
                    g[x][cnt].init(h,i);
                    cnt++;
                }
                g[x][cnt].init(h-1,x-1);
                cnt=1;
                for(int i=h;i>=1;i--)
                    g[x-1][cnt].init(i,w),cnt++;
                for(int i=1;i<=h-2;i++)
                    g[x-1][cnt].init(i,w-1),cnt++;
            }
        }
        int h=(n+1)/2,w=(n+1)*n/2/h;
        printf("%d %d\n",h,w);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                if(j==1) printf("%d %d",g[i][j].x,g[i][j].y);
                else printf(" %d %d",g[i][j].x,g[i][j].y);
            }
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值