码迷,mamicode.com
首页 > 其他好文 > 详细

DFS小题

时间:2018-07-10 00:27:10      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:round   display   int   div   mil   tps   play   dfs   www   

原创


题目为:()()()+()()()=()()()

将1~9这9个数字填入括号,每个数字只能用一次。

枚举:

技术分享图片
 1 public class Test {
 2     public static void main(String[] args){
 3         int a[]=new int[9];
 4         int flag[]=new int[10];
 5         long total=0L;
 6         for(int i=1;i<=9;i++) {
 7             flag[i]=0;
 8         }
 9         for(a[0]=1;a[0]<=9;a[0]++) {
10             for(a[1]=1;a[1]<=9;a[1]++) {
11                 for(a[2]=1;a[2]<=9;a[2]++) {
12                     for(a[3]=1;a[3]<=9;a[3]++) {
13                         for(a[4]=1;a[4]<=9;a[4]++) {
14                             for(a[5]=1;a[5]<=9;a[5]++) {
15                                 for(a[6]=1;a[6]<=9;a[6]++) {
16                                     for(a[7]=1;a[7]<=9;a[7]++) {
17                                         for(a[8]=1;a[8]<=9;a[8]++) {
18                                             int tt=0;
19                                             for(int i=0;i<=8;i++) {
20                                                 flag[a[i]]=1;
21                                             }
22                                             for(int i=1;i<=9;i++) {
23                                                 tt+=flag[i];
24                                             }
25                                             if(tt==9) {
26                                                 if(a[0]*100+a[1]*10+a[2]+a[3]*100+a[4]*10+a[5]==a[6]*100+a[7]*10+a[8]) {
27                                                     total++;
28                                                     tt=0;
29                                                 }
30                                             }
31                                             for(int i=1;i<=9;i++) {    //数组恢复
32                                                 flag[i]=0;
33                                             }
34                                             
35                                         }
36                                     }
37                                 }
38                             }
39                         }
40                     }
41                 }
42             }
43         }
44         System.out.println(total);
45     }
46 }
View Code

全排列:

关于全排列,请看我上一篇博客:https://www.cnblogs.com/chiweiming/p/9279858.html

技术分享图片
 1 public class Test{
 2     
 3     static int flag[]=new int[10];    //flag[i]=0代表第i个数未用
 4     static int win[]=new int[9];
 5     static long total=0L;
 6     
 7     public static void dfs(int step) {    //第step个格子
 8         if(step==9) {
 9             if(win[0]*100+win[1]*10+win[2]+win[3]*100+win[4]*10+win[5]==win[6]*100+win[7]*10+win[8]) {
10                 total++;
11             }
12             return;
13         }
14         for(int i=1;i<=9;i++) {    //尝试按顺序将1~9其中一个放入格子
15             if(flag[i]==0) {
16                 win[step]=i;
17                 flag[i]=1;
18                 dfs(step+1);
19                 flag[i]=0;
20             }
21         }
22     }
23     
24     public static void main(String args[]) {
25         for(int i=1;i<=9;i++) {
26             flag[i]=0;
27         }
28         dfs(0);
29         System.out.println(total);
30     }
31 }
View Code

答案:336

23:26:20

2018-07-09

DFS小题

标签:round   display   int   div   mil   tps   play   dfs   www   

原文地址:https://www.cnblogs.com/chiweiming/p/9283331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!