天天看點

hdu 4068 SanguoSHA

Problem Description Sanguosha has a singled version. Two players each select N heroes and start fighting. If a hero dies, the next follows. If one player's heroes are all dead, he loses.

hdu 4068 SanguoSHA

There're restraints among heroes. For example, YuJi restricts Zhu Geliang, LuXun restricts DaQiao, ZhangJiao restricts MaChao, WeiYan restricts XiaoQiao.

Today I play with friends. I know the heroes and the restraints.(If opponent's hero restraint my hero, my hero will be beaten, others my hero will beat opponent's hero)

Can you arrange my heroes' order,no matter what order of opponent's heroes, so that I can win the game?  

Input The first line is a number T(1<=T<=50), represents the number of case. The next T blocks follow each indicates a case.

The first line is N(3<=N<=6).

The second line has N names(shorter than 20 letter).

The following N lines each contains a restraints. Restraints are given as “k b1 b2 … bk”, which means the opponent's hero restricts my hero b1, b2 … bk. (0<=K<=N)  

Output For each case, first line output the number of case with "Yes" or "No". If Yes, output the order of your heroes separate by a space. If there are more than one order, please output the one with minimum lexicographic order.(as shown in the sample output)  

Sample Input

2
3
ZhugeLiang HuangYueying ZhenJi
1 ZhugeLiang
2 HuangYueying ZhenJi
2 ZhugeLiang ZhenJi
4
MaChao YanLiangWenChou YuJin XiaoQiao
2 MaChao XiaoQiao
2 YanLiangWenChou YuJin
1 XiaoQiao
0
        

Sample Output

Case 1: No
Case 2: Yes
MaChao YanLiangWenChou XiaoQiao YuJin
        

話說之前原來是想道歉說我帶了某些集訓隊的人玩1V1,但是我發現這還是明智的,至少不會在題意上花太多時間(ps:今天各種遊戲各種題意了解不能……英語好水)

兩邊,一邊最多六個人,枚舉所有情況就是6!*6!,不會逾時,兩邊都DFS直接搞即可。

代碼

#include <stdio.h>
#include <string.h>

char str[10][30];
int map[10][10];
char s[30];
int vis[10];
int n;
char ans[10][30];
char ret[10][30];
int ord[10];
int vis2[10];
int dr[10];
int ok,bj;

bool Check()
{
    int i,j;
    i=j=0;
    while(i<n && j<n)
    {
        if (map[dr[i]][ord[j]]==1) j++;
        else i++;
    }
    if (i==n) return true;
    return false;
}

bool DFS2(int t)
{
    int i,j;
    if (t==n)
    {
        if (Check()==false)
        {
            bj=1;
            return false;
        }
    }
    for (i=0;i<n && bj==0;i++)
    {
        if (vis2[i]==false)
        {
            dr[t]=i;
            vis2[i]=1;
            DFS2(t+1);
            vis2[i]=0;
        }
    }
    if (bj==1) return false;
    return true;
}

void DFS(int t)
{
    int i,j;
    if (t==n)
    {
        bj=0;
        if (DFS2(0)==true)
        {
            if (ok==0)
            {
                for (i=0;i<n;i++)
                {
                    strcpy(ans[i],ret[i]);
                }
                ok=1;
                return;
            }
            for (i=0;i<n;i++)
            {
                if (strcmp(ret[i],ans[i])<0)
                {
                    for (j=0;j<n;j++)
                    {
                        strcpy(ans[j],ret[j]);
                    }
                    break;
                }
                else if (strcmp(ret[i],ans[i])>0)
                {
                    break;
                }
            }
        }
        return;
    }
    for (i=0;i<n;i++)
    {
        if (vis[i]==0)
        {
            vis[i]=1;
            strcpy(ret[t],str[i]);
            ord[t]=i;
            DFS(t+1);
            vis[i]=0;
        }
    }
}

int main()
{
    int i,j,T,k,cnt;
    scanf("%d",&T);
    cnt=1;
    while(T--)
    {
        scanf("%d",&n);
        for (i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }
        memset(map,0,sizeof(map));
        for (i=0;i<n;i++)
        {
            scanf("%d",&k);
            while(k--)
            {
                scanf("%s",s);
                for (j=0;j<n;j++)
                {
                    if (strcmp(str[j],s)==0) map[i][j]=1;
                }
            }
        }
        ok=0;
        DFS(0);
        printf("Case %d: ",cnt++);
        if (ok==1)
        {
            printf("Yes\n");
            for (i=0;i<n-1;i++)
            {
                printf("%s ",ans[i]);
            }
            printf("%s\n",ans[i]);
        }
        else printf("No\n");
    }
    return 0;
}
           

繼續閱讀