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 ...
随机推荐
- OD 实验(十七) - 对一个程序的逆向分析
程序: 运行程序 弹出一个对话框,点击 OK 来到主界面,点击 Help -> Register Now 这是输入注册码的地方 按关闭程序的按钮 会提示剩下 30 天的使用时间 用 Ressco ...
- Mysql Docker Container Command
Hello, in my docker-compose file I have the following: db: image: mysql command: mysqld --character- ...
- Python环境搭建之OpenGL
以下内容为我python OpenGl 环境搭建历程: win7 64位操作系统,python3.5.3 ,无其他相关. 直接cmd或PowerShell输入以下命令: pip install PyO ...
- 使用Dottrace跟踪.net代码执行时间
当程序遇到性能问题,如IIs请求反应缓慢,.net客户端程序执行缓慢,如何分析是哪里出了问题?dottrace可以帮助.net程序跟踪出代码里每个方法的执行时间,清晰的看出是哪里执行时间过长,然后再分 ...
- 【Oracle】Oracle改变日志归档模式
一.确认工作模式: 1.查询V$DATABASE SQL>select log_mode from v$database; 归档日志:ARCHIVELOG 2.执 ...
- C++防止文件重复包含
引用自:https://blog.csdn.net/xhfight/article/details/51550446 为了避免同一个文件被include多次,C/C++中有两种方式,一种是#ifnde ...
- PHP 动态添加 Mcrypt 扩展库
简介: PHP 动态添加 Mcrypt 扩展库,这是一个支持多种加密.解密算法.模式的扩展库. shell > php -m | grep mcrypt # 如果没有输出,就是缺少这个扩展 sh ...
- Python与Go斐波那契数列
#!/usr/bin/env python # -*- coding: utf-8 -*- # 斐波那契数列 def fibonacci_sequence(num): aa = 0 b = 1 li ...
- C#给图片加文字水印
public class TxtWaterMark { public enum WaterPositionMode { LeftTop,//左上 LeftBottom,//左下 RightTop,// ...
- 【codevs3160】 LCS 【后缀自动机】
题意 给出两个字符串,求它们的最长公共子串. 分析 后缀自动机的基础应用. 比如两个字符串s1和s2,我们把s1建为SAM,然后根据s2跑,找出s2每个前缀的最长公共后缀. 我们可以理解为,当向尾部增 ...