首页 > 其他 > 详细

hdu 1495 非常可乐

时间:2020-07-25 22:16:25      阅读:72      评论:0      收藏:0      [点我收藏+]

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

题意:给三个杯子 体积为 s,n,m 刚开始水都装在s中 三个杯子都没有刻度 问存在操作使得任意两个杯子中的水体积相同吗 输出最少操作数

思路:正常想无法计算怎么才是最优解 又是要最少操作数 考虑bfs   数据范围也满足开三维vis 存储状态 只有s-n s-m n-m n-s m-n m-s 六种状态

技术分享图片
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define ll long long
  4 #define ull unsigned long long
  5 #define pb push_back
  6 const int maxn=2e5+10;
  7 const int mod=1e9+7;
  8 int vis[105][105][105];
  9 int s,n,m;
 10 struct ac
 11 {
 12     int s,n,m,step;
 13 };
 14 
 15 
 16 int main()
 17 {
 18     ios::sync_with_stdio(false);
 19     cin.tie(0);
 20     while(cin>>s>>n>>m)
 21     {
 22         if(s==0&&n==0&&m==0)
 23             break;
 24         if(s%2)
 25         {
 26             cout<<"NO"<<\n;
 27             continue;
 28         }
 29         memset(vis,0,sizeof(vis));
 30         queue<ac>q;
 31         q.push({s,0,0,0});
 32         vis[s][0][0]=1;
 33         int ans=-1;
 34         while(!q.empty())
 35         {
 36             ac u=q.front();
 37             q.pop();
 38             if((u.m==s/2&&u.n==s/2)||(u.m==s/2&&u.s==s/2)||(u.n==s/2&&u.s==s/2))
 39             {
 40                 ans=u.step;
 41                 break;
 42             }
 43             if(u.step>10000)
 44             {
 45                 break;
 46             }
 47             if(u.s>=n-u.n) // s n
 48             {
 49                 int ts=u.s-(n-u.n);
 50                 int tn=n;
 51                 int tm=u.m;
 52                 if(vis[ts][tn][tm]==0)
 53                 {
 54                     q.push({ts,tn,tm,u.step+1});
 55                     vis[ts][tn][tm]=1;
 56                 }
 57             }
 58             else
 59             {
 60                 int ts=0;
 61                 int tn=u.n+u.s;
 62                 int tm=u.m;
 63                 if(vis[ts][tn][tm]==0)
 64                 {
 65                     q.push({ts,tn,tm,u.step+1});
 66                     vis[ts][tn][tm]=1;
 67                 }
 68             }
 69             if(u.s>=m-u.m) // s m
 70             {
 71                 int ts=u.s-(m-u.m);
 72                 int tn=u.n;
 73                 int tm=m;
 74                 if(vis[ts][tn][tm]==0)
 75                 {
 76                     q.push({ts,tn,tm,u.step+1});
 77                     vis[ts][tn][tm]=1;
 78                 }
 79             }
 80             else
 81             {
 82                 int ts=0;
 83                 int tn=u.n;
 84                 int tm=u.m+u.s;
 85                 if(vis[ts][tn][tm]==0)
 86                 {
 87                     q.push({ts,tn,tm,u.step+1});
 88                     vis[ts][tn][tm]=1;
 89                 }
 90             }
 91             if(u.n>=s-u.s) // n s
 92             {
 93                 int ts=s;
 94                 int tn=u.n-(s-u.s);
 95                 int tm=u.m;
 96                 if(vis[ts][tn][tm]==0)
 97                 {
 98                     q.push({ts,tn,tm,u.step+1});
 99                     vis[ts][tn][tm]=1;
100                 }
101             }
102             else
103             {
104                 int ts=u.s+u.n;
105                 int tn=0;
106                 int tm=u.m;
107                 if(vis[ts][tn][tm]==0)
108                 {
109                     q.push({ts,tn,tm,u.step+1});
110                     vis[ts][tn][tm]=1;
111                 }
112             }
113             if(u.n>=m-u.m) // n m
114              {
115                 int ts=u.s;
116                 int tn=u.n-(m-u.m);
117                 int tm=m;
118                 if(vis[ts][tn][tm]==0)
119                 {
120                     q.push({ts,tn,tm,u.step+1});
121                     vis[ts][tn][tm]=1;
122                 }
123             }
124             else
125             {
126                 int ts=u.s;
127                 int tn=0;
128                 int tm=u.m+u.n;
129                 if(vis[ts][tn][tm]==0)
130                 {
131                     q.push({ts,tn,tm,u.step+1});
132                     vis[ts][tn][tm]=1;
133                 }
134             }
135             if(u.m>=s-u.s) // m s
136             {
137                 int ts=s;
138                 int tn=n;
139                 int tm=u.m-(s-u.s);
140                 if(vis[ts][tn][tm]==0)
141                 {
142                     q.push({ts,tn,tm,u.step+1});
143                     vis[ts][tn][tm]=1;
144                 }
145             }
146             else
147             {
148                 int ts=u.s+u.m;
149                 int tn=u.n;
150                 int tm=0;
151                 if(vis[ts][tn][tm]==0)
152                 {
153                     q.push({ts,tn,tm,u.step+1});
154                     vis[ts][tn][tm]=1;
155                 }
156             }
157             if(u.m>=n-u.n) // m n
158             {
159                 int ts=u.s;
160                 int tn=n;
161                 int tm=u.m-(n-u.n);
162                 if(vis[ts][tn][tm]==0)
163                 {
164                     q.push({ts,tn,tm,u.step+1});
165                     vis[ts][tn][tm]=1;
166                 }
167             }
168             else
169             {
170                 int ts=u.s;
171                 int tn=u.n+u.m;
172                 int tm=0;
173                 if(vis[ts][tn][tm]==0)
174                 {
175                     q.push({ts,tn,tm,u.step+1});
176                     vis[ts][tn][tm]=1;
177                 }
178             }
179 
180         }
181         if(ans==-1)
182             cout<<"NO"<<\n;
183         else
184             cout<<ans<<\n;
185     }
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 }
View Code

 

hdu 1495 非常可乐

原文:https://www.cnblogs.com/winfor/p/13377660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!