UVaLive 7269 Snake Carpet (找规律,模拟)

题意:给定一个数字n,表示有n条蛇,然后蛇的长度是 i ,如果 i 是奇数,那么它只能拐奇数个弯,如果是偶数只能拐偶数个,1, 2除外,然后把这 n 条蛇,

放到一个w*h的矩阵里,要求正好放满,让你输出一个解,如果没有,输出0 0.

析:这个题目是找规律,先画一下前几个,画到第7个,就应该能找到规律,假设现在是第6个,并且是最后一个了,那么我们就可以在第5个基础上,在矩阵的

右边放上两列,正好是6,而且拐弯为偶数,如果是要放到7,那么就可以这样放,把第7条,放到第4行到第六列再向上一个,正好是7个,在右面放两排,

正好是6个,放上6即可。其他的都可以依次来推。

注意输出的顺序,是要按蛇的顺序来输出,蛇身不能断开。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}

void solve(){
    if(n & 1)  printf("%d %d
", n/2+1, n);
    else printf("%d %d
", n/2, n+1);
    puts("3 4");  puts("1 4 1 5");  puts("2 4 2 5 3 5");  puts("2 2 2 3 3 3 3 2");  puts("3 1 2 1 1 1 1 2 1 3");
    if(n & 1) m = n;
    else m = n - 1;

    for(int i = 6; i <= m; ++i){
        int x = (i+1) / 2;
        if(i & 1){
            for(int j = 1; j < i; ++j)
                printf("%d %d ", x, j);
            printf("%d %d
", x-1, i-1);
        }
        else{
            int yy = i/2-1;
            for(int j = yy; j > 0; --j)
                printf("%d %d ", j, i);
            for(int j = 1; j <= x; ++j)
                printf("%d %d ", j, i+1);
            printf("%d %d
", x+1, i+1);
        }
    }

    if(n & 1) return ;

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

int main(){
    while(scanf("%d", &n) == 1){
        if(1 == n)  printf("1 1
1 1
");
        else if(2 == n){
            printf("1 3
");  printf("1 1
");  printf("1 2 1 3
");
        }
        else if(3 == n){
            puts("2 3");  puts("1 2");  puts("1 3 2 3");  puts("1 1 2 1 2 2");
        }
        else if(4 == n){
            puts("2 5");  puts("1 4");  puts("1 5 2 5");  puts("1 1 2 1 2 2");  puts("1 2 1 3 2 3 2 4");
        }
        else if(5 == n){
            puts("3 5");  puts("3 4");  puts("1 4 1 5");  puts("2 4 2 5 3 5");  puts("2 2 2 3 3 3 3 2");  puts("3 1 2 1 1 1 1 2 1 3");
        }
        else solve();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5752777.html