蓝桥杯试题 剪邮票 C语言

剪邮票


如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。

请你计算,一共有多少种不同的剪取方法。


思路:先找到5个数的组合,然后从第一个数字开始遍历,经过上下左右操作检测5个数是否都被访问一遍,如果5个数都可以遍历到则种类+1。

图中向上为-4,向下为+4,向左为-1,向右为+1,但是遇到3 4 5 7 8这种4+1=5但是这种情况不符合,需注意。

答案:116

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int record[100000][5] = {0};
int new_record[5] = {0};
int visit[13] = {0};
int count = 0;

bool judge(int x)//判断是否相邻,相邻返回true
{
    if(!x)
        return true;

    for(int i = x - 1; i >= 0; i--)
    {
        if(new_record[i] == 4 || new_record[i] == 5 || new_record[i] == 8 || new_record[i] == 9)
        {
            if(new_record[i] == 4)
                if( new_record[x] == 3 || new_record[x] == 8)
                    return true;

            if(new_record[i] == 5)
                if(new_record[x] == 1 || new_record[x] == 6 || new_record[x] == 9)
                    return true;

            if(new_record[i] == 8)
                if(new_record[x] == 4 || new_record[x] == 7 || new_record[x] == 12)
                    return true;

            if(new_record[i] == 9)
                if(new_record[x] == 5 || new_record[x] == 10)
                    return true;
        }

        else if( (abs(new_record[x] - new_record[i]) == 1) || (abs(new_record[x] - new_record[i]) == 4) )//相邻
            return true;
    }

    return false;
}

bool judgeEnd(int count)//判断是否重复,不重复返回true
{
    bool check1[100000] = {false};
    for(int i = count - 1; i >= 0; i--)
    {
        bool check2[5] = {false};
        for(int j = 0; j < 5; j++)
        {
            for(int k = 0; k < 5; k++)
            {
                if(record[i][j] == new_record[k])//重复
                    check2[j] = true;
            }
        }

        int flag = 0;//重复
        for(int j = 0; j < 5; j++)
        {
            if(!check2[j])//不重复
            {
                flag = 1;//不重复
                check1[i] = true;
            }
        }

        if(!flag)//重复
            return false;
    }

    for(int i = count - 1; i >= 0; i--)
    {
        if(!check1[i])//重复
            return false;
    }
    //不重复
    return true;
}

void fun(int x)
{
    if(x == 5)
    {
        if(judgeEnd(count))//不重复
        {
            for(int i = 0; i < 5; i++)//写入record
                record[count][i] = new_record[i];

            count++;
        }

        return;
    }

    for(int i = 1; i <= 12; i++)
    {
        if(visit[i])//已访问
            continue;

        new_record[x] = i;
        if( !judge(x) )//不相邻
            continue;

        visit[i]++;
        fun(x + 1);
        visit[i]--;
    }
}

int main()
{
    fun(0);
    printf("%d\n", count);
    for(int i = count - 1; i >= 0; i--)//打印结果,校验
    {
        for(int j = 0; j < 5; j++)
            printf("%d ", record[i][j]);

        puts("");
    }

    return 0;
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值