TCHS-5-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

    
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";
	}

}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值