Pour oil problem, breadth optimization search, Java

Source: Internet
Author: User

There is a chef from 12 pounds of oil (a barrel) of the barrels poured out 6 kilograms of oil, but only 8 pounds of oil (b barrels) and 5 pounds of oil (c barrels) Two barrels, asked how to operate to be 6 pounds out of it?
Class Dumpoilbfs:
Import Cn.hncu.sreach.putoil.common.bucket;import Cn.hncu.sreach.putoil.common.dumpcase;import cn.hncu.sreach.putoil.common.myset;/* a chef to from 12 pounds of oil (a barrel) of the barrels poured out 6 kilograms of oil, but only 8 pounds of oil (b barrels) and 5 pounds of oil (c barrels) Two barrels, asked how to operate to be 6 pounds out of it? * The breadth of the problem to optimize the search solution: every oil, there may be 6 kinds of programs, each one to judge if there is no 6 catty situation, if it appears on the record, and then continue to search, until all the circumstances of the search finished. * Detailed comments are below. */public class Dumpoilbfs {public static void main (string[] args) {bucket[] buckets = new bucket[3];//new array length is 3, representing 3 barrels Buc Kets[0] = new Bucket (12, 12); The first bucket of properties, the size of the bucket of 12 pounds, the existing 12 Jin buckets[1] = new Bucket (8, 0); The first bucket of properties, the size of the bucket of 8 pounds, the existing 0 Jin buckets[2] = new Bucket (5, 0); The first bucket of properties, the size of the bucket is 5 pounds, the existing 0 Jin dumpcase u = new dumpcase (buckets); New duampcase, used to represent the state of three buckets now myset caseset = new MySet (); New MySet, used to indicate the state of the three buckets that have occurred, followed by steps to prevent repetition caseset.add (u); Casequeue que = new casequeue (); A queue that is used to load the state of the current three barrels and the state of the caseset at this time que.enqueue (U, Caseset); BFs (que); Go to breadth optimization search}//breadth optimization search private static void BFs (Casequeue que) {while (!que.isempty ()) {///If the queue is empty, exit q q = Que.dequeque () directly; Remove an element from a queue qdumpcase u = qUe.getdumpcase (q); Extracting Dumpcasemyset Caseset = Que.getmyset (q) from Q; Extract the dumpcase//from Q to determine if there is a bucket of oil for 6 Jin, exit the loop if (U.buckets[0].now = = 6 | | u.buckets[1].now = = 6) {print (U, caseset);//Output path C Ontinue; There is no break here, otherwise it will only come out a situation//break;}  Dumpcase temp = new Dumpcase (u);//backup here once so that later restore//here is the core for (int i = 0; i < temp.buckets.length; i++) {for (int j = 0; J < Temp.buckets.length; J + +) {if (i = = j) {//cannot pour continue himself and herself;} int incandump = Temp.buckets[i].canout ();  See how much this can be poured if (Incandump > Temp.buckets[j].canin ()) {//To determine whether the amount of the can pour is greater than the amount received Incandump = Temp.buckets[j].canin (); If it is greater than that, then you can pour out all I}if (incandump = = 0) {//If you pour in continue;} Begin to pour the oil, reduce the reduction, the addition of Temp.buckets[i].out (Incandump), temp.buckets[j].in (Incandump),//To determine whether or not before the end of the case if ( Caseset.contains (temp)) {//If present, restore temp.buckets[i].in (incandump); Temp.buckets[j].out (incandump); continue;}  Dumpcase v = new Dumpcase (temp); v.parent = u; Record the parent node Caseset.add (v);//record to the set of nodes that have been searched Que.enqueue (V, caseset);//Add to the wide search queue//must be restored in order to continue to try other paths from this node temp.buckets[I].in (Incandump); Temp.buckets[j].out (Incandump);}}} Output path (output in reverse order), the next need to output is the previous parent node private static void Print (Dumpcase u, myset caseset) {MySet set = new MySet (); Set.add (U);D Umpcase d = u.parent;while (d! = null) {Set.add (d);d = D.parent;} object[] obj = Set.getall (); for (int i = obj.length-1; I >= 0; i--) {if (i > 0) {System.out.print (Obj[i] + "---&G T;> ");} else {System.out.println (obj[i]);}}} Object for loading dumpcase and MySet class Q {public dumpcase d;public MySet m;//Constructor public Q (Dumpcase D, MySet m) {this.d = D;THIS.M = n EW MySet (m);} Public Dumpcase getd () {return D;} Public MySet Getm () {return m;}} Queue (elements in the queue are objects bundled with Dumpcase and MySet) class Casequeue {private q[] qs = new q[100];D umpcase u = null; MySet set = Null;int end = 0; A queue pointer representing the number of elements in the queue//into the queue public int enqueue (dumpcase u, myset caseset) {qs[end++] = new Q (u, caseset); return end;} Public Dumpcase getdumpcase (q q) {return q.d;} Public MySet Getmyset (q q) {return q.m;} Out Queue public Q Dequeque () {if (IsEmpty ()) {return null;} Q q = qs[0]; Each time is the first element of the popup queue//All elements move forward one if (end > 1) {for (int i = 0; i < end; i++) {Qs[i] = qs[i + 1];}} End--;return Q;} Determines whether null public boolean isEmpty () {if (end = = 0) {return true;} return false;}}

Class Buckets:
public class Buckets {public int max;//represents the maximum capacity of a bucket public int now;//= number of oil in the current bucket//constructor public bucket (int max, int. now) {This.ma x = Max;this.now = Now;} The current maximum number of oil public int canIn () {return max-now;} Indicates the current maximum amount of oil that can be poured public int canout () {return now;} Indicates how much oil was added public void in (int a) {now + = A;} Indicates poured out, how much oil public void out (int. a) {now-= A;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + Max;result = Prime * result + Now;return result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; Bucket other = (bucket) obj;if (max! = Other.max) return false;if (now! = Other.now) return False;return true;}}


Class Dumpcase:
Import Java.util.arrays;public class Dumpcase {public Bucket buckets[] =null;public dumpcase parent=null;public DumpCase (Bucket buckets[]) {this.buckets = buckets;} Copy construction (here to be very careful to prevent bundling) public dumpcase (Dumpcase u) {buckets = new bucket[u.buckets.length];//Copy the elements of U into the new buckets for (int i=0;i<u.buckets.length;i++) {Buckets[i] = new Bucket (0,0); Buckets[i].max = U.buckets[i].max;buckets[i].now = U.buckets[i].now;}} @Overridepublic String toString () {return "a=" +buckets[0].now+ "b=" +buckets[1].now+ "c=" +buckets[2].now;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + arrays.hashcode (buckets); RET Urn result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false;dumpcase other = (dumpcase) obj;if (! Arrays.equals (buckets, other.buckets)) return False;return true;}}
Class MySet:
public class MySet {private object[] Objs = new object[0];//copy construction (prevents bundling) This is not the first time to write, because after debugging found that the total number of paths can not be,// This is to make a copy of the current state of Caseset each time it joins the queue, but there is still no output of all the values public MySet (MySet m) {objs = new Object[m.getall (). length];for (int i = 0; i < objs.length; i++) {Objs[i] = new Object (); Objs[i] = M.getall () [i];}} Public MySet () {}//Add function public boolean add (Object obj) {if (contains (obj)) {//If there are duplicates, do not put in return false;} Object tempobjs[] = new Object[objs.length + 1];//with dynamic array system.arraycopy (OBJS, 0, TEMPOBJS, 0, objs.length); tempobjs[obj S.length] = Obj;objs = Tempobjs;return true;} Determines whether there is a duplicate element public boolean contains (object obj) {for (object O:objs) {if (o.equals (obj)) {return true;}} return false;} Gets all the elements in MySet public object[] GetAll () {return OBJS;} Gets the number of elements of the MySet public int size () {return objs.length;}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pour oil problem, breadth optimization search, Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.