Test instructions: Three cups capacity is a,b,c, and now C is full, A and B is empty, two cups i to J pour water, or I pour out J is not full, or J full I also have surplus, asked to reach a cup of water for D when the total amount of water poured to the minimum? If D cannot be reached, find a solution less than D and closest to D.
Idea: pour water problem, but the topic is the total to the water, so in the BFS arrived at the state also to check the update, may currently I did reach D to use the sum of water, but there may be a smaller solution than sum. There are many codes on the web that are wrong and many are wrong here.
Code:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 202;con St int MAXN = 2005;constint MAXM = 200010;const int N = 1005;struct node{int sum; int val[3];}; int vis[maxn][maxn];//Determines whether the state has been accessed by int a[3],d;int Ans[maxn];//ans[i] The minimum amount of water transferred when the water volume of the cup reaches I, void BFs () {Node st,now; memset (vis,0,sizeof (VIS)); memset (ans,inf,sizeof (ans)); st.val[0]=0,st.val[1]=0,st.val[2]=a[2],st.sum=0; Vis[0][0]=1; ans[0]=ans[a[2]]=0; queue<node>q; Q.push (ST); while (! Q.empty ()) {St=q.front (); Q.pop (); for (int i=0;i<3;i++)//i to J inside pour water {for (int j=0;j<3;j++) {if (i==j| | st.val[i]==0| | ST.VAL[J]==A[J]) continue; NOW.VAL[0]=ST.VAL[0]; NOW.VAL[1]=ST.VAL[1]; NOW.VAL[2]=ST.VAL[2]; Now.sum=st.sum; int X=A[J]-NOW.VAL[J]; The amount of water inside the IF (now.val[i]>=x)//i can pour j full {now.val[i]-=x; NOW.VAL[J]=A[J]; Now.sum+=x; } ELSE//need to pour out {now.val[j]+=now.val[i]; Now.sum+=now.val[i]; now.val[i]=0; } if (!vis[now.val[0]][now.val[1]])//This state has not reached {VIS[NOW.VAL[0]][NOW.V Al[1]]=1; for (int k=0;k<3;k++)//update answer if (ans[now.val[k]]>now.sum) ans[no W.val[k]]=now.sum; Q.push (now); } else//Although this state was previously accessed, you still need to check if you can get a better solution {for (int k=0;k<3;k++) {if (ans[now.val[k]]>now.sum) { Ans[now.val[k]]=now.sum; Q.push (now); }}}}}}}int main () {#ifndef Online_judge freopen ("c:/users/ly F/desktop/in.txt "," R ", stdin); #endif inT i,j,t; scanf ("%d", &t); while (t--) {scanf ("%d%d%d%d", &a[0],&a[1],&a[2],&d); BFS (); for (i=d;i>=0;i--) {if (Ans[i]<inf) {printf ("%d%d\n", ans[i],i); Break }}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fill (Uva 10603 bfs pour water problem)