Dessert
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5430   Accepted: 2029

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

题意:往 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(搜索)的更多相关文章

  1. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  2. [Vjudge][POJ][Tony100K]搜索基础练习 - 全题解

    目录 POJ 1426 POJ 1321 POJ 2718 POJ 3414 POJ 1416 POJ 2362 POJ 3126 POJ 3009 个人整了一些搜索的简单题目,大家可以clone来练 ...

  3. poj 2251 搜索

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13923   Accepted: 5424 D ...

  4. poj 1011 搜索减枝

    题目链接:http://poj.org/problem?id=1011 #include<cstdio> #include<cstring> #include<algor ...

  5. 生日蛋糕 POJ - 1190 搜索 数学

    http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...

  6. poj 2531 搜索剪枝

    Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u ...

  7. POJ 3039 搜索??? (逼近)

    思路: 抄的题解 这叫搜索? 难以理解 我觉得就是枚举+逼近 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  8. poj 折半搜索

    poj2549 Sumsets 题目链接: http://poj.org/problem?id=2549 题意:给你一个含有n(n<=1000)个数的数列,问这个数列中是否存在四个不同的数a,b ...

  9. poj 1950 Dessert(dfs枚举,模拟运算过程)

    /* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再 ...

随机推荐

  1. 03Qt信号与槽(2)

    1. 元对象工具 ​ 元对象编译器 MOC(meta object compiler)对 C++ 文件中的类声明进行分析并产生用于初始化元对象的 C++ 代码,元对象包含全部信号和槽的名字及指向这些函 ...

  2. Python基础——模块与包

    在Python中,可以用import导入需要的模块.包.库.文件等. 把工作路径导入系统路径 import os#os是工作台 import sys#sys是系统 sys.path.append(os ...

  3. GoF23种设计模式之行为型模式之状态模式

    一.概述         定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 二.适用性 1.一个对象的行为取决于它的状态,并且它必须在运行时刻 ...

  4. python数据类型之列表(list)和其常用方法

    列表是python常用数据类型之一,是可变的,可由n = []创建,也可由n = list()创建,第一种方法更常用. 常用方法总结: # 创建方法 n = [] 或者 n = list() # in ...

  5. writing a usb driver(在国外的网站上复制下来的)

    Writing a Simple USB Driver   From Issue #120April 2004 Apr 01, 2004  By Greg Kroah-Hartman  in Soft ...

  6. 基于IAR6或者IAR7建立STM32开发工程(通过实际测试,使用IAR6.30.4)

    IAR和keil两个开发平台都是arm开发当中比较流行的平台,keil4的版本之间,可以兼容,但是版本4和版本5还是不兼容的,但是IAR的兼容性更加差,好像6.30.x之间是能够兼容的吧,没有实测过, ...

  7. POJ:2955-Brackets(经典:括号匹配)

    传送门:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS Memory Limit: 65536K Description We g ...

  8. HDU 4781 Assignment For Princess 构造

    题意: 构造一个\(N(10 \leq N \leq 80)\)个顶点\(M(N+3 \leq M \leq \frac{N^2} {7})\)条边的有向图,要满足如下条件: 每条边有一个\([1,M ...

  9. 和为n连续正数序列 【微软面试100题 第五十一题】

    题目要求: 输入一个正数n,输出所有和为n连续正数序列(至少两个). 例如输入15,由于1+2+3+4+5 = 4+5+6 = 7+8 = 15.所以输出3个连续序列1~5,4~6,7~8. 参考资料 ...

  10. 聊聊、Docker 安装