dfs

用dfs时就说一个问题有很多层,重这一层到下一层用dfs,一层跑完后retuen,然后回到原来的状态,如果一层中有多个情况,就需要跑一个循环

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
int res[5],t[5],ans;
int a[]={0,1,1,1,2,2,3};
int b[]={0,2,3,4,3,4,4};
void dfs(int d)
{
if (d==7)
{
int flag=1;
for (int i=1;i<=4;i++)
{
if (res[i]!=t[i])
{
flag=0;
break;
}
}
if (flag)
{
ans++;
return;
}
}
else
{
for (int k=0;k<3;k++)
{
if (k==0)
{
t[a[d]]+=3;
t[b[d]]-=0;
dfs(d+1);
t[a[d]]-=3;
t[b[d]]+=0;
}
if (k==1)
{
t[a[d]]+=1;
t[b[d]]+=1;
dfs(d+1);
t[a[d]]-=1;
t[b[d]]-=1;
}
if (k==2)
{
t[a[d]]-=0;
t[b[d]]+=3;
dfs(d+1);
t[a[d]]+=0;
t[b[d]]-=3;
}
}
}
}
int main()
{
int T,kase=0;
cin>>T;
while (T--)
{
memset(t,0,sizeof(t));
scanf("%d%d%d%d",&res[1],&res[2],&res[3],&res[4]);
ans=0;
dfs(1);
cout<<"Case #"<<++kase<<": ";
if (ans==0)
cout<<"Wrong Scoreboard"<<endl;
else if (ans==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}

posted @ 2019-07-23 16:34  啊哈啦  阅读(174)  评论(0编辑  收藏  举报