SRM 146 DIV1 600
Problem Statement |
|||||||||||||
Masterbrain is a two player board game in which one player decides on a secret combination of digits, while the other must figure it out in 10 guesses or less. The game differs from Mastermind in that the player making the secret combination is allowed to lie once. The game consists of one player making a sequence of guesses about what the secret combination is, and the other player giving him or her certain information about the quality of the guess. The following is how each guess is analyzed: if a digit is in the correct position then a black peg is given. If a digit is in the guess but in the wrong position then a white peg is given. For all other cases no pegs are given. For example, if guess = "1234", secret = "2335". Analyzing the guess digit by digit; the '1' is not in secret - no pegs given. The '2' is in secret but not in the right place - white peg given. The '3' is in secret and in the right place - black peg given. The '4' is not in secret - no pegs given. Result should be "1b 1w", meaning one black peg and one white peg. Now, if guess is "2334" and secret is "3224", we have the following: '2' is in secret, but not in the right place - white peg given. The first '3' is in secret, but not in the right place - white peg given. Since the '3' in secret has been used, the second '3' in guess should return no pegs. The '4' is in secret and in the right place - black peg given. Result should be "1b 2w". Given a String[] of guesses and a String[] of results for those guesses, return the total number of possible secret combinations, assuming that exactly one of the results is incorrect. Each element of results will be formatted as "<x>b <y>w", where <x> and <y> are the number of black and white pegs respectively. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | The second player must lie exactly once. | ||||||||||||
- | Black pegs always take precedence over white pegs. Thus, when analyzing a guess, black pegs are assigned first, and then white pegs are assigned. | ||||||||||||
- | No digit in either a guess or a secret combination may be involved in giving more than one peg. | ||||||||||||
Constraints |
|||||||||||||
- | guesses and results will have the same number of elements. | ||||||||||||
- | guesses will have between 1 and 10 elements inclusive. | ||||||||||||
- | results will have between 1 and 10 elements inclusive. | ||||||||||||
- | each element in guesses will contain exactly 4 characters and will only contain digits between '1' and '7' inclusive. | ||||||||||||
- | each element in results will contain exactly 5 characters. | ||||||||||||
- | each element of results will be formatted as follows: "<x>b <y>w", where <x> represents the number of black pegs and <y> represents the number of white pegs in a guess. <x> and <y> are non-negative integers whose sum is less than or equal to 4. | ||||||||||||
- | results will never have "3b 1w", because that is impossible. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
本题考虑到总共只有7^4种可能使用枚举暴力实现,在判断某一组合是否符合猜测是需优先考虑block leg
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.util.*; 2 import java.util.regex.*; 3 import java.text.*; 4 import java.math.*; 5 import java.awt.geom.*; 6 7 public class Masterbrain { 8 boolean detect(int[] combine, int[] guesses, String result) { 9 int b, w; 10 b = w = 0; 11 int[] c = new int[8]; 12 boolean[] ct = new boolean[4]; 13 for (int i = 0; i < 4; i++) { 14 if (combine[i] == guesses[i]) { 15 b++; 16 c[combine[i]]--; 17 } 18 c[combine[i]]++; 19 } 20 for (int i = 0; i < 4; i++) { 21 if (c[guesses[i]] == 0) 22 continue; 23 if (combine[i] != guesses[i]) { 24 c[guesses[i]]--; 25 w++; 26 } 27 } 28 String tmp = String.format("%db %dw", b, w); 29 return tmp.trim().equalsIgnoreCase(result); 30 } 31 32 public int possibleSecrets(String[] guesses, String[] results) { 33 int count = 0; 34 for (int i = 1; i < 8; i++) 35 for (int j = 1; j < 8; j++) 36 for (int k = 1; k < 8; k++) 37 for (int l = 1; l < 8; l++) { 38 int[] combine = new int[] { i, j, k, l }; 39 int fd = 0; 40 for (int m = 0; m < guesses.length; m++) { 41 int[] guess = new int[4]; 42 Integer g = Integer.parseInt(guesses[m]); 43 for (int n = 3; n >= 0; n--) { 44 guess[n] = g % 10; 45 g = g / 10; 46 } 47 boolean r = detect(combine, guess, results[m]); 48 if (!r) 49 fd++; 50 } 51 if (fd == 1) 52 count++; 53 } 54 return count; 55 } 56 }
posted on 2014-04-10 15:36 PolarisSherlock 阅读(168) 评论(0) 编辑 收藏 举报