zoukankan      html  css  js  c++  java
  • hiho1257 Snake Carpet

    题目链接:http://hihocoder.com/problemset/problem/1257

    题目大意:有n条蛇 编号为1-n 每条蛇的长度跟编号相等 奇数编号的蛇必须拐奇数次(除了第一条)偶数编号的蛇必须拐偶数次(除了第二条)问能不能在这种约束条件下面用蛇构造成一个矩形

    思路:构造题 肯定是有一种固定的构造方式,通过推可以发现矩形的长可以成为(n+1)/2 宽是n+((n&1)==0) 

    偶数很好做 因为直接在后面加就行了 奇数的时候可以发现通过前一个偶数和前一个奇数来向外面加一层

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int type[600];
    void pre(){
        type[0]=1;
        type[1]=1;
        for(int n=2;n<=550;n++){
            type[n]=type[n-2]+2;
        }
    }
    bool judge(int n){
        if(n<=3){
            if(n==1){
                printf("1 1
    ");
                return true;
            }
            if(n==2){
                printf("1 1
    1 2 1 3
    ");
                return true;
            }
            if(n==3){
                printf("2 1
    1 1 1 2
    1 3 2 3 2 2
    ");
                return true;
            }
        }
        return false;
    }
    void print(int n,int row,int col){
        if(n&1){
            for(int i=0;i<(n+1)/2;i++){
                printf("%d %d ",row+i,col);
            }
            for(int i=1;i<=n/2;i++){
                printf("%d %d%c",row+(n+1)/2-1,col-i,i==n/2?'
    ':' ');
            }
        }
        else {
            int tmp=n/2;
            for(int i=0;i<tmp;i++){
                printf("%d %d ",row,col-i);
            }
            for(int i=tmp-1;i>=0;i--){
                printf("%d %d%c",row+1,col-i,i==0?'
    ':' ');
            }
        }
    }
    void solve(int n,int row,int col){
        if(judge(n)) return;
        if(n&1){
            solve(n-3,row,col-2);
            print(n-2,row,col-1);
            print(n-1,row+(n-3)/2,col-(n+1)/2);
            print(n,row,col);
            return;
        }
        else {
            solve(n-1,row,col-2);
            for(int i=0;i<n/2;i++){
                printf("%d %d ",row+i,col);
            }
            for(int i=n/2-1;i>=0;i--){
                printf("%d %d%c",row+i,col-1,i==0?'
    ':' ');
            }
            return ;
        }
        return ;
    }
    int main(){
        int n;
        pre();
        while(scanf("%d",&n)!=EOF){
            printf("%d %d
    ",(n+1)/2,type[n]);
            solve(n,1,type[n]);
        }
        return 0;
    }
    /*
    133
    223

    13344
    22344

    22135
    44335
    44555
    偶数:
    *****nn
    *****nn
    *****nn
    奇数:
    * * * * n-2 n
    * * * * n-2 n
    * * * * n-2 n
    n-1n-1n-1n-2n-2n
    n-1n-1n-1n n n
    */
  • 相关阅读:
    drf之序列化组件
    RESTful API和Django的drf安装及使用
    Django之CBV中View、APIView源码分析
    Ajax以及crsf的校验
    Django之中间件
    Django之Auth用户认证模块
    Django之cookie、session以及FBV和CBV
    Django之Forms组件
    Django之ORM
    Django基本知识、路由层、视图层、模版层
  • 原文地址:https://www.cnblogs.com/as3asddd/p/6041520.html
Copyright © 2011-2022 走看看