Original question:
There are 4 bottle of red wine, their capacity is: 9 liters, 7 liters, 4 liters, 2 liters
the beginning of the state is [9,0,0,0], that is to say: The first bottle is full, the others are empty.
allow the wine to be poured from one bottle to another, but only fill a bottle or empty a bottle, not in a neutral state. Such a pour wine action is called 1 operations.
assuming that the capacity and initial state of the bottle are constant, at least how many operations are required to achieve a given target state?
the subject is the calculation that requires you to program to achieve the minimum number of operations.
Input: Final state (comma delimited)
output: Minimum number of operations (if not implemented, output-1)
For example:
Input:
9,0,0,0
should output:
0
Input:
6,0,0,3
should output:
-1
Input:
7,2,0,0
should output:
2
Idea: With my last blog is the same idea. BFS Breadth First search. In the status class, the last value is changed to a storage count because no print path is required. Change the bottle to 4, the capacity is determined. If you don't know, you can read my last blog first.
Java code:
Package Package1;import java.util.*;p ublic class t201207_03 {public static void main (string[] args) {Scanner cin = new SCA Nner (system.in);//Determine initial state int[] s = new Int[4];for (int i = 0;i<s.length;i++) s[i] = Cin.nextint (); ZT start = new ZT (s,0);//OK End State int[] ans = new Int[4];for (int i = 0;i<ans.length;i++) ans[i] = Cin.nextint (); ZT anszt = new ZT (ans,-1);//create queue, initial state queued queue<zt> queue = new linkedlist<zt> (); Queue.add (start);// Declares the team intermediate variable ZT temp = null; Zt newzt = Null;int p = 0;if (Start.equals (Anszt)) {SOP ("last =" +start.last); return;} while (!queue.isempty ()) {//out of queue temp = Queue.poll (); Temp.show ();//pour wine,1->2,1->3,1->4//2->1,2->3,2-> 4//3->1,3->2,3->4//<span style= "White-space:pre" ></span>4->1,4->2,4->3for (int i = 0; i<3;i++) for (int j = 0;j<3;j++) {//equal skip if (i = = j) continue;//generate a new state NEWZT = Temp.move (i,j,temp.last+1);//NULL to skip if ( NEWZT = = null) continue;queue.add (NEWZT), if (Newzt.equals (Anszt)) {//newzt.show (); SOP ("last =" +newzt.last); rEturn;}}} SOP ("-1");} private static void Sop (String string) {System.out.println (string);}} state classes class zt{//static capacity public static int[] RL = {9,7,4,2};//record count int last;//current state int[] Zt = new Int[4]; Zt (int[] Pz,int p) {last = p;for (int i = 0;i<zt.length;i++) zt[i] = Pz[i];} Determines whether the key value exists in the current state public int indexOf (int key) {for (int i = 0;i<zt.length;i++) {if (zt[i] = = key) return i;} return-1;} Place the wine of I into J in public Zt move (int i,int j,int p) {//Compare the remainder of the I bottle and the idle amount of the J bottle//Establish a new state int[] Newzt = new Int[4];for (int k = 0;k<zt.length ; k++) {newzt[k] = zt[k];} ZT newzt = new ZT (newzt,p);//cannot pour wine back nullif (newzt.zt[i] = = 0| | NEWZT.ZT[J] = [newzt.rl[j]) return null;if (newzt.zt[i]<= newzt.rl[j]-newzt.zt[j]) {//i's capacity is less than the remainder of J, I empty, j + = Inewzt.zt [j] + = newzt.zt[i];newzt.zt[i] = 0;return newzt;} Else{//i of alcohol is greater than the remaining amount of J, filled with J I have the remaining newzt.zt[i]-= (Newzt.rl[j]-newzt.zt[j]); Newzt.zt[j] = Newzt.rl[j];return newzt;}} public void Show () {System.out.print ("ZT = ' +this.last+"); for (int i = 0;i<zt.length;i++) System.out.print (zt[i]+ "," ); SOP ("");} Public BoOlean equals (Object arg0) {Zt e = (ZT) arg0;for (int i = 0;i<zt.length;i++) {if (Zt[i]!=e.zt[i]) return false;} return true;} private static void Sop (String string) {System.out.println (string);}}
Blue Bridge Cup final wine of the past