Gym 100989L (DFS)
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)的更多相关文章
- dfs Gym - 100989L
AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...
- Gym - 100971J ——DFS
Statements Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- 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 ...
随机推荐
- TypeError: 'ExcelData' object is not iterable
今天写了个测试的代码,结果在执行test_register.py文件在调用readexcle.py的时候一直报错TypeError: 'ExcelData' object is not iterabl ...
- Netty使用Google的ProtoBuf
protobuf是由Google开发的一套对数据结构进行序列化的方法,可用做通信协议,数据存储格式,等等.其特点是不限语言.不限平台.扩展性强 Netty也提供了对Protobuf的天然支持,我们今天 ...
- 利用redis限制单个时间内某个mac地址的访问次数
一.思路 用户mac地址唯一,可以作为redis中的key,每次请求进来,利用ttl命令,判断redis中key的剩余时间,如果大于零,则利用incr进行+1操作,然后再与总的限制次数作对比. 二.代 ...
- spring data jpa删除的使用方式
public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(Str ...
- zt <Windows Image Acquisition (WIA)> from msdn
Windows Image Acquisition (WIA) Windows Image Acquisition (WIA) is the still image acquisition pla ...
- Linux下Spark框架配置(Python)
简述 Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapReduce所具有的优点:但不同于MapRedu ...
- unit_2_homework
随记2018/4/23 # 找元祖中的元素,移除每个元素的空格,并查找以a或A开头,c结尾的所有元素. # 思路:将i取出来,求得li列表中有多少个元素for i in range(len(li)): ...
- Linux实战教学笔记46:NoSQL数据库之redis持久化存储 (二)
第3章 Redis数据类型详解 3.1 Redis键/值介绍 Redis key值是二进制安全的,这意味着可以用任何二进制序列作为key值,从形如"foo"的简单字符串到一个JPG ...
- Phong & BlinnPhong Specular Shader
[Phong Specular Shader] 如果物体离摄像机很远,或者不需要高精度镜面反射,则Phong模型适用. Phong模型如下: 使用前必须指定使用自定义Phong. [BlinnPhon ...
- 利用Fiddler对Android模拟器网络请求进行抓包
安装使用Fiddler 下载安装Fiddler的方法这里就略过了,一路Next就行了.装好之后运行软件,正常情况这个时候我们已经可以对电脑的网络请求进行抓包了.Fiddler默认的代理地址是127.0 ...