hihocoder 1257 Snake Carpet

题目链接:I题

题目大意:给你n个贪吃蛇,要求你去构造一个矩阵,使得编号为k的贪吃蛇长度为k,并且奇数编号贪吃蛇需要经过奇数个转折点,偶数编号贪吃蛇需要经过偶数个转折点,然后所有贪吃蛇刚好占满这个矩阵的所有各自且不能交叉,要求你输出每条贪吃蛇的路径,不是单纯的位置

题目思路:我们先构造出矩阵的大小,发现这样的一个关系:
111

1

212

122

323

223
223

425

22344
13344

535

12235
44335
44555

637

1223566
4433566
4455566

747

2234457
1334457
6665557
6667777

然后我们就可以发现规律了,长宽跟n有关,偶数的情况直接在奇数的情况下构造一个右边2*n/2的矩阵就可以了,拐点刚好两个,奇数的情况我们需要考虑到它的n-3的情况,补上后三个就好了,然后代码递归去写就好了


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;

void fun(int cur){
    if(cur == 1){
        printf("1 1\n");
        return ;
    }
    if(cur == 2){
        printf("1 1\n");
        printf("1 2 1 3\n");
        return;
    }
    if(cur == 3){
        printf("2 1\n");
        printf("1 1 1 2\n");
        printf("1 3 2 3 2 2\n");
        return;
    }
    int tx = (cur+1)/2;
    int ty = (cur%2)? tx*2-1:tx*2+1;

    if(cur % 2 == 0){
        fun(cur-1);
        for(int i = 1; i <= cur/2; i ++)
            printf("%d %d ",i,ty-1);
        for(int i = cur/2; i >= 1; i--)
            printf("%d %d ",i,ty);
        printf("\n");
        return ;
    }
    else{
        fun(cur-3);
        for(int i = 1; i <= (cur-2)/2; i++)
            printf("%d %d ",i,ty-1);
        for(int i = 1; i <= (cur-2)/2+1; i++)
            printf("%d %d ",tx-1,ty-i);
        printf("\n");

        for(int i = 1; i <= (cur-1)/2; i++)
            printf("%d %d ",tx-1,i);
        for(int i = 0; i <= (cur-1)/2-1; i++)
            printf("%d %d ",tx,(cur-1)/2-i);
        printf("\n");

        for(int i = 0; i < cur/2+1; i++)
            printf("%d %d ",tx,ty-cur/2+i);
        for(int i = 0; i < cur/2; i++)
            printf("%d %d ",tx-i-1,ty);
        printf("\n");
        return ;
    }
}

int main(){
    int n;
    while(~scanf("%d",&n)){
        printf("%d %d\n",(n+1)/2,(n%2)?(n+1)/2*2-1:(n+1)/2*2+1);
        fun(n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值