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,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再 ...
随机推荐
- 04 Django模板
基本概念 作为Web框架,Django提供了模板,用于编写html代码,还可以嵌入模板代码更快更方便的完成页面开发,再通过在视图中渲染模板,将生成最终的html字符串返回给客户端浏览器 模版致力于表达 ...
- JSON Web Token(JWT)的详解
1.传统身份验证和JWT的身份验证 传统身份验证: HTTP 是一种没有状态的协议,也就是它并不知道是谁是访问应用.这里我们把用户看成是客户端,客户端使用用户名还有密码通过了身份验证,不过下回这个客户 ...
- Python3爬虫一之(urllib库)
urllib库是python3的内置HTTP请求库. ython2中urllib分为 urllib2.urllib两个库来发送请求,但是在python3中只有一个urllib库,方便了许多. urll ...
- CentOS7搭建DNS服务器
DNS是域名系统(Domain Name System)的缩写,它的作用是将主机名解析成IP(正向解析),从IP地址查询其主机名(反向解析). DNS的工作原理(1)客户机发出查询请求当被询问到有关本 ...
- loadView、viewDidLoad及viewDidUnload的关系(转)
本文目录 一.loadView 二.viewDidLoad 三.viewDidUnload 四.三个方法的关系 标题中所说的3个方法,都是UIViewController的方法,跟UIViewCont ...
- P2014 选课(树形背包)
P2014 选课 题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有 ...
- [转] vuex最简单、最直白、最全的入门文档
前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...
- js---post与get请求的区别
request获取请求参数 最为常见的客户端传递参数方式有两种: 浏览器地址栏直接输入:一定是GET请求: 超链接:一定是GET请求: 表单:可以是GET,也可以是POST,这取决与<form& ...
- luogu2765 魔术球问题
发现好像没人来证明贪心啊--那我来写一下它的证明 欲证明:放一个数在已有的柱上(如果可以)总是比新开一个柱更优的 假如已经放了x1..x2....xu..xv..xw.... 现在我要放xx 我有两种 ...
- 深入浅出理解Javascript原型概念以及继承机制(转)
在Javascript语言中,原型是一个经常被讨论到但是有非常让初学者不解的概念.那么,到底该怎么去给原型定义呢?不急,在了解是什么之前,我们不妨先来看下为什么. Javascript最开始是网景公司 ...