Time limit of three cups: +Ms | Memory Limit:65535KB Difficulty:4
-
-
Describe
-
Give three cups, different sizes, and only the largest cup of water is filled, the remaining two are empty cups. Three cups of water are poured between each other, and the cups are not identified and can only be calculated according to the volume of the cups given. You are now asked to write a program that outputs the minimum number of times that the initial state reaches the target State.
-
-
Input
-
-
The first line an integer n (0<n<50) represents the n set of test data
Next, each set of test data has two lines, the first line gives three integers V1 V2 V3 (v1>v2>v3 v1<100 v3>0) represents the volume of three cups.
The second line gives three integers E1 E2 E3 (volume less than equal to the corresponding Cup volume) represents the final state we need
-
-
Output
-
-
each row outputs the minimum number of water pours for the corresponding test data. If the target status output is not reached-1
-
-
Sample input
-
-
26 3 14 1 19 3 27 1 1
-
-
Sample output
-
-
3-1
-
-
Source
-
-
Classic Topics
-
Uploaded by
Hzyqazasdf
Reference Links:
http://blog.csdn.net/code_pang/article/details/7802944
Http://www.cnblogs.com/jiaolinfengacm/archive/2011/12/15/2288334.html
The code is as follows:
#include <stdio.h> #include <string.h> #include <queue>using namespace std;typedef struct {int cur[3]; int vol[3];int Step;} Node;node begin,end;bool Visit[1000100];queue<node>q;int Find (node U,node v) {return (u.cur[0]==v.cur[0]& &U.CUR[1]==V.CUR[1]&&U.CUR[2]==V.CUR[2]);} int Check (node u) {return (u.cur[0]*10000+u.cur[1]*100+u.cur[2]);} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d%d", &begin.vol[0],&begin.vol[1],&begin.vol [2]); BEGIN.CUR[0]=BEGIN.VOL[0];BEGIN.CUR[1]=BEGIN.CUR[2]=0;SCANF ("%d%d%d",&end.cur[0],&end.cur[1],& END.CUR[2]); memset (visit,0,sizeof (visit)); Q.push (begin); Visit[check (begin)]=1;int Ok=0;while (!q.empty ()) {node u= Q.front (); Q.pop (); if (find (U,end)) {ok=1;printf ("%d\n", u.step); break;} for (int i=0;i<=2;++i) {for (int j=0;j<=2;++j) {if (i!=j) {int min=u.vol[j]-u.cur[j];//allowed space remaining if (Min>u.cur[i]) min=u.cur[i];//Prevent negative node v=u;v.cur[i]-=min;v.cur[j]+=min;v.step=u.step+1;if (!visit[check (v)]) {Visit[check (v)]= 1;q.Push (v);}}}} if (!ok) {printf (" -1\n");} while (!q.empty ())//q.pop ();} return 0;}
Queue, wide search Nyoj 213 water Cups