1. 程式人生 > 實用技巧 >hdu 1495 非常可樂

hdu 1495 非常可樂

題目連結: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