10.21 打卡搜索。

近期任务就是敲好暴力,调整状态。。。。
没有其他。
当然需要别的练练手,不然废了。

最近感觉自己把什么题都当模拟了。。。。。
上来就模拟。。。。。。。
从下午开始记得数吧、、

第一道:

poj 1416

切碎。数字段。
主要是要判断的调节比较细碎。。。。。看不懂提啊!

每次枚举合法的可以站在一起的数字。进行dfs。还是不行啊。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//by mars_ch
char s[15];
int target,num,len;
int ans[15],v[15];
int res,cnt; 
bool flag=false;
void dfs(int x,int y,int z)
{
    if(x == len)   //统计 
    {
        if(res<z)
        {
            res=z;
            num=y;
            for(int i=0;i<y;i++)
            {
                ans[i]=v[i];
            }
            cnt=1;
            if(flag) flag=true;
        }
        else if(res == z)
        {
            cnt++;
        }
    }
    int sum=0;
    for(int i=x;i<len;i++)
    {
        sum=sum*10+s[i]-'0';
        if(sum+z<=target)
        {
            v[y]=sum;
            dfs(i+1,y+1,sum+z);
        }
        else break;
    }
 } 
int main()
{
    while(scanf("%d%s",&target,s) && target!=0 && s[0]!=0)
    {
        len=strlen(s);
        cnt=0,res=0,num=0;
        flag=false;
        int p=0,sum=0;
        while(p<len)
        {
            sum=sum*10+s[p]-'0';
            p++;
        }
        if(sum == target)
        {
            printf("%d %d\n",target,target);
            continue;
        }
        dfs(0,0,0);

        if(flag||(res == 0 && target!=0)|| res>target) printf("error\n");
        else
        {
            if(cnt>1) printf("rejected\n");
            else
            {
                printf("%d ",res); 
                for(int i=0;i<num;i++)
                {
                    if(i != num-1) printf("%d ",ans[i]); 
                    else printf("%d\n",ans[i]);
                } 
            }
        }
    }
    return 0;
}

第二道:
poj 1129
挺简单的一道搜索。。。。但是我竟然很想模拟。或者贪心【这个是错的】。

但是时间复杂度有点说不清。。。

所以 我们枚举当前用k个颜色染点能否ok,不ok就颜色num++。。

问题还是在于 dfs的递归不是很好?

WA了1次是输出没有加句号。。。。。打死我吧。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//by mars_ch
int n,ans;
struct data
{
    int f,t,w,nxt;
}e[10005];
int first[10005],tot,col[10005];
bool flag=false;
void add(int a,int b)
{
    e[tot].f=a;
    e[tot].t=b;
    e[tot].nxt=first[a];
    first[a]=tot++;
}
bool judge(int x,int c)
{
    for(int i=first[x];i!=-1;i=e[i].nxt)
    {
        if(col[e[i].t] == c)
        {
            return false;
        }
    }
    return true;
}
void dfs(int x,int num)
{
    if(flag) return;
    if(x>=n)
    {
        flag=true;
        return;
    }
    for(int i=1;i<=num;i++)
    {
        if(judge(x,i))
        {
            col[x]=i;                             
            dfs(x+1,num);

            //col[x]=0;
        }
    }
    if(!flag)
    {
        ans++;
        dfs(x,num+1);
    }
}
int main()
{
    while(scanf("%d",&n) && n!=0)
    {
        scanf("\n");
        memset(first,-1,sizeof(first));
        memset(col,0,sizeof(col));
        for(int i=1;i<=n;i++)
        {
            char c;
            scanf("%c:",&c);
            int a=c-'A';
            while(scanf("%c",&c) && c!='\n')
            {
                int b=c-'A';
                add(a,b);
            }
            scanf("\n");
        }
        ans=1;
        flag=false;
        dfs(0,1);
        if(ans == 1) printf("%d channel needed.\n",ans);
        else printf("%d channels needed.\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值