首页 > 代码库 > 【UVA】10317 - Equating Equations(dfs + 剪枝)

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

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

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

1413345410317Equating EquationsAcceptedC++0.1022014-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;
}

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