D
D
Denis2016-05-09 16:05:25
Mathematics
Denis, 2016-05-09 16:05:25

Is there an algorithm for solving the game Bulls and Cows?

Tell me if there is an algorithm for solving the problem of the game Bulls and Cows. The task looks something like this:
The moves were made in the game:
1985 1 bull, 2 cows
5147 1 bull, 0 cows
4801 0 bulls, 3 cows
6870 0 bulls, 2 cows
Guess the correct answer from the moves made earlier.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DeepBlue, 2016-05-09
@TomskDiver

If the goal is to program a solution, then iterating over all four-digit numbers, checking to see if they meet all the specified conditions, will work for a reasonable amount of time. If you still need it faster than brute force, then you need to come up with tricks on how to reduce it in a particular situation. For example, in this example, you can argue like this:
1. Since 1985 contains three digits of a number, it means that 1 or 5 is in it. Since 5147 contains only one, it means that 4 and 7 are not in it.
2. 4801 contains 3 digits, but we already know that there is no 4. So 801 are present. From 5147 it can be seen that 1 is in second place, but there is no five.
3. Since there is no five, then from 1985 it follows that there are 198. Combining with item 2, we get that the number consists of the digits 0189.
4. The unit is in second place, and 0 is definitely not in the 3rd and not in the 4th m. So 0 on the first one.
5. In 1985, one and nine are not bulls, which means that 8 is in its place. So 0189 is the only valid number.
But I'm afraid that writing a program that will generate this type of reasoning is not easy. However, some of the ideas from here can be used to shorten the search: for example, you can first find possible sets of numbers, and then for each of them look for a suitable order.

D
Denis, 2016-05-09
@TomskDiver

Thanks deepblue ! Everything was well explained! Solved 4 more similar tasks. But he didn’t master this last one:
The moves were made in the game:
3086 1 bull 1 cow
7914 1 bull 1 cow
0794 1 bull 1 cow
6491 1 bull 1 cow
1947 1 bull 1 cow
Guess the correct answer from the moves made earlier.
Master?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question