【UVA】10317 - Equating Equations(dfs + 剪枝)

真郁闷,一道普通的搜索题 我拿dp的方法去做,结果一直TLE和WA

如果所有数的和为奇数,肯定没有正解。

14133454 10317 Equating Equations Accepted C++ 0.102 2014-09-02 09:01:23

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
#define MAXD 110
#define _PI acos(-1.0)
int array[MAXD];
int _ansx[MAXD],_ansy[MAXD];
int _x , _y;
int size , add ;
int aim;
int vis[MAXD];
int  _operator[MAXD];
bool dfs(int ans,int cur,int pos){
    if(cur == add){
        if(ans == aim)
            return true;
        else
            return false;
    }
    for(int i = pos ; i < size ; i++)if(!vis[i]){
        vis[i] = 1;
        if(ans + array[i] <= aim && dfs(ans + array[i],cur + 1,i + 1))
            return true;
        vis[i] = 0;
    }
    return false;
}
void init(){
    for(int i = 0 ; i < size ; i++){
        if(vis[i]){
            _ansx[_x ++] = array[i];
        }
        else{
            _ansy[_y ++] = array[i];
        }
    }
}
int main(){
    char oper[MAXD];
    char m;
    while(scanf("%d",&array[0]) != EOF){
         _operator[0] = 1;
         memset(vis,0,sizeof(vis));
         size = 1; _x = 0 ; _y = 0;
         add  = 1;
         int p = 0;
         int pos ,sum = array[0];
         while((m = getchar())){
             if(m == '\n') break;
             scanf("%s",oper);
             if(oper[0] == '+'){
                if(!p){
                add ++;
                _operator[size] = 1;
                }
                else{
                _operator[size] = -1;
                }
             }
             if(oper[0] == '-'){
                if(!p){
                _operator[size] = -1;
                }
                else{
                _operator[size] = 1;
                add++;
                }
             }
             if(oper[0] == '='){
                p = 1;
                pos = size;
                _operator[size] = -1;
             }
             scanf("%d",&array[size++]);
             sum += array[size - 1];
         }
         p = 0;
         aim = sum / 2;
         if(sum % 2 == 0 && dfs(0,0,0)){
            init();
            for(int i = 0 , p1 = 0 , q1 = 0 ; i < size ; i++){
                  if(i == 0)
                      printf("%d",_ansx[p1++]);
                  else if(i == pos){
                      p = 1;
                      printf(" = %d",_ansy[q1++]);
                  }
                  else if(_operator[i] == 1){
                      if(!p)
                        printf(" + %d",_ansx[p1++]);
                      else
                        printf(" - %d",_ansx[p1++]);
                  }
                  else if(_operator[i] == -1){
                      if(!p)
                        printf(" - %d",_ansy[q1++]);
                      else
                        printf(" + %d",_ansy[q1++]);
                  }
             }
             printf("\n");
         }
         else
            printf("no solution\n");
    }
    return 0;
}
时间: 2024-07-28 15:14:17

【UVA】10317 - Equating Equations(dfs + 剪枝)的相关文章

UVA 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

UVa 208 消防车(dfs+剪枝)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=144 题意:给出一个n个结点的无向图以及某个结点k,按照字典序从小到大顺序输出从1到结点k的所有路径. 思路:如果直接矩阵深搜的话是会超时的,所以我们可以从终点出发,将与终点相连的连通块保存起来,这样dfs深搜时可以剪枝掉一些到达不了的点.只要解决了这个,dfs就是小问题. 这道题还有点坑的

uva 219 - Department of Redundancy Department(dfs+剪枝)

题目链接:uva 219 - Department of Redundancy Department 题目大意:给定一些关系,问哪一些关系是可以被替代的,如果可以被替代,给出替代的方案,一种即可. 解题思路:因为总共也就26个字母,所以用二进制数表示状态.剪枝,每次将所有可选关系均考虑进去都无法满足则是false. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

UVA10317- Equating Equations(回溯+剪枝)

题目链接 题意:给出一个式子,但这个式子不一定是等式,在'+','-'符号位置不变的情况下,重新排列数字的位置,使其成为等式,如果可以的话,输出其中一种排列方式. 思路:我们将等号右边的数全部移动到等号右边,例如a+b-c=d-e,移动后变成a+b+e-(c+d)=0,也就是a+b+e=c+d,所以当式子可以变化成等式时,所有数的和必然是偶数.那么问题可以转化为在n个数中找出m个数(m的值为等号左边的整数的数量),使m个 数的和为从和的一半. #include <iostream> #incl

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

Cubes(DFS+剪枝)

题意:给一个数N,求N最少由多少个数的立方构成,并输出这些数. 做法:DFS + 剪枝,剪枝的边界很很很重要! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> int cub[400]; int ans[300]; int tp[300]; int n; int sum = 0x3f3f3f3f; //个数 void dfs(int le

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const