UASCO Zero Sum DFS + Stack

给一个N 表示1 2 3 ...N

求出所有 zero sum的情况

【简单Dfs 即可】 运算结果的时候我使用了一个stack...

比如N = 7

那么要求输出

1+2-3+4-5-6+7
1+2-3-4+5+6-7
1-2 3+4+5+6+7
1-2 3-4 5+6 7
1-2+3+4-5+6-7
1-2-3-4-5+6+7

Source Code:
/*
ID: wushuai2
PROG: zerosum
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)
#define RV(num) ((num) > 0 ? 0 : 1)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 660000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;
const int MAXSIZE = 101000000;

ofstream fout ("zerosum.out");
ifstream fin ("zerosum.in");

int N, b[11];
void solve(){
    int i, j, t;
    vector <int> vec;
    vec.push_back(1);
    for(i = 2; i <= N; ++i){
        int num = vec[vec.size() - 1];
        if(num < 0){
            num = vec[vec.size() - 2];
        }
        if(b[i] == 1){
            vec.pop_back();
            vec.push_back(num * 10 + i);
        } else if(b[i] == 2){
            vec.push_back(-1);
            vec.push_back(i);
        } else if(b[i] == 3){
            vec.push_back(-2);
            vec.push_back(i);
        } else{
            vec.push_back(i);
        }
    }

    int ans = vec[0];
    for(i = 1; i < vec.size(); i += 2){
        if(vec[i] == -1){
            ans += vec[i + 1];
        } else if(vec[i] == -2){
            ans -= vec[i + 1];
        }
    }
    if(ans == 0){
        fout << 1;
        for(i = 2; i <= N; ++i){
            if(b[i] == 1)   fout << ‘ ‘;
            else if(b[i] == 2)  fout << ‘+‘;
            else if(b[i] == 3)  fout << ‘-‘;
            fout << i;
        }
        fout << endl;
    }
}

void change(int n){
    int i, j;
    if(n == N + 1){
        solve();
        return;
    }
    for(i = 1; i <= 3; ++i){
        b[n] = i;
        change(n + 1);
    }
}

int main() {
    int i, j, k, l, m, n, t, s, c, w, q, num;
    fin >> N;
    for(i = 2; i <= N; ++i){
        b[i] = 1;
    }
    change(2);

    fin.close();
    fout.close();
    return 0;
}
时间: 2024-11-03 06:02:05

UASCO Zero Sum DFS + Stack的相关文章

[email&#160;protected] [124] Binary Tree Maximum Path Sum (DFS)

https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connection

(tarjan/+DFS+反向建图) hdu 3639

G - Hawk-and-Chicken Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3639 Description Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: ev

HDOj-2209-翻纸牌游戏-DFS

翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2622    Accepted Submission(s): 943 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一

小火山的宝藏收益 多校训练2(小火山专场) poj(邻接表+DFS)

http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1907 Description 进去宝藏后, 小火山发现宝藏有N个房间,且这n个房间通过N-1道门联通. 每一个房间都有一个价值为Ai的宝藏, 但是每一个房间也都存在一个机关.如果小火山取走了这个房间的宝藏,那么这个房间通往其他房间的门就永远打不开了,也就是说后面的宝藏小火山是得不到了(进入这个房间的门是不会关闭的,小火山还是可以回去的):如果小火山不取这个宝藏,而是去打开通往另一房间的门,那么这个

【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse

Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contr

POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

题目链接:POJ 3628 Bookshelf 2 Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3436 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly,

hdu 1501 Zipper dfs

题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.For examp

hdu1501&amp;&amp;poj2192 Zipper(DFS)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1501 POJ:   http://poj.org/problem?id=2192 Zipper Description Given three strings, you are to determine whether the third string can be formed by combining the

记忆化搜索hdu1078 dfs

http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 using namespace std; 5 int dp[110][110],s[110][110]; 6 int n,k,t[4][2]= {1,0,-1,