zoukankan      html  css  js  c++  java
  • 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);
    			}
    		}
    		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
    ",res);
    	return 0;
    }
  • 相关阅读:
    vue 数字滚动的插件 vue-count-to
    ASP.NET Core EF 查询获取导航属性值,使用Include封装
    nginx在asp.net mvc项目中 配置 初步快速入门
    JQuery EasyUI 扩展方法 日期控件 设置时间段函数
    JQueryEasyUI easyui-combobox 单击文本区域显示下拉菜单
    Jquery Easy UI Datagrid 上下移动批量保存数据
    ASP.NET MVC BundleConfig介绍和使用
    ASP.NET MVC5 视图相关学习
    SqlDependency数据库同步+signalr 推送消息
    T4 模板自动生成带注释的实体类文件
  • 原文地址:https://www.cnblogs.com/FrankChen831X/p/10326078.html
Copyright © 2011-2022 走看看