转载请注明出处,谢谢http://blog.csdn.net/ZEROcmm?viewmode=contents by---ZEROm
http://acm.hdu.edu.cn/showproblem.php?pid=4536
题目看起来好像很不好做,其实就是搜索,因为每次恐慌值会对多个国家增加,而只会减少一个国家的恐慌值,而且超过5就结束,所以搜索深度不会很深,直接用广搜做就行了,再注意一下细节就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct Node
{
int z,d;
}node[20];
int g[105][3];
int n,m,k;
struct DD
{
int kh[20],cnt;
};
queue<DD>q;
int bfs()
{
while(!q.empty())q.pop();
DD D,ind;
D.cnt=0;
for(int i=0;i<n;++i)D.kh[i]=node[i].d;
q.push(D);
int ans=0;
while(!q.empty())
{
D=q.front();
q.pop();
if(D.cnt>ans)ans=D.cnt;
if(D.cnt==k)return ans;
for(int i=0;i<3;++i)
{
ind=D;
int flag=1;
ind.kh[g[ind.cnt][i]]-=2;
if(ind.kh[g[ind.cnt][i]]<1)ind.kh[g[ind.cnt][i]]=1;
for(int j=0;j<3;++j)
{
if(j!=i)
{
int oo=g[ind.cnt][j];
++ind.kh[oo];
if(ind.kh[oo]>5)flag=0;
oo=node[oo].z;
for(int p=0;p<n;++p)
if(node[p].z==oo)
{
++ind.kh[p];
if(ind.kh[p]>5)flag=0;
}
}
}
++ind.cnt;
if(flag)q.push(ind);
}
}
return ans;
}
int main()
{
int t,Case=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;++i)scanf("%d",&node[i].z);
for(int i=0;i<n;++i)scanf("%d",&node[i].d);
for(int i=0;i<k;++i)scanf("%d%d%d",&g[i][0],&g[i][1],&g[i][2]);
printf("Case #%d: %d\n",++Case,bfs());
}
return 0;
}