UVa 817 According to Bartjens (暴力,DFS)

题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子。 所有数字不能有前导0,

且式子必须是合法的。

析:这个题很明显的暴力,因为最长才9位数字,也就是最多有8个位置位置可能插符号,当然实际并没有那么多,所以直接暴力就行,也不用优化,直接暴就行。

就是DFS,在每个位置考虑四种情况,*,+,-,或者不放,最后再一个一个的判断是不是等于2000就好,注意这个题有一个坑,我也不知道是哪个数据,

也没有想到,就是一个没有用运算符也没有的时候,是不成立的,比如2000,这个是不成立,我并没有找到其他的数据,但是如果我只判2000,是WA,

如果哪位大神知道,告诉一下,感激不尽。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
vector<string> ans;

void dfs(int idx, string s){
    if(idx == s.size()){
        ans.push_back(s);
        return ;
    }

    for(int i = 0; i < 3; ++i){
        string t = s;
        t.insert(idx, 1, mark[i]);
        dfs(idx+2, t);
    }
    dfs(idx+1, s);
}

inline bool before0(const string &s){
    int cnt = 0;
    for(int i = 1; i < s.size()-1; ++i){
        if(!isdigit(s[i]))  ++cnt;
        if(!isdigit(s[i-1]) && s[i] == ‘0‘ && isdigit(s[i+1]))  return true;
    }

    return !(cnt > 0);
}

bool judge(const string &s){
    int i = 0;
    int ans = 0, tmp = 0;
    char ch = ‘+‘;
    while(i < s.size() && isdigit(s[i])) tmp = tmp * 10 + s[i++] - ‘0‘;
    while(i < s.size()){
        if(s[i] == ‘*‘){
            int t = 0;  ++i;
            while(i < s.size() && isdigit(s[i])) t = t * 10 + s[i++] - ‘0‘;
            tmp *= t;
        }
        else{
            ans += ch == ‘+‘ ? tmp : -tmp;
            ch = s[i];  ++i;  tmp = 0;
            while(i < s.size() && isdigit(s[i]))  tmp = tmp * 10 + s[i++] - ‘0‘;
        }
    }
    ans += ch == ‘+‘ ? tmp : -tmp;
    return ans == 2000;
}

int main(){
    string s;
    int kase = 0;
    while(cin >> s && s[0] != ‘=‘){
        printf("Problem %d\n", ++kase);
        if(s == "2000=" || s.size() < 4){  puts("  IMPOSSIBLE"); continue; }
        s.pop_back();
        ans.clear();
        dfs(1, s);
        bool ok = false;
        for(int i = 0; i < ans.size(); ++i)
            if(!before0(ans[i]) && judge(ans[i]))
                cout << "  " << ans[i] << "=\n", ok = true;
        if(!ok) puts("  IMPOSSIBLE");
    }
    return 0;
}
时间: 2024-08-07 07:41:17

UVa 817 According to Bartjens (暴力,DFS)的相关文章

UVA - 817 According to Bartjens 暴力

题目大意:给出一个字符串,要求你在这个字符串里面加入符号,使得结果为2000 解题思路:直接暴力 #include<stdio.h> #include<string.h> #include<vector> #define maxn 30 using namespace std; char str[maxn]; bool flag; int num[maxn], sign[maxn], len; char s[5]= " *+-"; bool judg

UVA - 817 According to Bartjens

Description  According to Bartjens  The wide dissemination of calculators and computers has itsdisadvantages. Even students in technical disciplinestend to exhibit a surprising lack of calculating ability. Accustomed tothe use of calculators and comp

UVa 817 According to Bartjens 题解

难度:β 建议用时:40 min 实际用时:4 h 题目:?? 代码:?? 这题有两个坑点首先要注意: 1)对于 "2000=" 要特判.应该判为 "IMPOSSIBLE" 2)枚举顺序为 "*+-" 然后是数字. 好了.这题的 DFS 过程很简单了.主要的是怎样计算一个字符串多项式(不带除号的). 代码链接:??(这里我把除号带上了,原理跟减号一样的) 从最简单的加法开始. 1+2+3 怎样计算上面的式子? 我选择用递归. 从左到右(不是遍历,

uva 646 - The Gourmet Club(暴力)

题目链接:uva 646 - The Gourmet Club 题目大意:有16个人参加聚会,聚会一共5天,每天有4桌,每桌4个人,一起吃饭的4个人会互相认识.现在要安排座位使得16个任意两个人都互相认识.给出前三天的安排,求后两天的安排. 解题思路:任意两个人之间肯定只能同桌一次.所以根据这个条件,只要枚举出第4天的第1桌的情况,就可推导出所有的,或者是矛盾. 在Poj和Zoj上都过了,uva上过不了,求大神指教. #include <stdio.h> #include <string

A. The Fault in Our Cubes 暴力dfs

http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行,就会早早退出. 这样写起来比较好写. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

【UVA】12169-Disgruntled Judge(暴力or欧几里得)

可能由于后台数据的原因,这道题直接暴力枚举a,b进行判断也能过,不过跑的时间长,效率太差了. 14021006 12169 Disgruntled Judge Accepted C++ 0.876 2014-08-11 08:46:28 不说了,比较无脑. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #incl

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK

hihoCoder 1185 连通性&#183;三(Tarjan缩点+暴力DFS)

#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出去,就拜托小Hi和小Ho忙帮放牧. 约翰家一共有N个草场,每个草场有容量为W[i]的牧草,N个草场之间有M条单向的路径. 小Hi和小Ho需要将牛羊群赶到草场上,当他们吃完一个草场牧草后,继续前往其他草场.当没有可以到达的草场或是能够到达的草场都已经被吃光了之后,小hi和小Ho就把牛羊群赶回家. 一开