poj 1950
来源:互联网 发布:硬件和软件 哪个 编辑:程序博客网 时间:2024/05/16 19:54
题目链接:http://poj.org/problem?id=1950
dfs完全不会。。。知道思路,写不出来,没用,能写出来,不熟,没用。。。
//1950Accepted164K94MSC1165B#include <stdio.h>#include <stdlib.h>#include <string.h>#define M 20int n;char str[M];int ans;void dfs(int dep,int dep_pre_pre_sum,int dep_pre,int add_sub){ if(dep == n+1) { if(dep_pre_pre_sum + add_sub * dep_pre == 0) { if( ans < 20 )//输出前20个 { int i; for( i = 0; i< dep -2; i++ ) printf( "%d %c ", i+1, str[i] ); printf("%d\n",i+1); } ans ++; } return ; } str[dep - 2] = '+'; //比如:有1-2-3-4搜到第四层时,dep_pre_pre_sum保留的是1-2的结果,dep_pre是3的值 //搜索下一层,即1-2-3-4-5时,dep_pre_pre_sum该保留1---3的结果, //故用dep_pre_pre_sum + dep_pre * add_sub即1--3层来更新dep_pre_pre_sum,dep更新dep_pre dfs(dep+1,dep_pre_pre_sum + dep_pre * add_sub,dep,1); str[dep - 2] = '-'; dfs(dep+1,dep_pre_pre_sum + dep_pre * add_sub,dep,-1); str[dep - 2] = '.'; if(dep < 10) { dfs(dep+1,dep_pre_pre_sum,dep_pre*10 + dep,add_sub); } else //10以上 8.10 ==>8*100+10 { dfs(dep+1,dep_pre_pre_sum,dep_pre*100 + dep,add_sub); }}//思路是:dfs//比如1--2--3---4---5--6---7搜索到第7层,使用用1-----5的结果+/-/.上 6和7运算结果,所以要照顾3层int main(){ int dep; //深度 int add_sub; //是加还是减 int dep_pre; //当前搜索深度的前一个 例子中的6 int dep_pre_pre_sum; //例子中的1----5层运算后的结果 while(scanf("%d",&n) != EOF) { ans = 0; dep = 0; add_sub = 0; dep_pre = 0; dep_pre_pre_sum = 0; memset(str,0,sizeof(str)); dfs(2,0,1,1); printf("%d\n",ans); } return 0;}
- POJ 1950
- POJ 1950
- poj 1950
- POJ 1950
- poj 1950 数字dfs
- POJ 1950 Dessert
- poj 1950 Desser dfs
- poj 1950 Dessert
- poj 1950 Dessert
- poj 1950 Dessert 深搜
- POJ 1950 Dessert
- POJ 1950 desserts
- POJ 1950暴搜
- 强势回归!poj 1950
- POJ 1950 Dessert
- POJ 1950 Dessert 笔记
- POJ
- poj
- WinCE 插上网线后3G拨号 拨上号但是无法通过3G模块连接网络
- zju 1013 dp
- Ofbiz10.04改用mysql数据库配置
- 【Qt开发】Qt中的中文字符问题
- c# 调用 研华库函数中 C++ 非托管 Dll 一例(包含指针成员的结构体的调用)
- poj 1950
- Win7安装VS2008 SP1卡在 VS90sp1-KB945140-X86-CHS 的解决方法
- C#中调用C++写的DLL注意事项
- asp.net sitemappath站点地图
- uva-10010 - Where's Waldorf?
- 预处理
- Struts2学习笔记(十四) 输入校验(下)
- 创建Eclipse Editor
- jQuery插件AjaxFileUpload