POJ 1950

直接DFS,因为实在没想到什么剪枝了...

注意一点是,10.11使用的是1011哦

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL __int64
using namespace std;
int n,ct,k;
char str[20];

void dfs(LL sum,LL last,int pos){
	if(pos==n+1){
		if(sum==0){
			ct++;
			if(ct<=20){
				for(int i=1;i<n;i++){
					printf("%d %c ",i,str[i]);
				}
				printf("%d\n",n);
			}
		}
		return ;
	}
	str[pos-1]=‘+‘;
	dfs(sum+pos,pos,pos+1);
	str[pos-1]=‘-‘;
	dfs(sum-pos,-pos,pos+1);
	str[pos-1]=‘.‘;
	if(pos>=10){
		k=100;
	}
	else k=10;
	if(last<0)
	dfs(sum-last+last*k-pos,last*k-pos,pos+1);
	else if(last>0){
		dfs(sum-last+last*k+pos,last*k+pos,pos+1);
	}
}

int main(){
	while(scanf("%d",&n)!=EOF){
		ct=0;
		dfs(1,1,2);   //sum,last,pos,char
		printf("%d\n",ct);
	}
	return 0;
}

  

时间: 2024-12-16 03:29:12

POJ 1950的相关文章

POJ 1950 Dessert

题目链接~~ 题意 : 给你 n ( 1 <= n <= 15) 个数(1 . 2 . 3 . 4 ....) 在这些数中间添加 + ,-  ,.,使得最终的计算结果为 0 ,如果情况不大于 20 种 ,则输出全部,否则最多输出 前20 种,按字典序输出. 解题思路 :这题和在广东区域赛的热身赛的一题差不多,深搜枚举所有情况,就是处理点的时候需要注意,同时处理的的情况大于等于 10 的数字应该乘 100 . 代码: #include <iostream> #include <

poj 1950 Dessert(dfs枚举)

/* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值,知道最后,这样不必每一次枚举都要从头再算一遍值! */ 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 7 char ch[20]; 8 char sign[

【POJ - 1950】Dessert(dfs)

-->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果num大于20,则只输出前20中情况.运算符‘.’相当于连接符,例如:11.22 <-> 1122. Sample Input 7 Sample Output 1 + 2 - 3 + 4 - 5 - 6 + 7 1 + 2 - 3 - 4 + 5 + 6 - 7 1 - 2 + 3 + 4 - 5

wewe

BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位大虾啦. pku 1175 Starry Night 题目地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1175 解法:BFS,要注意的是如何判断图形是一样的,我的做法就是计算每两个点的距离之和. 看:http://hi.baidu.com/doxi

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

POJ 3494 Largest Submatrix of All 1&#39;s 栈的运用 好题

Language: Default Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5185   Accepted: 1950 Case Time Limit: 2000MS Description Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest