hihoCoder 1257 Snake Carpet(构造)

33 篇文章 0 订阅
14 篇文章 0 订阅
本文介绍了hihoCoder 1257题目的解题思路,重点在于如何构造Snake Carpet,涉及算法构造技巧。
摘要由CSDN通过智能技术生成

题目链接:hihoCoder 1257 Snake Carpet

代码

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;
typedef pair<int,int> pii;

char ans[5][100] = { "1 1\n1 1", "1 3\n1 1\n1 2 1 3", "2 3\n1 2\n1 3 2 3\n1 1 2 1 2 2",
                     "2 5\n1 4\n1 5 2 5\n1 1 2 1 2 2\n1 2 1 3 2 3 2 4",
                     "3 4\n1 4 1 5\n2 4 2 5 3 5\n2 2 2 3 3 3 3 2\n3 1 2 1 1 1 1 2 1 3"};

void put(vector<pii>& out) {
    printf("%d %d", out[0].first, out[0].second);
    for (int i = 1; i < out.size(); i++)
        printf(" %d %d", out[i].first, out[i].second);
    printf("\n");
}

void dfs (int r, int c, int d, int n) {
    if (d > n) return;
    vector<pii> out;

    if (d == n) {
        for (int i = 1; i < r; i++) out.push_back(make_pair(i, c));
        for (int i = r-1; i; i--) out.push_back(make_pair(i, c+1));
        put(out);
    } else {
        for (int i = r-2; i; i--) out.push_back(make_pair(i, c));
        for (int i = 1; i <= r; i++) out.push_back(make_pair(i, c+1));
        put(out);

        out.clear();
        for (int i = 1; i <= c; i++) out.push_back(make_pair(r, i));
        out.push_back(make_pair(r-1, c));
        put(out);
    }
    dfs(r + 1, c + 2, d + 2, n);
}

int main () {
    int n;
    while (scanf("%d", &n) == 1) {
        if (n < 5) printf("%s\n", ans[n-1]);
        else {
            int h = (n + 1) >> 1;
            int w = n * (n+1) / 2 / h;
            printf("%d %d\n%s\n", h, w, ans[4]);
            dfs(4, 6, 6, n);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值