UVA10344 23 out of 5【暴力+DFS】

Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers ai (1 ≤ i ≤ 5) that will yield the value 23.

  For this problem we will only consider arithmetic expressions of the following from:

          (((aπ(1) o1 aπ(2)) o2 aπ(3)) o3 aπ(4)) o4 aπ(5)

where π : {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5} is a bijective function and oi ∈ {+, −, ∗}(1 ≤ i ≤ 4)

Input

The Input consists of 5-Tupels of positive Integers, each between 1 and 50.

  Input is terminated by a line containing five zero’s. This line should not be processed. Input file will have no more than 25 lines.

Output

For each 5-Tupel print ‘Possible’ (without quotes) if their exists an arithmetic expression (as described above) that yields 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

问题链接UVA10344 23 out of 5

问题描述

  输入5个数,按照指定的公式,用运算符{+,-*}进行计算,判断其结果是否有可能为23。

问题分析

  暴力是必须的,问题在于怎么实现暴力。

  用DFS实现是一种有效的方法,其中用到递归和回溯。

  另外一种做法是使用全排列实现。操作数先做一下全排列,然后再对运算符进行暴力搜索,也许程序逻辑可以稍微简单一些。

程序说明

  给出2种程序,可以对照着看。

  第一个程序的第16行条件,加上程序可以加快程序结束。

  使用全排列函数进行计算似乎是需要先排序的!

参考链接:(略)

题记:需要暴力时,考虑一下全排列。

AC的C++语言程序如下:

/* UVA10344 23 out of 5 */

#include <bits/stdc++.h>

using namespace std;

const int N = 5;
int a[N];
bool flag, vis[N];

void dfs(int cnt, int result)
{
    if (cnt == N) {
        if (result == 23)
            flag = true;
    } else if(!flag) {
        for (int i = 0; i < N; i++) {
            if (!vis[i]) {
                vis[i] = true;
                dfs(cnt + 1, result + a[i]);
                dfs(cnt + 1, result - a[i]);
                dfs(cnt + 1, result * a[i]);
                vis[i] = 0;
            }
        }
    }
}

int main()
{
    for(; ;) {
        int sum = 0;
        for(int i = 0; i < N; i++) {
            scanf("%d", &a[i]);
            sum += a[i];
        }
        if(sum == 0)
            break;

        flag = false;
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < N; i++) {
            vis[i] = true;
            dfs(1, a[i]);
            vis[i] = false;
            if(flag)
                break;
        }

        printf("%s\n", flag ? "Possible" : "Impossible");
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA10344 23 out of 5 */

#include <bits/stdc++.h>

using namespace std;

const int N = 5;
int a[N];

bool dfs(int cnt, int result)
{
    if(cnt == N) {
        if(result == 23)
            return true;
    } else
        return dfs(cnt + 1, result + a[cnt]) || dfs(cnt + 1, result - a[cnt]) || dfs(cnt + 1, result * a[cnt]);
    return false;
}

int main()
{
    for(; ;) {
        int sum = 0;
        for(int i = 0; i < N; i++) {
            scanf("%d", &a[i]);
            sum += a[i];
        }
        if(sum == 0)
            break;

        bool flag;
        sort(a, a + N);
        do {
            if((flag = dfs(1, a[0])))
                break;
        } while(next_permutation(a, a + N));

        printf("%s\n", flag ? "Possible" : "Impossible");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81900824
23
今日推荐