zoukankan      html  css  js  c++  java
  • DFS——hdu4068

    三国杀
    摆放自己的英雄,使敌方英雄不管怎么摆都你赢
    N<=6
    暴力枚举
    自己英雄序列6!*敌方英雄序列6!*6判断
    这种题目还是不够仔细,错了几次
    View Code
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;

    bool hash[9];
    int no[9];

    bool hash2[9];
    int no2[9];

    struct data
    {
    int n;
    char name[9][29];
    }node[9];

    struct Name
    {
    char str[29];
    }f[9];

    int N;

    int cmp(Name a,Name b)
    {
    return strcmp(a.str,b.str)<0;
    }

    int ok=0;
    int ok2=0;

    int bi()
    {
    int i=0,j=0,k=0;

    int shi=0;
    while(i<N&&j<N)
    {
    shi=0;
    for(k=0;k<node[no2[j]].n;k++)
    {
    if(strcmp(f[no[i]].str,node[no2[j]].name[k])==0)
    {
    shi=1;
    break;
    }
    }

    if(shi==1)
    {
    i++;
    }
    else
    j++;
    }
    if(i==N)return 0;//s失败
    else return 1;

    }

    void dfs2(int step)
    {
    int i;

    if(ok2==1)return;

    if(step==N)
    {
    if(bi()==0)//失败返回
    {
    ok2=1;
    return ;
    }

    }

    for(i=0;i<N;i++)
    {
    if(ok2==1)return;
    if(hash2[i]!=0)continue;

    no2[step]=i;
    hash2[i]=1;
    dfs2(step+1);
    hash2[i]=0;
    }
    }


    void dfs(int step)
    {
    int i;
    if(ok==1)
    return ;

    if(step==N)
    {

    ok2=0;
    dfs2(0);
    if(ok2==0)//成功
    {
    ok=1;return ;
    }
    }

    for(i=0;i<N;i++)
    {
    if(ok==1)
    return ;
    if(hash[i]!=0)continue;

    no[step]=i;
    hash[i]=1;
    dfs(step+1);
    hash[i]=0;
    }
    }

    int main()
    {
    int t,add=0;
    scanf("%d",&t);

    while(t--)
    {
    add++;
    int n,i,j;
    scanf("%d",&n);
    N=n;

    getchar();
    for(i=0;i<n;i++)
    {
    scanf("%s",f[i].str);
    }

    for(i=0;i<n;i++)
    {
    scanf("%d",&node[i].n);
    getchar();

    for(j=0;j<node[i].n;j++)
    {
    scanf("%s",node[i].name[j]);
    }
    }

    sort(&f[0],&f[n],cmp);

    memset(hash,0,sizeof(hash));
    memset(hash2,0,sizeof(hash2));
    ok=0;

    dfs(0);

    printf("Case %d: ",add);
    if(ok==1)
    {
    printf("Yes\n");
    printf("%s",f[no[0]].str);
    for(i=1;i<N;i++)
    {
    printf(" %s",f[no[i]].str);
    }
    printf("\n");
    }
    else
    printf("No\n");
    }

    return 0;
    }
  • 相关阅读:
    Windows SDK 之 mciSendString最后一个参数
    java常用包下载地址(非maven)
    windows api(GDI)实现图片旋转
    windows sdk版本 之 并查集生成迷宫
    自签https证书2(适配新版chrome,不会显示“不安全”)
    数据结构——栈(Stacks)
    数据结构——表(list)
    数据结构——链表(linkedlist)
    解题报告1010 诡秘的余数
    函数体中用指针返回数组的方法
  • 原文地址:https://www.cnblogs.com/huhuuu/p/2203039.html
Copyright © 2011-2022 走看看