水杯问题队列判断

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 
 6 const int max=100;
 7 int c[3];
 8 int w;
 9 int vis[max][max];
10 int m;
11 typedef struct 
12 {
13     int v[3];
14     int deep;
15     //ruct node *fa;
16     //t a;
17     //t b;
18 }node;
19 
20 
21 int bfs()
22 {
23     int i,j,k;
24     node t,n;
25     queue <node> q;
26     n.deep=0;
27     n.v[0]=c[0];
28     n.v[1]=n.v[2]=0;
29     q.push(n);
30     while(!q.empty())
31     {
32         t=q.front();
33         if(t.v[0]==w||t.v[1]==w||t.v[2]==w)
34         {
35             printf("Yes %d\n",t.deep);
36             return 1;
37         }
38         for(i=0;i<3;i++)
39         {
40             for(j=0;j<3;j++)
41             {
42                 if(i==j) continue;
43                 m=t.v[i]<c[j]-t.v[j]?t.v[i]:c[j]-t.v[j];
44                 n=t;
45                 n.v[i]-=m;
46                 n.v[j]+=m;
47                 if(!vis[n.v[1]][n.v[2]])
48                 {
49                     vis[n.v[1]][n.v[2]]=1;
50                     n.deep++;
51                     q.push(n);
52                 }
53             }
54         }
55         k=q.size();
56         q.pop();
57         k=q.size();
58     }
59     return 0;
60 }
61 
62 
63 int main()
64 {
65     while(scanf("%d%d%d%d",&c[0],&c[1],&c[2],&w)!=EOF)
66     {
67         memset(vis,0,sizeof vis);
68 
69         if(!bfs()) printf("No\n");
70     }
71     return 0;
72 }
原文地址:https://www.cnblogs.com/xiaofanke/p/2998432.html