The main topic: the classic water-pouring problem. Give you three bottles, volume is a,b,c.
At first, a, B is empty, C is full, now requires you to out of volume d of water. The rule for pouring water is to either pour the water side empty, or the water is filled
Ask if the volume is D, the minimum amount of water poured, if you can't pour out the volume of D, find d ' < D, the closest d ' and the smallest volume
Problem-solving ideas: just thought directly bfs, with a vis mark the end, the result WA. Why WA, because I'm asking for the least amount of water to pour, not the least water volume, WA is sure.
The VIS array is then converted to int, and the volume of water poured when the record reaches this state, the result can be thought of by the tle (perhaps I write rubbing. )
Learn from others, suddenly, with an array to represent the volume of water poured down the smallest volume of water, so you can reduce a lot of cases, is indeed a big pruning
#include <cstdio>#include <cstring>#include <queue>using namespace STD;#define N#define INF 0x3f3f3f3fstructnode{inthave[3];intD;} N1, N2;intDone[n], val[3];intDBOOLVis[n][n];voidInit () {memset(Vis,0,sizeof(VIS));memset(Done,0x3f,sizeof(done));scanf("%d%d%d%d", &val[0], &val[1], &val[2], &d); done[0] = done[val[2]] =0;}voidBFS () { queue<Node>Q; vis[0][0] =true; n1.have[0] = n1.have[1] = N1.D =0; n1.have[2] = val[2]; Q.push (N1); while(! Q.empty ()) {n1 = Q.front (); Q.pop (); for(inti =0; I <3; i++) for(intj =0; J <3; J + +) {if(i ^ j) {n2 = n1;intTMP = Val[j]-N2.HAVE[J] < N2.have[i]? VAL[J]-n2.have[j]: n2.have[i]; N2.HAVE[J] + = tmp; N2.have[i]-= tmp; N2.D + = tmp;if(!vis[n2.have[0]][n2.have[1]] || done[n2.have[0]] > N2.D | | done[n2.have[1]] > N2.D | | done[n2.have[2]] > n2.d) {vis[n2.have[0]][n2.have[1]] =true; for(intK =0; K <3; k++) {Done[n2.have[k]] = min (Done[n2.have[k]], n2.d); } q.push (N2); } } } }}voidSolve () {BFS (); for(inti = D; I >=0; i--)if(Done[i]! = INF) {printf("%d%d\n", Done[i], i); Break; }}intMain () {intTestscanf("%d", &test); while(test--) {init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-10603 Fill (implicit figure Search)