Snake Carpet UVALive - 7269 (构造)

北京大学电子工程学院为新生举办了一场AI蛇博弈比赛。比赛结束后,一位参赛者决定用比赛中的蛇图案制作一张地毯作为纪念。地毯为正方形,由多个1x1单位组成,每个蛇的宽度为1单位,长度不一。除了特定条件限制,每条蛇不能与自己或其他蛇相交,且地毯上的所有单位必须被蛇覆盖。问题是是否存在这样的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<bits/stdc++.h>
int n,a1,a2;
void aa(int n,int x,int y)
{
    if(n==5)
    {
        printf("3 4\n");
        printf("1 4 1 5\n");
        printf("2 4 2 5 3 5\n");
        printf("2 2 2 3 3 3 3 2\n");
        printf("3 1 2 1 1 1 1 2 1 3\n");
    }
    else
    {
         aa(n-2,x-1,y-2);
         for(int i=x;i>=1;i--)
         {
             if(i!=x) printf(" ");
             printf("%d %d",i,y);
         }
         for(int i=1;i<=n-1-x;i++)
         {
             printf(" %d %d",i,y-1);
         }
         printf("\n");
         for(int i=1;i<=n-1;i++)
         {
             if(i!=1) printf(" ");
             printf("%d %d",x,i);
         }
         printf(" %d %d\n",x-1,y-1);
    }
}
void aaaa(int x)
{
    if(x==1)
    {
        printf("1 1\n");
        printf("1 1\n");
    }
    else if(x==2)
    {
        printf("1 3\n");
        printf("1 1\n");
        printf("1 2 1 3\n");
    }
    else if(x==3)
    {
        printf("2 3\n");
        printf("1 2\n");
        printf("1 3 2 3\n");
        printf("1 1 2 1 2 2\n");
    }
    else if(x==4)
    {
        printf("2 5\n");
        printf("1 4\n");
        printf("1 5 2 5\n");
        printf("1 1 2 1 2 2\n");
        printf("1 2 1 3 2 3 2 4\n");
    }
}
int main()
{
  while(~scanf("%d",&n))
  {
      a1=3,a2=5;
      if(n<5)
      {
          aaaa(n);
          continue;
      }
      else
      {
          if(n%2==1)
          {
              int k=(n-5)/2;
              printf("%d %d\n",a1+k,a2+2*k);
              aa(n,a1+k,a2+2*k);
          }
          else
          {
              int k=(n-6)/2;
              printf("%d %d\n",a1+k,a2+2*k+2);
              aa(n-1,a1+k,a2+2*k);
              for(int i=1;i<=a1+k;i++)
              {
                  if(i!=1) printf(" ");
                  printf("%d %d",i,a2+2*k+1);
              }
              for(int i=a1+k;i>=1;i--)
              {
                  printf(" %d %d",i,a2+2*k+2);
              }
              printf("\n");
          }
      }
  }
}


 

 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值