hdu 1979 剪枝暴搜

Fill the blanks

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 283    Accepted Submission(s): 115


Problem Description
There is a matrix of 4*4, you should fill it with digits 0 – 9, and you should follow the rules in the following picture:

 

 

Input
No input.
 

 

Output
Print all the matrixs that fits the rules in the picture. 
And there is a blank line between the every two matrixs.
 

 

Sample Output
1193
1009
9221
3191
 
1193
1021
9029
3911
……
9173
1559
3821
3391
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 bool flag[10000],mark;
 7 int map[5][5];
 8 
 9 void get_primes()
10 {
11     memset(flag,1,sizeof(flag));
12     int i,j;flag[0]=flag[1]=0;
13     for(i=2;i<10000;i++)
14     {
15         for(j=i+i;j<10000;j+=i)
16             flag[j]=0;
17     }
18 }
19 bool cjudge(int x)
20 {
21     int i,a=0,b=0;
22     for(i=0;i<4;i++) a=a*10+map[x][i];
23     for(i=3;i>=0;i--) b=b*10+map[x][i];
24     if(flag[a]&&flag[b]) return true;
25     return false;
26 }
27 bool rjudge(int x)
28 {
29     int i,a=0,b=0;
30     for(i=0;i<4;i++) a=a*10+map[i][x];
31     for(i=3;i>=0;i--) b=b*10+map[i][x];
32     if(flag[a]&&flag[b]) return true;
33     return false;
34 }
35 bool ljudge()
36 {
37     int i,a=0,b=0;
38     for(i=0;i<=3;i++) a=a*10+map[i][3-i];
39     for(i=3;i>=0;i--) b=b*10+map[i][3-i];
40     if(flag[a]&&flag[b]) return true;
41     return false;
42 }
43 bool rjudge()
44 {
45     int i,a=0,b=0;
46     for(i=0;i<=3;i++) a=a*10+map[i][i];
47     for(i=3;i>=0;i--) b=b*10+map[i][i];
48     if(flag[a]&&flag[b]) return true;
49     return false;
50 }
51 void Printf()
52 {
53     for(int i=0;i<4;i++)
54     {
55         for(int j=0;j<4;j++)
56             printf("%d",map[i][j]);
57         printf("\n");
58     }
59 }
60 
61 void dfs(int i,int j)
62 {
63     if(i==4)
64     {
65         if(mark++) puts("");
66         Printf();
67         return ;
68     }
69     for(int k=0;k<10;k++)
70     {
71         if(k%2==0 && (i==0||i==3||j==0||j==3)) continue;
72         map[i][j]=k;
73         if(j==3 && !cjudge(i)) continue;
74         if(i==3 && !rjudge(j)) continue;
75         if(i==3 && j==0 && !ljudge()) continue;
76         if(i==3 && j==3 && !rjudge()) continue;
77         dfs(i+(j+1)/4,(j+1)%4);
78     }
79 }
80 
81 int main()
82 {
83     get_primes();
84     mark=0;
85     dfs(0,0);
86     return 0;
87 }

 

posted on 2014-08-15 15:08  雄..  阅读(259)  评论(0)    收藏  举报

导航