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 ...
随机推荐
- 查看Android内存,cpu
转自https://testerhome.com/topics/2583 一.查看内存 查看Android应用内存: adb shell dumpsys meminfo 1.查看详细的内存: adb ...
- [Python] WeChat_Robot
在微信中接入一个聊天机器人 1. WeChat 个人接口itchat 2. 图灵机器人 #-*- coding:utf-8 -*- import itchat import requests apiU ...
- FastQC 测序质量
文章转载于 Original 2017-07-06 Jolvii 生信百科 介绍一下如何理解 FastQC 各模块的结果 FastQC 的使用 FastQC的安装介绍请看这里.FastQC 支持 fa ...
- azkaban平台的使用
最近接触一些大数据的测试,有些hadoop/spark任务在服务器测试不太方便,会放到azkaban上跑 简单写下azkaband的使用流程:包括任务的上传和提交任务到hadoop集群 一 登陆azk ...
- 构造方法PK实例方法
1.构造方法 (1)用于对象初始化,一个类中至少有一个构造方法 (2)不能显示调用,只能在创建对象时,使用new来调用 (3)构造方法不能有返回值 (4)构造方法名称必须与类名一样 2.实例方法 (1 ...
- Rhythmk 一步一步学 JAVA (16) dom4j 操作XML
1.项目文件结构图: 2.文件代码: doc.xml <?xml version="1.0" encoding="UTF-8"?> <Shop ...
- mybatis与springdata的一些简单比较与思考
主题 最近在用mybatis做项目,有一些感触想记录下,主要是mybatis(以及它的一些插件)相比较于Spring data(或者jpa,hibernate等)的优势地方. 感触 我觉得mybati ...
- 71. Simplify Path压缩文件的绝对路径
[抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...
- Linux的作业管理
一.作业管理的场景 作业管理(job control)是在bash环境下使用的,主要使用在同一个bash中管理多个作业的场景,譬如登录bash之后想同时复制文件.数据搜索,编译. 但是bash的作业管 ...
- spring4-2-bean配置-4-bean之间的关系