LA 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", n/2+1, n);
    else printf("%d %d\n", 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\n", 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\n", 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", n/2, n+1);
}

int main(){
    while(scanf("%d", &n) == 1){
        if(1 == n)  printf("1 1\n1 1\n");
        else if(2 == n){
            printf("1 3\n");  printf("1 1\n");  printf("1 2 1 3\n");
        }
        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;
}
时间: 2024-08-09 09:41:17

LA 7269 Snake Carpet (找规律,模拟)的相关文章

洛谷 P1014 Cantor表【蛇皮矩阵/找规律/模拟】

题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … 3/1 3/2 3/3 … 4/1 4/2 … 5/1 … … 我们以Z字形给上表的每一项编号.第一项是1/1,然后是1/2,2/1,3/1,2/2,… 输入输出格式 输入格式: 整数N(1≤N≤10000000) 输出格式: 表中的第N项 输入输出样例 输入样例#1: 复制 7 输出样例#1: 复

8/2 multi4 E找规律+模拟,空间开小了然后一直WA。。。J爆搜check不严谨WA。。。multi3 G凸包判共线写错数组名???样例太好过.想哭jpg。

multi4 Problem E. Matrix from Arrays 题意:构造一个数组,求子矩阵前缀和. 思路:打表找规律,"发现"L为奇数时循环节为L,为偶数时循环节为2L,求相应循环节的二维前缀和然后加加减减计算一下就好. 虚伪地证明一下循环节:L为奇数时对于第x行/列开始的位置有(x  +  x+L-1)*L/2   ->     (2x+L-1)/2(为整数)*L,因此扫过L行/列也就扫过了L整数倍"(2x+L-1)/2"倍的A[0]~A[L],

[找规律][模拟]罗马数字

贴一个洛谷的链接吧. tvvj题号是1070 思考 1 A 2 AA 3 AAA 4 AB 5 B 6 BA 7 BAA 8 BAAA 9 AC 无论个位十位百位千位 上面的数字总是会出现这样的规律 那么这道题就是暴力模拟题目~ 举个例子 255 CC L V 268 CC LX VIII 278 CC LXX VIII 289 CC LXXX IX 299 CC XC IX 发现了规律了没有? 9是一类 4是一类 5-8一类 1-3一类 那么思路就出现了 先判断个位上面的数 如果是九 那么一定

LA 3357 (递推 找规律) Pinary

n位不含前导零不含连续1的数共有fib(n)个,fib(n)为斐波那契数列. 所以可以预处理一下fib的前缀和,查找一下第n个数是k位数,然后再递归计算它是第k位数里的多少位. 举个例子,比如说要找第11个数,发现它是个5位数,所以最高位是个1,然后它还是5位数里的第4个数. 这时要找第三个数了,因为后面的数允许有前导零,第三个数是100,所以第5位是1,第4位是0,后面三位是100, 答案就是10100 说的有点啰嗦,在纸上模拟一下还是很容易理解的. 1 #include <cstdio>

HDU 6121 Build a tree(找规律+模拟)

Build a tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 946    Accepted Submission(s): 369 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n−1

UVALive 7269 Snake Carpet (构造)

题目:传送门. 题意:构造出一个矩阵,使得矩阵含有n条蛇,每条蛇的长度是1到n,并且奇数长度的蛇有奇数个拐弯,偶数长度 的蛇有偶数个拐弯. 奇数和偶数分开构造,奇数可以是: 1357 3357 5557 7777 这样一直构造下去,偶数可以这样: 2266 4466 4466 8888 8888 不断的放右边和放下面. 然后预处理每一个蛇的每一个坐标,然后考虑一下偶数的是放在奇数部分的右边还是下面. #include <bits/stdc++.h> using namespace std; #

HDU 4588 Count The Carries(找规律,模拟)

题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <stack> #include <vector> using namespace std; int main() { int

450A - Jzzhu and Children 找规律也可以模拟

挺水的一道题,规律性很强,在数组中找出最大的数max,用max/m计算出倍数t,然后再把数组中的书都减去t*m,之后就把数组从后遍历找出第一个大于零的就行了 #include<iostream> #include<stdio.h> using namespace std; int main(){ // freopen("in.txt","r",stdin); int a[105],n,m; while(~scanf("%d%d&qu

HDU 1337 The Drunk Jailer--(模拟题找规律)

题意:有n个监狱,共n轮,第 i 轮警察会去编号为 i 的倍数的监狱,如果是锁的就开锁,如果是开的就锁上,求n轮过后有多少犯人会逃出来 分析:这题实际上是个模拟题,因为数据很小我直接用两重循环模拟的,如果数据很大的话,就不能直接模拟了,模拟题卡时间多半是找规律. 这题的规律是:如果一个监狱被查看了偶数次的话相当于则什么都没发生,还是锁的,也就是说找没有锁上的监狱只要n以内找因数为奇数个的数有多少个即可 代码: #include<iostream> using namespace std; in