poj 1950(搜索)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5430 | Accepted: 2029 |
Description
1 - 2 . 3 - 4 . 5 + 6 . 7
This means 1-23-45+67, which evaluates to 0. You job is to assist
the cows in getting dessert. (Note: "... 10 . 11 ...") will use the
number 1011 in its calculation.)
Input
Output
line of output for each of the first 20 possible expressions -- then a
line with a single integer that is the total number of possible answers.
Each expression line has the general format of number, space, napkin,
space, number, space, napkin, etc. etc. The output order is
lexicographic, with "+" coming before "-" coming before ".". If fewer
than 20 expressions can be formed, print all of the expressions.
Sample Input
7
Sample Output
1 + 2 - 3 + 4 - 5 - 6 + 7
1 + 2 - 3 - 4 + 5 + 6 - 7
1 - 2 + 3 + 4 - 5 + 6 - 7
1 - 2 - 3 - 4 - 5 + 6 + 7
1 - 2 . 3 + 4 + 5 + 6 + 7
1 - 2 . 3 - 4 . 5 + 6 . 7
6
题意:往 1 - n里面添加符号 + - . 问怎样使得结果为 0 ..输出方案数以及方案。。如果方案数>20,那么输出前20个方案。
题解:DFS太弱了。。不知道怎么处理点号。。参考别人的。多加练习深搜!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
int cnt,n;
int vis[];
char expe[];
void dfs(int deep,int ans,int pre)
{
if(deep==n)
{
if(ans==)
{
cnt++;
if(cnt<=)
{
for(int i=; i<n; i++)
{
printf("%d %c ",i,expe[i]);
}
printf("%d\n",n);
}
}
return;
}
else
{
int now,next,k;
expe[deep]='+';
dfs(deep+,ans+deep+,deep+); ///当前数为deep+1 ,由于是"+",得到的下一个数为ans+(deep+1)
expe[deep]='-';
dfs(deep+,ans-(deep+),deep+);
expe[deep]='.';
if(deep+>)
{
now = *pre+(deep+);
}
else now = *pre+(deep+);
int j = deep-;
while(expe[j]=='.'&&j>=) j--;
if(expe[j]=='+')
dfs(deep+,(ans-pre)+now,now); ///这里的话开始硬是没写出来
else dfs(deep+,(ans+pre)-now,now);
}
return;
}
int main()
{
int t = ;
while(scanf("%d",&n)!=EOF&&n)
{
cnt = ;
memset(vis,,sizeof(vis));
expe[] = '+'; ///0+????
dfs(,,);
printf("%d\n",cnt);
}
return ;
}
poj 1950(搜索)的更多相关文章
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- [Vjudge][POJ][Tony100K]搜索基础练习 - 全题解
目录 POJ 1426 POJ 1321 POJ 2718 POJ 3414 POJ 1416 POJ 2362 POJ 3126 POJ 3009 个人整了一些搜索的简单题目,大家可以clone来练 ...
- poj 2251 搜索
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13923 Accepted: 5424 D ...
- poj 1011 搜索减枝
题目链接:http://poj.org/problem?id=1011 #include<cstdio> #include<cstring> #include<algor ...
- 生日蛋糕 POJ - 1190 搜索 数学
http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...
- poj 2531 搜索剪枝
Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u ...
- POJ 3039 搜索??? (逼近)
思路: 抄的题解 这叫搜索? 难以理解 我觉得就是枚举+逼近 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- poj 折半搜索
poj2549 Sumsets 题目链接: http://poj.org/problem?id=2549 题意:给你一个含有n(n<=1000)个数的数列,问这个数列中是否存在四个不同的数a,b ...
- poj 1950 Dessert(dfs枚举,模拟运算过程)
/* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再 ...
随机推荐
- JDBC-防止SQL注入问题
String sql = "select * from user where name = '" + name + "' and password = '" ...
- cols
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- ccf 201712-4 行车路线(Python实现)
一.原题 问题描述 试题编号: 201712-4 试题名称: 行车路线 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明和小芳出去乡村玩,小明负责开车,小芳来导航. 小芳将 ...
- GoF23种设计模式之行为型模式之策略模式
传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 1概述 定义一系列算法,把它们一个个都封装起来,并且让它们可以相互 ...
- 用Python抓取并分析了1982场英雄联盟数据,教你开局前预测游戏对局胜负!
英雄联盟想必大多数读者不会陌生,这是一款来自拳头,由腾讯代理的大型网络游戏,现在一进网吧,你就能发现一大片玩英雄联盟的人.在2017年中国战队无缘鸟巢的世界总决赛后,一大片人选择了弃游,只是终究没躲过 ...
- for_each_node(node)
遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...
- 当列表推导式遇到lambda(匿名函数)
Python这么优雅的语言,我也是醉了...... 事情由一段代码引发,请看: 上述的列表推导式+lambda表达式+for循环,他们碰撞出来的结果搞的人晕头转向,咱们逐步来分析一下他们到底是个什么鬼 ...
- JAVA-基础(三)
Character 类型字符(Character)是围绕字符型(char)的一个简单的包装器.字符(Character)的构造函数如下:Character(char ch)这里ch指定了被创建的字符( ...
- luogu2761 软件补丁问题
状压最短路 #include <iostream> #include <cstring> #include <cstdio> #include <queue& ...
- GridView的RowCommand事件中获取每行控件的值
//获取当前行 GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent; //获取 ...