Ultraviolet A 10400 game show math (memory-based search)

Source: Internet
Author: User

Question connection: 10400-game show math


N numeric values can be added to any number, such as +,-, *, And/. A formula must be found to make the formula value equal to the target value.

Note: 1. Arithmetic Operators have no priority. They are all combined from left to right.

2. The positions between values cannot be changed. That is to say, the order of values is determined and operators can only be modified.

3. The absolute value of an operation value must not be greater than 32000.


Solution: first, we thought of recursion. If we didn't think of good pruning, we would just recursively continue and the result would have timed out. After looking at other people's question solutions, we knew we would use a memory-based search. Is to record the sum obtained by the current first n digits through any operation. If this sum is determined to be unable to reach the goal, if another different calculation method makes the sum of the First n equal to the sum previously determined, this branch is cut off because it is no longer possible.

#include <stdio.h>#include <string.h>#include <stdlib.h>const int N = 105;const int M = 32000;const char sign[] = "+-*/";int n, num[N], order, vis[N][2 * M + 10];char str[N];int count(int sum, char c, int number) {    if (c == '+')return sum + number;    else if (c == '-')return sum - number;    else if (c == '*')return sum * number;    else if (c == '/')return sum / number;     return 0;}int find(int sum, int deep) {    if (deep == n) {if (sum == order) {    str[deep - 1] = '=';    return true;}else    return false;    }    int cur;    for (int i = 0; i < 4; i++) {if (i == 3 && (num[deep] == 0))  continue;cur = count(sum, sign[i], num[deep]);if (cur < -32000 || cur > 32000)continue;if (vis[deep][cur + M])continue;if (find(cur, deep + 1)) {    str[deep - 1] = sign[i];    return true;}vis[deep][cur + M] = 1;    }    return false;}int main () {    int cas, flag;    scanf("%d", &cas);    while (cas--) {memset(str, 0, sizeof(str));memset(vis, 0, sizeof(vis));scanf("%d", &n);for (int i = 0; i < n; i++)    scanf("%d", &num[i]);scanf("%d", &order);if (find(num[0], 1)) {    for (int i = 0; i < n; i++)printf("%d%c", num[i], str[i]);    printf("%d\n", order);}else    printf("NO EXPRESSION\n");    }    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.