Algorithm and data structure surface questions (15)-8 Poker games __java

Source: Internet
Author: User
Tags int size split stub
Topics


There are 4 red cards and 4 blue cards, the host first take any two, and
then respectively in a, B, c three people on the forehead affixed any two cards,
A, B, c three people can see the other two people on the forehead of the cards,
after reading let them guess what color on their forehead card,
A Say do not know, B said do not know, C said do not know, then a said to know.
ask how to reason, A is how to know.
If you use the program, how to achieve it.

Thinking of solving problems


the first case of combination is 3x3x3 =


public void Group () {        string[] arg = {"Blue-blue", "red-red", "Red-Blue"};     &nb
sp;   int size = arg.length;
        int index = 0;         for (int i = 0; i < size; i++) {         &nbs P
 string A = Arg[i];             for (int j = 0; J < size; J + +) {     &nbs P
         string B = arg[j];                 for (int k = 0; k < size; k++) { &nbs P
                 string C = arg[k];
                    index++;                     string group = STRING.FOrmat ("%s-%s-%s", A, B, C);                     string groupstr =
String.Format ("%s%s%s", A, B, C);                     system.out.print (
Index + ":");                     system.out.println (
GROUPSTR);                 }         &
 nbsp;   }         }     }

1: Blue-blue blue-blue-Blue 2: Blue-blue blue-blue-red-
Red
3: Blue-blue-blue red-blue
4: Blue-blue red-red blue-Blue
5: Blue-blue red-red-red
6: Blue-blue-red-red-blue
7: Blue-blue-red  -Blue-Blue
8: Blue-blue red-blue red-red
9: blue-blue red-blue red-blue
10: Red-red blue-blue-blue
11: Red-red blue-blue red-red
12: Red-red blue-blue red-blue
13: Red-red-red  Blue-Blue
14: Red-red red-red red-red
15: Red-red red-red-blue
16: Red-red red-blue-blue
17: Red-red red-blue red-red
18: red-red red-blue red-blue
19: Red-Blue  Blue-blue-blue
20: Red-blue blue-blue red-red
21: Red-blue blue-blue red-blue
22: Red-blue red-red blue-blue
23: Red-blue red-red red-red
24: Red-blue red-red red-blue
25: Red-blue red-blue-blue
26: Red-blue red-blue red-red
27: Red-blue red-blue red-blue

and then filter out some of the options in the above combinations by some conditions


Condition 1: The number of red in a combination or the number of blue cannot exceed 4


public void Group () {
		string[] arg = {"Blue-blue", "red-red", "Red-Blue"};
		int size = Arg.length;
		int index = 0;
		for (int i = 0; i < size; i++) {
			String A = arg[i];
			for (int j = 0; J < size; J + +) {
				String B = arg[j];
				for (int k = 0; k < size; k++) {
					String C = arg[k];
					index++;
					String Group = String.Format ("%s-%s-%s", A, B, C);
					String groupstr = String.Format ("%s%s%s", A, B, C);
					if (Getstrcount (group)) {
						System.out.print (index + ":");
						System.out.println (GROUPSTR);

	}}}} public boolean getstrcount (String str) {
		string[] args = Str.split ("-");
		int bluecount = 0;
		int redcount = 0;
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals ("Blue")) {
				bluecount++;
			} else if (args[i].equal S ("Red")) {
				redcount++;
			}
		}
		if (Bluecount > 4 | | redcount > 4) {
			return false;
		}
		return true;
	}


Filter out the above numbered 1,3,7,14,15,17,19,23 eight cases. There are 19 of them left.


2: Blue-blue-blue red-red
4: Blue-blue-red-red-blue-blue
5: Blue-blue-red-red-red
6: Blue-blue-red-red-blue
8: Blue-blue red-blue red-red
9: blue-blue red-blue red-blue
10: red-Red Blue-blue-blue
11: Red-red-blue-blue-red
12: Red-red-blue-blue-red-blue
13: Red-red red-red blue-Blue
16: Red-red-blue-blue
18: Red-red red-blue red-blue
20: Red-Blue Blue-blue red-red
21: Red-blue blue-blue red-blue
22: Red-blue red-red blue-Blue
24: Red-blue red-red red-blue
25: Red-blue red-blue-blue
26: Red-blue red-blue red-red
27: Red-blue red-blue red-blue

condition 2. A don't know, b don't know, C don't know. Description of the three people can not each hand two cards are the same color.


To disprove: if there are three people in each hand the cards are the same color. So for the first time, 3 people can guess directly at one glance.


public void Group () {string[] arg = {"Blue-blue", "red-red", "Red-blue"};
		int size = Arg.length;
		int index = 0;
			for (int i = 0; i < size; i++) {String A = arg[i];
				for (int j = 0; J < size; J + +) {String B = arg[j];
					for (int k = 0; k < size; k++) {String C = arg[k];
					index++;

					String groupstr = String.Format ("%s%s%s", A, B, C);
					Condition 1 String group = String.Format ("%s-%s-%s", A, B, C);
					if (!getstrcount (group)) continue;
					Condition 2 if (!iseveryonesame (A) &&!iseveryonesame (B) &&!iseveryonesame (C)) continue;
					System.out.print (Index + ":");
				System.out.println (GROUPSTR); }}}}//Condition 2     private boolean iseveryonesame (String one) {       &nb Sp
string[] args = One.split ("-");         if (Args[0].equals (args[1])) {         
   return true;         }         return false;
		    }//Condition 1 Private boolean getstrcount (String str) {string[] args = Str.split ("-");
		int bluecount = 0;
		int redcount = 0;
			for (int i = 0; i < args.length; i++) {if (Args[i].equals ("Blue")) {bluecount++;
			} else if (Args[i].equals ("Red")) {redcount++;
		}} if (Bluecount > 4 | | redcount > 4) {return false;
	} return true; }


There are a couple of possible combinations left.


6: Blue-blue red-red red-blue
8: Blue-blue red-blue red-red
9: blue-blue red-blue red-blue
12: Red-red blue-blue red-blue
16: Red-red red-blue-blue
18: Red-red red-blue red-blue
20: Red -Blue blue-blue red-red
21: Red-blue blue-blue red-blue
22: Red-blue red-red blue-Blue
24: Red-blue red-red red-blue
25: Red-blue red-blue-blue
26: Red-blue red-blue red-red
27: Red -Blue red-blue red-blue


Number 2,4,5,10,11,13 can be excluded in six cases. There are 13 of them left.


condition 3. In the above case, to exclude C can be based on a, B, do not know, and then can be seen at a glance what the card combination.


This is the case of a hand of the card color is the same, B hands of the card color is the same. For example, 6 and 12


public void Group () {string[] arg = {"Blue-blue", "red-red", "Red-blue"};
		int size = Arg.length;
		int index = 0;
			for (int i = 0; i < size; i++) {String A = arg[i];
				for (int j = 0; J < size; J + +) {String B = arg[j];
					for (int k = 0; k < size; k++) {String C = arg[k];
					index++;

					String groupstr = String.Format ("%s%s%s", A, B, C);
					Condition 1 String group = String.Format ("%s-%s-%s", A, B, C);
					if (!getstrcount (group)) continue;
					Condition 2 if (Iseveryonesame (A) && iseveryonesame (B) && iseveryonesame (C)) continue;
					Condition 3 if (Iseveryonesame (A) && iseveryonesame (B)) continue;
					System.out.print (Index + ":");
				System.out.println (GROUPSTR);
		}}}}//Condition 2 Private Boolean iseveryonesame (String one) {string[] args = One.split ("-");
		if (Args[0].equals (Args[1])) {return true;
	} return false; }//Condition 1 Private boolean getstrcount (String str) {string[]args = Str.split ("-");
		int bluecount = 0;
		int redcount = 0;
			for (int i = 0; i < args.length; i++) {if (Args[i].equals ("Blue")) {bluecount++;
			} else if (Args[i].equals ("Red")) {redcount++;
		}} if (Bluecount > 4 | | redcount > 4) {return false;
	} return true;
		} public static void Main (string[] args) {//TODO auto-generated method stub Problem22 problem = new Problem22 ();
	Problem.group ();
 }

}


There are 11 other cases left.


8: Blue-blue red-blue red-red
9: blue-blue red-blue red-blue
16: Red-red red-blue-blue
18: Red-red-blue-blue
20: Red-blue-blue-red
21: Red-blue blue-blue red-blue
22: Red-blue red-red blue-Blue
24: Red-blue red-red-blue
25: Red-blue red-blue-blue
26: Red-blue red-blue red-red
27: Red-blue red-blue red-blue

come to the results


The remaining 11 of the above is in accordance with the actual licensing situation, and according to the first round of a,b,c can not judge their hands of the cards these conditions filtered combinations. Then according to the last second round a will already know their cards. Description from the combination above to find, with b,c combination as the keyword, a only one case of a combination. For example, in case 8, the b,c combination is "red-blue red-red", then according to this to find out, the same is the condition of a there is no other situation. It was obvious that number 26th met. It is indicated that this combination is not in accordance with the requirements. And the 20th "blue-blue red-red" only one case, the description meets the requirements. That's how the program is implemented, we can use b+c as the keyword to build a map,value value for this keyword in the above 11 occurrences of the number of times. So we can get the keyword that appears once, so it's easy to determine what the number is.


public class Problem22 {//with BC as the keyword, number of times is value of map map<string,integer> map = new Linkedhashmap<string,intege
    R> ();

    Map map<string,string> map1 = new linkedhashmap<string,string> () with BC as the keyword, combined as value
        public void Group () {string[] arg = {"Blue-blue", "red-red", "Red-blue"};
        int size = Arg.length;
        int index = 0;
            for (int i = 0; i < size; i++) {String A = arg[i];
                for (int j = 0; J < size; J + +) {String B = arg[j];
                    for (int k = 0; k < size; k++) {String C = arg[k];
                    index++;

                    String groupstr = String.Format ("%s%s%s", A, B, C);
                    Condition 1 String group = String.Format ("%s-%s-%s", A, B, C);
                    if (!getstrcount (group)) continue;
       Condition 2 if (Iseveryonesame (A) && iseveryonesame (B)                     && Iseveryonesame (C)) continue;

                    Condition 3 if (Iseveryonesame (A) && iseveryonesame (B)) continue;
                       if (Map.containskey (B + C)) {int num = Map.get (b+c);
                    Map.put (B+c,++num);
                    }else{Map.put (b+c,1);
                    } map1.put (B+c,index + ":" +groupstr);
                    System.out.print (Index + ":");
                System.out.println (GROUPSTR);
        }}} set<map.entry<string, integer>> Entrys = Map.entryset ();
        Iterator<map.entry<string, integer>> iter = Entrys.iterator ();
            A combination of 1 found while (Iter.hasnext ()) {map.entry<string, integer> entry= iter.next (); if (Entry.getvalue () ==1) {String answer = MAP1.GET (Entry.getkey ());
            SYSTEM.OUT.PRINTLN ("The qualifying answer found from the combination above is:" +answer);
        }}}//Condition 2 Private Boolean iseveryonesame (String one) {string[] args = One.split ("-");
        if (Args[0].equals (Args[1])) {return true;
    } return false;
        }//Condition 1 Private boolean getstrcount (String str) {string[] args = Str.split ("-");
        int bluecount = 0;
        int redcount = 0;
            for (int i = 0; i < args.length; i++) {if (Args[i].equals ("Blue")) {bluecount++;
            } else if (Args[i].equals ("Red")) {redcount++;
        }} if (Bluecount > 4 | | redcount > 4) {return false;
    } return true;  } public static void Main (string[] args) {//TODO auto-generated method stub Problem22 problem = new
        PROBLEM22 ();
    Problem.group (); }

}

Result is


8: Blue-blue red-blue red-red
9: blue-blue red-blue red-blue
16: Red-red red-blue-blue
18: Red-red-blue-blue
20: Red-blue-blue-red
21: Red-blue blue-blue red-blue
22: Red-blue red-red blue-Blue
24: Red-blue red-red-blue
25: Red-blue red-blue-blue 26: Red-blue-red-blue-red
27: Red-blue red-blue red-Blue the
answer that matches the criteria found in the combination above is: 20: Red-blue blue-blue red-red the answer to the
criteria found in the combination above is: 21: Red-blue blue-blue red-Blue The
qualifying answer found from the combination above is: 22: Red-blue red-red blue-Blue the
answer that matches the criteria found in the combination above is: 24: Red-blue red-red red-blue


There are 4 cases in which a can guess its own cards. Of course, a can have such a strong logic.


Summary


There is no possibility that the cards in the hands are the same: red and blue. So after that you guess you're red and blue.

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.