Bulls and Cows (100 Marks) You are required to play a game of Bulls and Cows, with a computer. The game is a word-game, and proceeds as follows The computer 'thinks' of a four-letter word consisting of letters A to D Then it proceeds to give you several clues, as to what the word is Given the clues, you have to work-out the word that the computer thought of (secret-word) The computer, keeping in mind our agrarian roots, gives clues in the form of 'bulls' and 'cows'! Given a guess-word, it tells us the number of 'bulls' and 'cows' in the word. A 'bull' stands for a letter which is in it's correct place in the secret-word, and a 'cow' stands for a letter which is present in the secret-word, but not in the correct place. However, to make the game a bit challenging, the computer does not tell us which letter is a bull and which one is a cow! Bulls are counted first and then cows. Additionally, each letter in the secret-word is counted only once. For example, if the computer thought of the word: BDAA and the guess-word is: ADDA, then the computer would respond as 2 bulls and 1 cow. This is so, because the letters 'A' and 'D' in the clue word are present in their correct places (with regard to BDAA) and hence 2 bulls. The other 'A' in the guess-word is present in the wrong place (with regard to BDAA) and hence 1 cow. Given several such clue words and the number of cows and bulls for each word, your program should be able to work out the actual word that the computer had thought of. NOTE: It is guaranteed that the clues will be enough to work out the secret-word uniquely Input Format First line contains one integer N indicating the number of clue words which follow The next N lines contain input of the form: W B C Where 'W' is a string specifying the clue word, B is an integer specifying the number of bulls and C is an integer specifying the number of cows Constraints 1<= N <=10 0<= B, C <=|W| Output Format A string which is the word that the computer thought of, i.e., secret-word. Code in java
4 DBCC 0 2
CDAB 2 1
CAAD 1 2
CDDA 2 0
output BDAA
Answers
Answer:oh ok now please ask a science questions please
Answer:
import java.util.Scanner;
class Evaluation
{
String str;
int bulls;
int cows;
Evaluation(String s, int b, int c)
{
str=s;
bulls=b;
cows=c;
}
boolean isCorrect(String str2)
{
int bulls2 = 0;
int cows2 = 0;
String str1 = str;
for (int i=0; i<4; i++)
{
if (str1.charAt(i)==str2.charAt(i))
{
bulls2++;
str2 = deleteChar(str2,i);
str1 = deleteChar(str1,i);
}
}
for (int i=0; i<4; i++)
{
char ver = str1.charAt(i);
int pos = str2.indexOf(ver);
if (ver=='.')
pos = -1;
if (pos!=-1)
{
cows2++;
str2 = deleteChar(str2,pos);
str1 = deleteChar(str1,i);
}
}
return ((bulls==bulls2)&&(cows==cows2));
}
String deleteChar(String str, int pos)
{
return (str.substring(0,pos)+"."+str.substring(pos+1));
}
}
public class CandidateCode
{
static int number_of_clues;
static Evaluation[] word;
static void checkParameter(String str)
{
boolean done = true;
for (int i=0; i<number_of_clues; i++)
{
if (!word[i].isCorrect(str))
{
done=false;
}
}
if (done)
{
System.out.println(str);
}
}
static void ghostProtocol(String str)
{
if (str.length()==4)
{
checkParameter(str);
return;
}
for (int i=0; i<4; i++)
ghostProtocol(str+((char)(i + 'A')));
}
static void getData()
{
Scanner input = new Scanner(System.in);
number_of_clues = input.nextInt();
word = new Evaluation[number_of_clues];
System.out.flush();
String clueword;
int BULL;
int COW;
for(int i=0;i<number_of_clues;i++)
{
clueword = input.next();
BULL = input.nextInt();
COW = input.nextInt();
word[i] = new Evaluation(clueword, BULL, COW);
System.out.flush();
}
}
public static void main(String []s)
{
getData();
ghostProtocol("");
}
}
Explanation: