The 7th Blue Bridge Cup winter work

Keywords: C++

Please indicate the source of Reprint: http://www.cnblogs.com/zhishoumuguinian/p/8365430.html
Winter work

Now the math problems in primary school are not so interesting. Look at this winter vacation assignment. Each box represents a number from 1 to 13, but it can't be repeated.

□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □

For example: and:
6 + 7 = 13          7 + 6 = 13
9 - 8 = 1           9 - 8 = 1
3 * 4 = 12          3 * 4 = 12
10 / 2 = 5          10 / 2 = 5

Two solutions. How many schemes have you found? Please fill in an integer representing the number of scenarios. Note: what you submit should be an integer. Do not fill in any redundant content or explanatory text.

 

There is no comment for this code, but the idea is the same as the previous one. If you want to understand the idea, you can study this code, or see my last essay Fill in the number of squares in the 7th Blue Bridge Cup . Next, stick on the code.

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <math.h>
 4 
 5 using namespace std;
 6 int a[4][4], flag[14]={0};
 7 int ans=0;
 8 
 9 bool Check(int x, int y, int i)
10 {
11     if(y==0)  return true;
12     int tem;
13     switch(x)
14     {
15         case 0:
16             tem=a[x][y]+a[x][y-1];
17             break;
18         case 1:
19             tem=a[x][y]-a[x][y-1];
20             break;
21         case 2:
22             tem=a[x][y]*a[x][y-1];
23             break;
24         case 3:
25             if(a[x][y-1]%a[x][y]!=0)
26             return false;
27             else tem=a[x][y-1]/a[x][y];
28             break;
29     }
30     if(flag[tem]==0&&tem>=1&&tem<=13)
31     {
32         flag[tem]=1;
33         a[x][y+1]=tem;
34         return true;
35     }
36     else return false;
37 }
38 
39 void dfs(int x, int y)
40 {
41     if(x==4)
42     {
43         ans++;
44         /*for(int i=0; i<4; i++)
45         {
46             for(int j=0; j<3; j++)
47             {
48                 cout<<a[i][j]<<"   ";
49             }
50             cout<<endl;
51         }
52         cout<<endl<<endl;*//*The note part is the number filling of printing compound requirements*/
53         return;
54     }
55     for(int i=1; i<=13; i++)
56     {
57         if(!flag[i])
58         {
59             a[x][y]=i;
60             flag[i]=1;
61             bool check=Check(x,y,i);
62             if(check)
63             {
64                 if(y==0)
65                     dfs(x, y+1);
66                 else
67                     dfs(x+1, 0);
68             }
69             if(check)
70             {
71                 flag[i]=0;
72                 flag[a[x][y+1]]=0;
73             }
74             else
75                 flag[i]=0;
76         }
77 
78     }
79 }
80 
81 int main()
82 {
83     dfs(0,0);
84     cout<<ans;
85     return 0;
86 }

answer:

If this article helps you, please give me a A kind of Thank you.......

Posted by Rianna on Sun, 03 May 2020 06:24:45 -0700