• Gym 100518E Embedding Caterpillars


    构造+DFS

    很容易的可以构造两个,最后一个不会构造的话  DFS一下就可以了

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<stdio.h>
    #include<queue>
    #include<iomanip>
    #include<algorithm>
    #include<map>
    using namespace std;
    
    int n;
    int G[205][205];
    int flag[205];
    int ans[205];
    int TOT,FF;
    
    void dfs(int now)
    {
        if(now==2*n+1)
        {
            for(int i=1; i<=2*n; i++) printf("%d ",ans[i]);
            FF=1;
            return;
        }
        if(now==1)
        {
            for(int i=1; i<=2*n; i++)
            {
                if(!flag[i])
                {
                    flag[i]=1;
                    ans[now]=i;
                    dfs(now+1);
                    if(FF) return;
                    flag[i]=0;
                }
            }
        }
        else if(now>=2&&now<=n+1)
        {
            for(int i=1; i<=2*n; i++)
            {
                if(!flag[i]&&!G[i][ans[now-1]])
                {
                    flag[i]=1;
                    ans[now]=i;
                    dfs(now+1);
                    if(FF) return;
                    flag[i]=0;
                }
            }
        }
        else if(now>=n+2&&now<=2*n)
        {
            for(int i=1; i<=2*n; i++)
            {
                if(!flag[i]&&!G[i][ans[now-n]])
                {
                    flag[i]=1;
                    ans[now]=i;
                    dfs(now+1);
                    if(FF) return;
                    flag[i]=0;
                }
            }
        }
    }
    
    int main( )
    {
        freopen("embedding.in","r",stdin);
        freopen("embedding.out","w",stdout);
        while(~scanf("%d",&n))
        {
            if(n==0) break;
            TOT=1;
            for(int i=1; i<=2*n; i++) ans[TOT]=i,TOT++;
            TOT--;
            for(int i=1; i<=TOT; i++) printf("%d ",ans[i]);
            printf("
    ");
            for(int i=1; i<=n; i++) G[ans[i]][ans[i+1]]=1,G[ans[i+1]][ans[i]]=1;
            for(int i=2; i<=n; i++) G[ans[i]][ans[i+n]]=1,G[ans[i+n]][ans[i]]=1;
            TOT=1; ans[TOT]=1; TOT++;
            for(int i=n+2; i<=2*n; i++) ans[TOT]=i,TOT++;
            ans[TOT]=n+1; TOT++; ans[TOT]=n; TOT++;
            for(int i=2; i<=n-1; i++) ans[TOT]=i,TOT++;
            TOT--;
            for(int i=1; i<=TOT; i++) printf("%d ",ans[i]);
            printf("
    ");
            for(int i=1; i<=n; i++) G[ans[i]][ans[i+1]]=1,G[ans[i+1]][ans[i]]=1;
            for(int i=2; i<=n; i++) G[ans[i]][ans[i+n]]=1,G[ans[i+n]][ans[i]]=1;
            FF=0;
            memset(flag,0,sizeof flag);
            dfs(1);
        }
        return 0;
    }
  • 相关阅读:
    【示例】手把手教你构建一个简单的JavaWeb应用(会员注册唯一性检查,不带数据库)
    [JAVA]字符串单词倒转处理前面的空格
    [JAVA]用数学解释数组移步问题(新增对链表移步的解释)
    686. Repeated String Match
    160. Intersection of Two Linked Lists
    278. First Bad Version
    7. Reverse Integer
    155. Min Stack
    203. Remove Linked List Elements
    141. Linked List Cycle
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4792410.html
Copyright © 2020-2023  润新知