AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tries to make it correct as quickly as possible!

Given an equation of the form: A1 o A2 o A3 o ... o An  =  0, where o is either + or -. Your task is to help AbdelKader find the minimum number of changes to the operators + and -, such that the equation becomes correct.

You are allowed to replace any number of pluses with minuses, and any number of minuses with pluses.

Input

The first line of input contains an integer N (2 ≤ N ≤ 20), the number of terms in the equation.

The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 108.

Values and operators are separated by a single space.

Output

If it is impossible to make the equation correct by replacing operators, print  - 1, otherwise print the minimum number of needed changes.

题意:给出了你一个数字n,和一个由n个数字以及n-1个加减运算符组成的算式,你可以把 ‘+’ 变成 ‘-’,或者把‘-’变成加‘+’。问你最少改变几个符号可以使算式等于0。注意:符号和数字之间用空格隔开。

思路:这道题可以用DFS来解决,枚举所有符号的改变情况,每个符号只有两种情况,改变或者不变。递归每个符号的两种情况,DFS(不变),DFS(改变),记录递归的层数,当递归到最后一个符号时判断运算结果是否符合要求,记录符合要求的最小改变次数。具体步骤看代码中的标注。

 #include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int minn;
int n,num[],num1,num2;
char str[]; int compute() //计算结果是否为0
{
int it=,ans = num[];
while(it < num2)
{
if(str[it] == '+')
ans += num[it+];
else
ans -= num[it+];
it++;
}
if(ans == ) return ;
else return ;
} void dfs(int times,int change) //times表示判断第几个符号,change表示改变了多少次
{
if(times >= num2) //如果已经判断完所有的符号了,就计算结果
{
if(change < minn && compute()) //如果改变的次数比之前记录的小且计算结果符合要求
{
minn = change; //就重新记录最小值
}
return;
} dfs(times+,change); //递归不改变这个符号的额情况 if(str[times] == '+') //改变符号
str[times] = '-';
else str[times] = '+';
dfs(times+,change+); //递归改变符号的情况,改变数加1
if(str[times] == '+') //回溯,将改变的的符号还原
str[times] = '-';
else str[times] = '+';
return;
} int main()
{
while(cin>>n)
{
num1=,num2=;
minn = inf; for(int i=; i<=n*-; ++i) //注意输入方式,输入一个数字,一个运算符,总共2*n-1个,数字和字母之间有空格
{
if(i%==) // 如果是奇数位置,则输入数字
{
scanf("%d",&num[num1++]);
getchar();//吸收空格
}
else scanf("%c",&str[num2++]);//输入运算符
}
dfs(,);
if(minn == inf)
cout<<"-1"<<endl;
else
cout<<minn<<endl;
}
return ;
}

Gym 100989L (DFS)的更多相关文章

  1. dfs Gym - 100989L

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  2. Gym - 100971J ——DFS

    Statements Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, ...

  3. Random Numbers Gym - 101466K dfs序+线段树

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  4. Gym - 100989L

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...

  5. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  6. Gym 100463D Evil DFS

    Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...

  7. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  8. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  9. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...

随机推荐

  1. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  2. 基于OpenCV读取摄像头进行人脸检测和人脸识别

    前段时间使用OpenCV的库函数实现了人脸检测和人脸识别,笔者的实验环境为VS2010+OpenCV2.4.4,opencv的环境配置网上有很多,不再赘述.检测的代码网上很多,记不清楚从哪儿copy的 ...

  3. maven引入源码

    选中要添加的源码的项目右键-->debug--->debugs-configurations-->source-->java project

  4. Python实践练习:在 Wiki 标记中添加无序列表

    题目描述 项目:在 Wiki 标记中添加无序列表 在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你 ...

  5. Sping实战之通过JAVA代码装配Bean

    尽管在很多场景下通过组件扫描和自动装配实现Spring的自动化配置是更为推荐的方式,但有时候自动化配置的方案行不通,因此需要明确配置Spring.比如说,你想要将第三方库中的组件装配到你的应用中,在这 ...

  6. Bootstrap 与 Jquery validate 结合使用——多个规则实现

    进行开发的时候,遇到了需要有多个规则来校验,如新用户过来一套校验规则,老用户过来又是一套规则,这时候就要需要定义多套校验规则. 首先要熟悉Bootstrap和Jquery validate的使用,详情 ...

  7. Python之路,Day9 , IO多路复用(番外篇)

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...

  8. STL : List使用时应注意的问题

    这篇文章所述只是本人遇到的问题,仅供参考. #include<list> #include<iostream> using namespace std; class Foo { ...

  9. 解决编译错误 implicit declaration of function 'strptime'

    根据man手册,在文件中加上以下定义,应该可以去处该warning        #define _XOPEN_SOURCE /* glibc2 needs this */        #inclu ...

  10. iOS设计模式(01):观察者

    iOS设计模式(01):观察者 iOS-Observer-Pattern 什么是观察者模式 什么是观察者模式?你曾经订阅过报纸吗?在订阅报纸的时候,你不用去任何地方,只需要将你的个人地址信息以及订阅信 ...