dfs Gym - 100989L
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.
Examples
7
1 + 1 - 4 - 4 - 4 - 2 - 2
3
3
5 + 3 - 7
-1 题目翻译:一串数字改变其的符号,让sum为0,输出最小的改变次数,不存在则输出-1 运用算法DFS ac代码:
#include<iostream>
using namespace std;
int a[25],n,ans;
void dfs(int index,int sum,int c)
{
if (index==n+1){
if (sum==0){
ans=ans<c?ans:c;
}
return ;
}
int j;
for (j=0;j<2;j++){ //只存在 + 或者是 - 两种情况
if (j==0){
if (a[index]<0)
dfs(index+1,sum-a[index],c+1);
else
dfs(index+1,sum+a[index],c);
}
else if (a[index]<0)
dfs(index+1,sum+a[index],c);
else
dfs(index+1,sum-a[index],c+1);
}
return ;
}
int main()
{
//scanf("%d",&n);
char op;
int i,j;
ans=99999999;
cin>>n;
for (i=1;i<=n;i++){
if (i==1)
scanf("%d",&a[i]);
else{
scanf(" %c %d",&op,&a[i]);
if (op=='-')
a[i]=-a[i];
}
}
dfs(2,a[1],0);
if (ans==99999999)
cout<<"-1\n";
else
cout<<ans<<endl;
return 0;
}
dfs Gym - 100989L的更多相关文章
- Gym 100989L (DFS)
AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...
- DFS Gym 100553J Jokewithpermutation
题目传送门 /* 题意:将字符串分割成一个全排列 DFS:搜索主要在一位数和两位数的处理,用d1, d2记录个数,在不饱和的情况下,两种都试一下 DFS还是写不来,难道是在家里懒? */ #inclu ...
- Gym - 100989L
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- Gym 100463D Evil DFS
Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
随机推荐
- SAP订单编排和流程增强概述
SAP产品里的订单处理,无论是On-Premises解决方案还是云产品,我认为归根到底可以概括成四个字:订单编排,包含两个层次的内容: 1. 单个订单通过业务流程或者工作流驱动的状态迁移: 2. 多种 ...
- 【转载】#438 - Benefits of Using Interfaces
You might wonder why you'd define an interface, have a class implement that interface and then acces ...
- 计算次数,POJ(1207)
题目链接:http://poj.org/problem?id=1207 #include <stdio.h> #include <algorithm> using namesp ...
- refinedet网络结构
refinedet只预测4个层,并且只有conv6_1.conv6_2,没有ssd中的conv7.8.9 refinedet的4个层都只有1个aspect ratio和1个min_size,所以每层每 ...
- HashMap面试知识点
HashMap的工作原理是近年来常见的Java面试题. 几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如 ...
- [luoguP1090][Noip2004]合并果子
合并果子 首先来看一下题目: (OI2004合并果子) [题目描述] 果园里,多多已经将所有的果子打了下来,而且按果子的 ...
- HDU 1175 连连看(超级经典的bfs之一)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others) ...
- centos 安装配置 rabbitmq 以及nginx转发
安装erlang cd /tmp wget http://erlang.org/download/otp_src_18.3.tar.gz . cd /opt/otp_src_18. yum -y in ...
- GET&&POST请求编码过程
编码.解码 我们在开发过程中不可避免的一个话题就是编码和解码,那么什么是编码什么是解码呢?为什么要进行编码和解码呢?下面我们一一分析! 编码和解码的概念 编码是信息从一种形式或格式转换为另一种形式的过 ...
- JNI 和 socket api
1.JavaVM 和 JNIEnvJNIEnv是一个与线程相关的变量,不同线程的JNIEnv彼此独立.JavaVM是虚拟机在JNI层的代表,在一个虚拟机进程中只有一个JavaVM,因此该进程的所有线程 ...