hdu_1495_非常可乐(bfs模拟)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495

题意:不解释

题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长,比较重复,尝试优化了一下,不过WA了难过

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 struct dt{int s,m,n,t;};
 6 int s,m,n;
 7 bool v[101][101][101];
 8 int bfs(){
 9     memset(v,0,sizeof(v));
10     v[s][0][0]=1;
11     dt tmp,o;tmp.s=s,tmp.m=0,tmp.n=0,tmp.t=0;
12     queue<dt>Q;Q.push(tmp);
13     while(!Q.empty()){
14         o=Q.front();Q.pop();
15         if((o.s==o.m&&!o.n)||(o.s==o.n&&!o.m)||(o.n==o.m&&!o.s))return o.t;
16         if(o.s){
17             if(o.m<m){
18                 if(o.s+o.m>=m){
19                     tmp.m=m,tmp.s=o.s+o.m-m,tmp.n=o.n,tmp.t=o.t+1;
20                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
21                 }else{
22                     tmp.m=o.m+o.s,tmp.s=0,tmp.n=o.n,tmp.t=o.t+1;
23                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
24                 }
25             }
26             if(o.n<n){
27                 if(o.s+o.n>=n){
28                     tmp.n=n,tmp.s=o.s+o.n-n,tmp.m=o.m,tmp.t=o.t+1;
29                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
30                 }else{
31                     tmp.n=o.n+o.s,tmp.s=0,tmp.m=o.m,tmp.t=o.t+1;
32                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
33                     }
34             }
35         }
36         if(o.m){
37             if(o.s<s){
38                 if(o.s+o.m>=s){
39                     tmp.s=s,tmp.m=o.m+o.s-s,tmp.n=o.n,tmp.t=o.t+1;
40                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
41                 }else{
42                     tmp.s=o.s+o.m,tmp.m=0,tmp.n=o.n,tmp.t=o.t+1;
43                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
44                 }
45             }
46             if(o.n<n){
47                 if(o.m+o.n>=n){
48                     tmp.n=n,tmp.m=o.m+o.n-n,tmp.s=o.s,tmp.t=o.t+1;
49                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
50                 }else{
51                     tmp.n=o.n+o.m,tmp.m=0,tmp.s=o.s,tmp.t=o.t+1;
52                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
53                 }
54             }
55         }
56         if(o.n){
57             if(o.m<m){
58                 if(o.n+o.m>=m){
59                     tmp.m=m,tmp.n=o.n+o.m-m,tmp.s=o.s,tmp.t=o.t+1;
60                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
61                 }else{
62                     tmp.m=o.m+o.n,tmp.n=0,tmp.s=o.s,tmp.t=o.t+1;
63                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
64                 }
65             }
66             if(o.s<s){
67                 if(o.n+o.s>=s){
68                     tmp.s=s,tmp.n=o.n+o.s-s,tmp.m=o.m,tmp.t=o.t+1;
69                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
70                 }else{
71                     tmp.s=o.s+o.n,tmp.n=0,tmp.m=o.m,tmp.t=o.t+1;
72                     if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=1;}
73                 }
74             }
75         }
76     }
77     return -1;
78 }
79 int main(){
80     while(~scanf("%d%d%d",&s,&m,&n),s+n+m){
81         if(s%2){puts("NO");continue;}
82         int ans=bfs();
83         if(ans==-1)puts("NO");
84         else printf("%d
",ans);
85     }
86     return 0;
87 }
View Code
原文地址:https://www.cnblogs.com/bin-gege/p/5696168.html