POJ1950----DFS
Dessert
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6193 | Accepted: 2299 |
Description
FJ has a new rule about the cows lining up for dinner. Not only must the N (3 <= N <= 15) cows line up for dinner in order, but they must place a napkin between each pair of cows with a "+", "-", or "." on it. In order to earn their dessert, the cow numbers and the napkins must form a numerical expression that evaluates to 0. The napkin with a "." enables the cows to build bigger numbers. Consider this equation for seven cows:
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
One line with a single integer, N
Output
One 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
思路:
采用DFS搜索解决。由于数字的位置是固定的,所以在符号的位置上不断地尝试三种符号即可。但这个dfs的实现需要有一定的技巧。最开始我是直接暴力搜的,通过不断搜索后面的符号来计算表达式的结果,这样大大增加了程序的执行时间。第一次提交960MS(题目限制是1000MS),十分危险地AC了。后来在网上看到了更好的解法,区别主要在于它在搜索的过程中把表达式的结果sum记录了下来,这样就大大提高了算法速度。对我原来的代码改进后提交,94MS!
代码如下:
#include<cstdio>
#include<cstdlib>
using namespace std;
int res=0;
char c[20];
int n;
//sum是表达式的结果,pre是上一个位置的值(带符号)
//pos是当前位置的值
void dfs(int sum,int pre,int pos){
if(pos==n+1){
if(sum==0){
res++;
if(res<=20){
for(int i=1;i<n;i++)
printf("%d %c ",i,c[i]);
printf("%d\n",n);
}
}
return;
}
c[pos-1]='+';
dfs(sum+pos,pos,pos+1);
c[pos-1]='-';
dfs(sum-pos,-pos,pos+1);
c[pos-1]='.';
int k;
if(pos<10)
k=10;
else
k=100;
if(pre<0)
dfs(sum-pre+pre*k-pos,pre*k-pos,pos+1);
else
dfs(sum-pre+pre*k+pos,pre*k+pos,pos+1);
}
int main(){
scanf("%d",&n);
dfs(1,1,2);
printf("%d\n",res);
return 0;
}
POJ1950----DFS的更多相关文章
- poj1950 Dessert(DFS)
题目链接 http://poj.org/problem?id=1950 题意 输入一个整数n(3<=n<=15),将1,2,..n顺序排列,在数字中间插入'+','-','.',这样会产生 ...
- 【POJ - 1950】Dessert(dfs)
-->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
随机推荐
- 廖雪峰Java1-2程序基础-5浮点数运算
1.浮点数运算的特点 很多浮点数无法精确表示 计算有误差 整型可以自动提升到浮点型 如0.1用二进制表示会是一个无限循环的小数.计算机不可能在有限内存中表示一个无限小数.因此浮点数不能精确表示.也造成 ...
- LightGBM优势总结
效率和内存上的提升 1) 在训练决策树计算切分点的增益时,xgboost采用预排序,即需要对每个样本的切分位置都要计算一遍,所以时间复杂度是O(#data). 而LightGBM则是将样本离散化为直方 ...
- matlab批量修改图片大小
转自:http://blog.csdn.net/cike0cop/article/details/53087995 %author:coplin %time:2016-10-10 %function: ...
- 让MySql支持Emoji表情(MySQL中4字节utf8字符保存方法)
手机端插入Emoji表情,保存到数据库时报错: Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' ...
- WCF服务部署
一.将WCF服务部署到IIS上 1.首先检测电脑上是否安装了IIS,一般来说Win7以上系统自带IIS 2.下面进行IIS服务的开启设置 控制面板=>打开或关闭Windos功能 3.勾选该窗口中 ...
- C++多线程同步之临界区(CriticalSection)
原文链接:http://blog.csdn.net/olansefengye1/article/details/53262917 一.Win32平台 1.相关头文件和接口 #include <w ...
- Python while 循环使用实例
while循环是在Python中的循环结构之一. while循环继续,直到表达式变为假.表达的是一个逻辑表达式,必须返回一个true或false值,本文章向码农介绍Python while 循环使用方 ...
- spark sql 的性能调优
Caching Data in Memory 其他调优参数
- django项目分页
测试版本 代码: # 测试分页users=[{'name':'alex{}'.format(i),'pwd':'aaa{}'.format(i)}for i in range(1,302)] def ...
- django中使用Ajax
内容: 1.Ajax原理与基本使用 2.Ajax发送get请求 3.Ajax发送post请求 4.Ajax上传文件 5.Ajax设置csrf_token 6.django序列化 参考:https:// ...