zoukankan      html  css  js  c++  java
  • poj 1564 && zoj 1711 Sum It Up (dfs)

    http://poj.org/problem?id=1564 

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1711 

      简单深搜,主要是这一句 if(!vis[i]&&data[i]<=sum&&(data[i]!=data[i-1]||i==pos))。

    确保两个连续的相同的数只有一次机会进入ans[],避免了结果的重复。

    code:

    #include<cstdio>
    #include<cstring>
    int data[15] ;
    int ans[15] ;
    bool vis[15] ;
    bool flag ;
    int n ;
    void dfs(int sum, int count, int pos){
        if(!sum){
            flag = true ;
            printf("%d", ans[0]) ;
            for(int i=1; i<count; i++)
                printf("+%d", ans[i]) ;
            printf("\n") ;
            return ;
        }
        for(int i=pos; i<n; i++){
            if(!vis[i]&&data[i]<=sum&&(data[i]!=data[i-1]||i==pos)){
                vis[i] = true ;
                ans[count] = data[i] ;
                dfs(sum-data[i], count+1, i+1) ;
                vis[i] = false ;
            }
        }
    }
    int main(){
        int t, i, j ;
        while(~scanf("%d%d", &t, &n)&&t+n){
            memset(vis, falsesizeof(vis)) ;
            for(i=0; i<n; i++){
                scanf("%d", &data[i]) ;
                if(data[i]>t)  vis[i] = true ;
            }
            printf("Sums of %d:\n", t) ;
            flag = false ;
            dfs(t, 00) ;
            if(!flag)  printf("NONE\n") ;
        }
        return 0 ;} 
  • 相关阅读:
    迭代器模式
    命令模式
    模板方法
    springmvc执行原理及自定义mvc框架
    代理模式
    外观模式
    组合模式
    装饰器模式
    02——Solr学习之Solr安装与配置(linux上的安装)
    01——Solr学习之全文检索服务系统的基础认识
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2390644.html
Copyright © 2011-2022 走看看