10344-23 out of 5, intelligence games:

Source: Internet
Author: User
10344-23
Out of 5
7004 31.42% 1882 73.33%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 1285

Type: backtracking


Original question:

Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers (1 <= I <= 5)
That will yield the value 23.
For this problem we will only consider arithmetic expressions of the following from:

 
 
where : {1,2,3,4,5} -> {1,2,3,4,5} is a bijective function
and  {+,-,*} (1<=i<=4)
Sample input:

1 1 1 1 1
1 2 3 4 5
2 3 5 7 11
0 0 0 0 0

Sample output:

Impossible
Possible
Possible
 
 
 
Question:
The meaning of this question is a good question. The game rules are basically the same as the "24 o'clock" of playing cards.
The rule is as follows: Give you five cards and use the +,-, and * symbols to calculate the five numbers so that they are 23.
You need to program to determine whether all five cards can be implemented.
 
 
Ideas and summary:
This game is believed to have been played by many people when they were young. If we could compile a small program to help us when we were young, then ...... It seems that programming is still a bit useful,
At least you can lie to a child...
 
If we use a computer and Recursive Backtracking Algorithm to do this, it will be very simple. We only need to find all the arrays and use all the symbols,
You can easily get the answer
 
 
Code implementation:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int arr [5]; bool vis [5], flag; void DFS (INT cur, int sum) {If (FLAG) return; // once the solution is found, you can continue to roll back the stack for (INT I = 0; I <5; ++ I) if (! Vis [I]) {vis [I] = true; If (! Cur) DFS (cur + 1, arr [I]); else {DFS (cur + 1, Sum-Arr [I]); // subtraction DFS (cur + 1, sum + arr [I]); // Add DFS (cur + 1, sum * arr [I]); // multiplication} vis [I] = false; // backtracking} If (cur = 5 & sum = 23) {flag = true; return ;}} int main () {# ifdef local freopen ("input.txt ", "r", stdin); # endif while (~ Scanf ("% d", & arr [0], & arr [1], & arr [2], & arr [3], & amp; arr [4]) {If (! Arr [0] &! Arr [1] &! Arr [2] &! Arr [3] &! Arr [4]) break; flag = false; memset (VIS, 0, sizeof (VIS); DFS (0, 0); If (FLAG) printf ("Possible \ n"); else printf ("impossible \ n");} return 0 ;}

-- The meaning of life is to give it meaning.

 

Original Http://blog.csdn.net/shuangde800 , By d_double(For details, refer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.