poj 2718 Smallest Difference(穷竭搜索dfs)

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0. 

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

1
0 1 2 4 6 7

Sample Output

28

Source

给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小。

思路:

有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生的差值比当前所搜索到的结果还要大,那么就直接返回。这个剪枝就是超时与几十MS的差距

注意一点就是可能有0 与一个数字存在的情况,比如0 3,0 5等等。

其他的就比较简单了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<set>
 5 #include<map>
 6 #include<algorithm>
 7 #include<queue>
 8 using namespace std;
 9 #define inf 1<<29
10 #define N 16
11 int a[N];
12 int vis_a[N],vis_b[N];
13 int ans;
14 int Anum,Bnum;
15 int w;
16 int n;
17 int nums[16]={1,10,100,1000,10000,100000};
18 void dfs_B(int num,int vals,int sum){
19 
20     if( num>0 && sum*nums[Bnum-num]-vals>=ans ) return;
21 
22     if(num==Bnum){
23         ans=min(ans,abs(sum-vals));
24         return;
25     }
26 
27     for(int i=0;i<n;i++){
28         if(!vis_a[i] && !vis_b[i]){
29             if(num==0 && a[i]==0)
30                continue;
31             vis_b[i]=1;
32             dfs_B(num+1,vals,sum*10+a[i]);
33             vis_b[i]=0;
34         }
35     }
36 
37 }
38 void dfs_A(int num,int vals){
39 
40    if(num==Anum){
41         w=vals;
42         memset(vis_b,0,sizeof(vis_b));
43       dfs_B(0,vals,0);
44       return;
45    }
46 
47    for(int i=0;i<n;i++){
48       if(!vis_a[i]){
49          if(num==0 && a[i]==0)
50             continue;
51          vis_a[i]=1;
52          dfs_A(num+1,vals*10+a[i]);
53          vis_a[i]=0;
54       }
55    }
56 }
57 int main()
58 {
59     int t;
60 
61     while(scanf("%d",&t)!=EOF)
62     {
63 
64          getchar();
65         while(t--){
66             n=0;
67             char ch;
68             while((ch=getchar())!='
'){
69                 if(ch>='0' && ch<='9')
70                     a[n++]=ch-'0';
71             }
72 
73 
74 
75             //for(int i=0;i<n;i++)
76                 //printf("---%d
",a[i]);
77             ans=inf;
78 
79             Anum=n>>1;
80             Bnum=n-Anum;
81 
82             dfs_A(0,0);
83 
84             if(ans!=inf)
85                 printf("%d
",ans);
86             else
87                 printf("%d
",w);
88             }
89 
90 
91     }
92     return 0;
93 }
View Code
 
原文地址:https://www.cnblogs.com/UniqueColor/p/4764856.html