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.
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?
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)
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;
}
本文介绍了一个关于三国题材的英雄对决游戏策略算法问题。玩家需要安排己方英雄的出场顺序,以确保无论对手如何排列都能获胜。文章提供了输入输出样例及解析,并附带了完整的C语言实现代码。
215

被折叠的 条评论
为什么被折叠?



