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 ...
随机推荐
- maven将手动下载的jar包安装到本地仓库
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 「小程序JAVA实战」小程序视图之细说wx:key列表高级特性(16)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-16/ wx:key的高级特性.这个很重要,因为在app上经常有上拉,下拉加载,我们如果不使用这个特 ...
- maven-resources-plugin使用
命令行中带参数指定${}变量值 <build> <resources> <resource> <directory>src/main/resources ...
- Spring cloud Eureka高可用 - 配置
个人项目源码: https://github.com/easonstudy/cloud-demo/tree/master/eureka-peer-server Window 7 修改hosts ht ...
- 留用 未验证 js适配根字体大小
方法一:<script> (function (doc, win) { var docEl = doc.documentElem ...
- HTML怎么让img 等比例缩放
在img标签里面只设置宽,不设置高,图片就会等比例缩放.
- StringUtils.hasText()
StringUtils.hasText(字符串) 如果字符串里面的值为null, "", " ",那么返回值为false:否则为true
- 将maven打包为一个jar(可以体外加入jar)
使用 maven-compiler-plugin插件, 在maven的pom的<build></build>标签中上加入 <build> <plugins&g ...
- iOS学习之UITableView中Cell的操作
接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...
- Java多线程-线程的生命周期
线程可以分为4个状态:New(新生),Runnable(可运行):为了方便分析,还可将其分为:Runnable与Running.blocked(被阻塞),Dead(死亡). 与人有生老病死一样,线程也 ...