UVa 10344 5: Full Array enumeration & backtracking

Source: Internet
Author: User

10344-23 out of 5

Time limit:3.000 seconds

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

Your task is to write a program so can decide whether can find a arithmetic expression consisting of five given Num Bers (1<=i<=5) that would yield the value 23. For this problem we'll 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)

Input

The Input consists of 5-tupels of positive integers, each between 1 and 50. The Input is terminated by a line containing five zero ' s. This line should is processed.

Output

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45364.htm

For each 5-tupel print "Possible" (without quotes) if their exists a arithmetic expression (as described) that above DS 23. Otherwise print "impossible".

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

Water problem.

Complete code:

/*0.279s*/
    
#include <cstdio>  
#include <algorithm>  
using namespace std;  
    
int a[5];  
BOOL Flag;  
    
void Dfs (int cur, int ans)  
{  
    if (cur = 5)  
    {  
        if (ans = =) flag = true;  
        return;  
    }  
    if (!flag) DFS (cur + 1, ans + a[cur]);  
    if (!flag) DFS (cur + 1, ans-a[cur]);  
    if (!flag) DFS (cur + 1, ans * a[cur]);  
    
int main ()  
{while  
    (scanf ("%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4), a[0])  
    {  
        flag = false;  
        Sort (A, a + 5);  
        Do
        {  
            dfs (1, a[0]);  
            if (flag) break;  
        while (Next_permutation (A, A + 5));  
        Puts (flag?) "Possible": "Impossible");  
    return 0;  
}

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.