首先这是一道经典的搜索题目,的确十分经典。
其次网上的解答大多数都是DFS的方法,
于是,
为了解救在深搜中沉迷自我的大众们
我
“freopen。”
将广搜的代码公布于此
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int x[
2];
node(){}
node(
int a,
int b){
x[
0]=a;
x[
1]=b;
}
};
queue<node>m;
bool f[
21][
21],l[
21];
int main()
{
freopen(
"cow.in",
"r",stdin);
freopen(
"cow.out",
"w",stdout);
int a[
3];
scanf(
"%d%d%d",&a[
0],&a[
1],&a[
2]);
m.push(node(
0,
0));
while(!m.empty())
{
int k[
3];
k[
0]=m.front().x[
0];
k[
1]=m.front().x[
1];
k[
2]=a[
2]-k[
0]-k[
1];
if(k[
0]==
0)
l[k[
2]]=
1;
for(
int i=
0;i<=
2;i++)
for(
int j=
0;j<=
2;j++)
{
if(i==j)
continue;
int h;
if(a[j]-k[j]>k[i])
h=k[i];
else
h=a[j]-k[j];
k[i]-=h;
k[j]+=h;
if(f[k[
0]][k[
1]]==
0)
{
m.push(node(k[
0],k[
1]));
f[k[
0]][k[
1]]=
1;
}
k[i]+=h;
k[j]-=h;
}
m.pop();
}
for(
int i=
0;i<a[
2];i++)
{
if(l[i]==
1)
printf(
"%d ",i);
}
printf(
"%d",a[
2]);
}
转载请注明原文地址: https://ju.6miu.com/read-1124717.html