Skip to main content
  1. Posts/

POJ 1564 Sum It Up (DFS+剪枝)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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

dfs

三个参数 x,sum,k,   x表示开始的坐标,sum表示当前的和,k表示这是一组答案中的第几个数,是用来记录路径的…

调了好久没写出来…我写完之后答案会有重复.一开始想开一个boolean数组记录,这样第一组样例的3+1就只会输出一遍,但是这样,2+2就不会被记录到答案中了.

然后看了下别人的代码…

卧槽,只是加了个判断…当前的数和上一个如果不同,就继续dfs….

我为何就没想到…这特么是判断重复的直译啊….

 1
 2    /*************************************************************************
 3    	> File Name: code/2015summer/0714/K.cpp
 4    	> Author: 111qqz
 5    	> Email: rkz2013@126.com
 6    	> Created Time: 2015年07月16日 星期四 01时03分01秒
 7     ************************************************************************/
 8
 9    #include<iostream>
10    #include<iomanip>
11    #include<cstdio>
12    #include<algorithm>
13    #include<cmath>
14    #include<cstring>
15    #include<string>
16    #include<map>
17    #include<set>
18    #include<queue>
19    #include<vector>
20    #include<stack>
21    using namespace std;
22    #define REP(i, n) for (int i=0;i<int(n);++i)
23    typedef long long LL;
24    typedef unsigned long long ULL;
25    const int N=20;
26    int n,t;
27    int a[N];
28    int ans;
29    int k;
30    int rec[N];
31    bool vis[105];
32    bool ok;
33    void dfs(int x,int sum,int k)
34    {
35        if (sum==t)
36        {
37    	  ok=true;
38    	  for ( int i = 0 ; i < k ; i++)
39    	  {
40    		if (i)
41    		{
42    		    printf("+%d",rec[i]);
43    		}
44    		else
45    		{
46    		    printf("%d",rec[i]);
47    		}
48    	  }
49    	  printf("\n");
50    	  return;
51        }
52        int pre = -1;
53        for ( int i = x ; i < n ; i++)
54        {
55    	  if (sum+a[i]<=t&&a[i]!=pre)
56    	  {
57    		pre = a[i];
58    		rec[k] = a[i];
59    		dfs(i+1,sum+a[i],k+1);
60    	  }
61        }
62    }
63    int main()
64    {
65        while (scanf("%d %d",&t,&n)!=EOF&&n)
66        {
67    	   ok = false;
68    	  memset(vis,false,sizeof(vis));
69    	  ans  = 0 ;
70    	  k = 0;
71    	  for ( int i = 0 ; i < n ; i++)
72    		scanf("%d",&a[i]);
73    	  printf("Sums of %d:\n",t);
74    	  dfs(0,0,0);
75    	  if (!ok) printf("NONE\n");
76        }
77
78    	return 0;
79    }

Related

cf 556C Case of Matryoshkas

·1 min
http://codeforces.com/contest/556/problem/C 果然一晚上不睡觉会导致读错题么… 需要注意的是 如果有一个是 1 2 4 6 那么 1,2是不必拆开的….

最大连续区间和的算法总结

·2 mins
最大连续区间和是一个经典的问题。给定一个长度为 n 的序列 a[1],a[2]…a[n-1],a[n],求一个连续的子序列 a[i],a[i+1]…a[j-1],a[j],使得 a[i]+a[i+1]…a[j-1]+a[j]最大。

poj 3278 catch that cow

·1 min
http://poj.org/problem?id=3278 bfs,用到了stl的queue 1 2 3 /* *********************************************** 4 Author :111qqz 5 Created Time :2016年02月19日 星期五 15时45分05秒 6 File Name :3278.cpp 7 ************************************************ */ 8 9 #include <algorithm> 10 #include <cstdio> 11 #include <iostream> 12 #include <cstring> 13 #include <string> 14 #include <cmath> 15 #include <map> 16 #include <stack> 17 #include <queue> 18 19 using namespace std; 20 typedef long long LL; 21 const int inf = 8E8; 22 const int N=2E5+7; 23 int d[N]; 24 int n,k; 25 void bfs() 26 { 27 queue<int> q; 28 memset(d,-1,sizeof(d)); 29 q.push(n); 30 d[n]=0; 31 while (!q.empty()) 32 { 33 int x = q.front(); 34 q.pop(); 35 if ( x==k ) 36 { 37 break; 38 } 39 int next[10]; 40 next[1]=x-1; 41 next[2]=x+1; 42 next[3]=2*x; 43 for ( int i = 1; i <= 3 ; i++ ) 44 { 45 if (next[i]>=0&&next[i]<=100000&&d[next[i]]==-1) 46 { 47 d[next[i]]=d[x]+1; 48 q.push(next[i]); 49 } 50 } 51 } 52 53 54 } 55 int main() 56 { 57 while (scanf("%d %d",&n,&k)!=EOF) 58 { 59 bfs(); 60 cout<<d[k]<<endl; 61 } 62 return 0; 63 }

POJ 1028 Web Navigation

·1 min
http://poj.org/problem?id=1028 1 2 3 4 /* *********************************************** 5 Author :111qqz 6 Created Time :2016年02月19日 星期五 15时45分01秒 7 File Name :1028.cpp 8 ************************************************ */ 9 10 #include <algorithm> 11 #include <cstdio> 12 #include <iostream> 13 #include <cstring> 14 #include <string> 15 #include <cmath> 16 #include <map> 17 #include <stack> 18 #include <queue> 19 20 using namespace std; 21 typedef long long LL; 22 const int inf = 8E8; 23 stack<string> backstack; 24 stack<string> forwardstack; 25 string cur; 26 string cmd; 27 int main() 28 { 29 cur ="http://www.acm.org/"; 30 31 while (cin>>cmd) 32 { 33 if (cmd=="QUIT") 34 { 35 break; 36 } 37 if (cmd=="BACK") 38 { 39 if (backstack.empty()) 40 { 41 cout<<"Ignored"<<endl; 42 continue; 43 } 44 forwardstack.push(cur); 45 cur=backstack.top(); 46 backstack.pop(); 47 cout<<cur<<endl; 48 } 49 if (cmd=="FORWARD") 50 { 51 if (forwardstack.empty()) 52 { 53 cout<<"Ignored"<<endl; 54 continue; 55 } 56 backstack.push(cur); 57 cur=forwardstack.top(); 58 forwardstack.pop(); 59 cout<<cur<<endl; 60 } 61 if (cmd=="VISIT") 62 { 63 backstack.push(cur); 64 65 cin>>cur; 66 while (!forwardstack.empty()) forwardstack.pop(); 67 cout<<cur<<endl; 68 69 } 70 } 71 72 73 return 0; 74 }