蓝桥杯-剪邮票

剪邮票

找的大神代码
渣渣我只会看别人代码。。stay foolish….

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

在原图中向上为-4,向下为+4,向左为-1,向右为+1,但是遇到3 4 5 7 8这种4+1=5但是这种情况不符合,所以重构一下原图:

这里写图片描述

这样,向上为-5,向下为+5,向左为-1,向右为+1,避免了每行最后一个+1后等于下一行第一个的情况。

#include<bits/stdc++.h>
using namespace std;


int Map[12]= {1,2,3,4,6,7,8,9,11,12,13,14};
int aa[5],vis[5],sum;
int p[5]= {-1,1,-5,5};



void dfs(int x)
{
    for(int i=0; i<4; i++)
    {
        int t=aa[x]+p[i];
        if(t<1||t>14||t==5||t==10)
            continue;
        for(int j=0; j<5; j++)
        {
            if(!vis[j]&&aa[j]==t)
            {
                vis[j]=1;
                dfs(j);
            }
        }
    }
}




int main()
{
    sum=0;
    for(int a=0; a<12; a++)
    {
        for(int b=a+1; b<12; b++)
        {
            for(int c=b+1; c<12; c++)
            {
                for(int d=c+1; d<12; d++)
                {
                    for(int e=d+1; e<12; e++)
                    {
                        aa[0]=Map[a];
                        aa[1]=Map[b];
                        aa[2]=Map[c];
                        aa[3]=Map[d];
                        aa[4]=Map[e];
                        for(int i=0; i<5; i++)
                        {
                            vis[i]=0;
                        }
                        vis[0]=1;
                        dfs(0);
                        int flag=1;
                        for(int i=0; i<5; i++)
                        {
                            if(vis[i]!=1)
                            {
                                flag=0;
                                break;
                            }
                        }

                        if(flag==0)
                            continue;
                        else
                            sum++;
                    }
                }
            }
        }
    }

    printf("%d\n",sum);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值