24-point Game Dynamic Programming solution (Java)

Source: Internet
Author: User


Algorithm principle reference Programming Beauty 1.16 Chapter Solution 2:

Below is the Java source code: The child problem is s[i] u= Fork (f (x), F (i));

Import Java.util.HashMap;
Import Java.util.HashSet;
Import Java.util.Map;
Import Java.util.Scanner;

Import Java.util.Set;
	Class elem{public double res;
	public String info;
		Public Elem (double r,string i) {res = R;
	info = i;
	} public class Calculateanswer {private final int N = 4;
	Private final int res = 24;
	Private int[] A;
	Private Map<integer, set<elem>> Map;
	
	Private set<string> answers;
		Public Calculateanswer (int[] a) {a = A;
		Map = new Hashmap<integer, set<elem>> ();

	Answers = new hashset<string> ();
			public void Run () {for (int i=0;i< (1<<n); i++) {set<elem> Set = new hashset<elem> ();
		Map.put (I,set);
			for (int i=0;i<n;i++) {Elem e = new Elem (a[i), a[i]+ "");
			set<elem> set = new hashset<elem> ();
			Set.add (e);
		Map.put (1<<i, set);
		for (int i = 1; i < (1 << N); i++) {fork (i);
		} set<elem> Mset = Map.get ((1<<n)-1); for (ElemE:mset) {if (e.res==res) {answers.add (e.info);
		} if (Answers.size () ==0) {System.out.println ("no solution");
			else {for (String s:answers) System.out.println (s);
		SYSTEM.OUT.PRINTLN ("Total" +answers.size () + "solution");
		} public set<elem> fork (int m) {set<elem> Mset = Map.get (m);
		if (Mset.size () >0) return mset;
					else {for (int x = 1; x <= m x + +) {if (x&m) ==x) {set<elem> S1 = fork (x);
					set<elem> s2 = fork (m-x);
							for (Elem e1:s1) {for (Elem e2:s2) {String str = "(" +e1.info+ "+" +e2.info+ ")";
							
							Mset.add (New Elem (E1.res+e2.res, str));
							str = "(" + e1.info+ "-" +e2.info+ ")";
							
							Mset.add (New Elem (E1.res-e2.res, str));
							str = "(" + e2.info+ "-" +e1.info+ ")";
							
							Mset.add (New Elem (E2.res-e1.res, str));
							str = "(" + e1.info+ "*" +e2.info+ ")";
							
							Mset.add (New Elem (E1.res*e2.res, str)); if (e1.res!=0) {str = "(" + e2.info+ "/"+e1.info+") ";
							Mset.add (New Elem (E2.res/e1.res, str));
								} if (e2.res!=0) {str = "(" + e1.info+ "/" +e2.info+ ")";
							Mset.add (New Elem (E1.res/e2.res, str));
		}}} return mset;
		} public static void Main (string[] args) {int[] a = new int[4];
		Scanner sc = new Scanner (system.in);
		for (int i = 0;i<4;i++) {A[i] = Sc.nextint ();
		} calculateanswer cal = new Calculateanswer (a);
		Cal.run ();
	Sc.close ();
 }
}


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.