`
superhack
  • 浏览: 32104 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TCHS-5-600

J# 
阅读更多

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

    
Class: Masterbrain
Method: possibleSecrets
Parameters: String[], String[]
Returns: int
Method signature: int possibleSecrets(String[] guesses, String[] results)
(be sure your method is public)
    
 

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)  
    
{"1575"}

{"4b 0w"}

Returns: 2400

If the result was true, we would conclude that 1575 is the only possible combination. However, we know that the second player must lie exactly once, thus we know that 1575 is the only combination NOT possible. Since there are 7^4 = 2401 total combinations, the method should return 2401-1 = 2400.
1)  
    
{"1234"}

{"0b 4w"}

Returns: 2392

If the result was true then the set of secret combinations would have 9 elements: {2143, 2341, 2413, 3142, 3412, 3421, 4123, 4312, 4321}. But since the result is false, we must subtract this number from the total. The method should return 2401-9 = 2392.
2)  
    
{"6172","6162","3617"}

{"3b 0w","2b 1w","0b 3w"}

Returns: 14

If all results were true, then the secret must be 6176. If the first result is false then the set of secret combinations is {1362, 1762, 2163, 6123, 6136, 6176, 6361, 6761, 7166}. If the second result is false then set is {6132, 6171, 6174, 6175, 6176, 6372}. Finally, if the third result is false then the set is {6176, 6672}. Thus the method should return (9-1)+(6-1)+(2-1) = 14.
3)  
    
{"1513","5654","4564","1377","1671","1342"}

{"1b 0w","0b 1w","1b 0w","1b 0w","0b 1w","0b 1w"}

Returns: 6

 
4)  
    
{"2611", "1371", "7417", "2647", "3735", "4272", "2442", "3443", "1252", "3353"}

{"0b 2w","0b 2w","0b 1w","0b 2w","1b 0w","1b 0w","1b 0w","0b 1w","1b 1w","0b 1w"}

Returns: 1

 
public class Masterbrain {

	public int possibleSecrets(String[] guesses, String[] results) {
		int count = 0;
		String secret = null;
		for (int i = 1111; i < 7778; i++) {
			secret = i + "";
			if (!secret.matches("[1-7]{4}"))
				continue;
			int fault = 0;
			for (int j = 0; j < guesses.length; j++) {
				if (!getResult(guesses[j], secret).equals(results[j]))
					fault++;
				if (fault > 1)
					break;
			}
			if (fault == 1)
				count++;
		}
		return count;
	}
	
	private String getResult(String guess, String secret) {
		int black = 0, white = 0;
		int[] counts = new int[8];
		for (int i = 0; i < 4; i++) {
			char ch = secret.charAt(i);
			if (ch == guess.charAt(i))
				black++;
			else
				counts[ch-'0']++;
		}
		for (int i = 0; i < 4; i++) {
			char ch = guess.charAt(i);
			if (ch != secret.charAt(i) && counts[ch-'0'] > 0) {
				white++;
				counts[ch-'0']--;
			}
		}
		return black + "b " + white + "w";
	}

}

  

分享到:
评论

相关推荐

    javalruleetcode-Open-Source-Algorithms:快乐编码!

    TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...

    唐亚辉文件关于算法,怎样去实现算法的

    - 计算`PDCHS_zhuan`和`TCHS_zhuan`(专用数据信道和语音信道转换后的数量)。 - 计算`DSP_pdch`和`DSP_tch`(所需的数字信号处理器数量)。 #### 输出结果 - 使用`printf`函数输出计算结果,包括基站数量、链路接...

    Bit Error Rate Testing with CMU300

    电路交换业务信道(TCHs)测试是BER测试的重要组成部分,主要用于评估语音和低速数据服务的质量。这一部分将详细介绍如何设置测试环境并执行测试。 #### 分组数据业务信道(PDTCHs)测试 随着数据传输速度的提高,...

    基站代维考试复习题汇总.doc

    TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...

    基站代维考试复习题Nokia设备.docx

    4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...

    techies:Javascript 验证库。 为什么? 因为我写了一些非常糟糕的验证代码,并且想在学习的同时做一些更健壮的事情

    要告诉技术人员验证元素,请将“tchs”属性添加到元素。 &lt;input type="text" tchs=""&gt;&lt;/input&gt; 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...

    TOPCODER 算法PPT1

    此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...

Global site tag (gtag.js) - Google Analytics