This is a typical C language algorithm. The question is to give a given graph. Based on the logo in this graph, we can find the total number of squares that this graph can contain.
For example, the following figure:
The following is a solution: First, we use a combination algorithm to figure out how many of these vertices can constitute a quadrilateral composed of four vertices and list each quadrilateral, then we use a subfunction to judge the Quadrilateral. If a square is used, we can add one to obtain the total number of squares.
Code:
# Include <stdio. h>
# Include <math. h>
/* Enter the correct number of vertices for the input graph. modify this value to obtain
Number of squares */
# Define N 13
# Define B (N * (N-1) * (N-2) * (N-3)/(4*3*2 ))
Typedef struct {
Int x;
Int y;
} Point;
Typedef struct {
Point a [4];
} Squre;
Point dian [N];
Point queue [4];/* store rectangular coordinates */
Squre tmp;
Int k = 0;
Int top = 0;
Void comb (int s, int n, int m );
Int function (Squre s );
Int main (void ){
Int I = 0;
Int num = (int) B;
Printf ("% d", num );
For (I = 0; I <N; I ++ ){
Printf ("\ nplease input the % d zuo biao:", I + 1 );
Scanf ("% d", & dian [I]. x, & dian [I]. y );
}
Comb (0, N, 4 );
Printf ("the sum of sibianxing are % d \ n", k );
Getch ();
Return 0;
}
/* Determine if it is a square. If it returns 1, otherwise 0 */
Int function (Squre s ){
Int e, B, c, d, k;
E = pow (s. a [0]. x-s.a [1]. x), 2) + pow (s. a [0]. y-s.a [1]. y), 2 );
B = pow (s. a [0]. x-s.a [2]. x), 2) + pow (s. a [0]. y-s.a [2]. y), 2 );
If (e> B) {/* e exists as the diagonal line */
C = pow (s. a [2]. x-s.a [1]. x), 2) + pow (s. a [1]. y-s.a [2]. y), 2 );
D = pow (s. a [3]. x-s.a [1]. x), 2) + pow (s. a [3]. y-s.a [1]. y), 2 );
K = pow (s. a [0]. x-s.a [3]. x), 2) + pow (s. a [0]. y-s.a [3]. y), 2 );
If (B = c) & (c = d) & (d = k) & (k = B) & (e = (B + c )))
Return 1;
} Else if (e = B ){
C = pow (s. a [1]. x-s.a [3]. x), 2) + pow (s. a [1]. y-s.a [3]. y), 2 );
D = pow (s. a [2]. x-s.a [3]. x), 2) + pow (s. a [2]. y-s.a [3]. y), 2 );
K = pow (s. a [1]. x-s.a [2]. x), 2) + pow (s. a [1]. y-s.a [2]. y), 2 );
If (e = c) & (c = d) & (e + B) = k ))
Return 1;
} Else {/* B exists as the diagonal line */
C = pow (s. a [2]. x-s.a [1]. x), 2) + pow (s. a [1]. y-s.a [2]. y), 2 );
D = pow (s. a [3]. x-s.a [2]. x), 2) + pow (s. a [3]. y-s.a [2]. y), 2 );
K = pow (s. a [0]. x-s.a [3]. x), 2) + pow (s. a [0]. y-s.a [3]. y), 2 );
If (e = c) & (c = d) & (d = k) & (k = e) & (B = (e + c )))
Return 1;
}
Return 0;
}
/* Combination algorithm: used to obtain a rectangle set that may constitute a square
M indicates that the selected number is the combination number C (m, n), and m points are selected from n.
And returns the number of squares */
Void comb (int s, int n, int m)
{
Int I, j = 0;
If (s> n) return;
If (top = m ){
For (I = 0; I <m; I ++ ){
Tmp. a [I] = queue [I];
}
J = function (tmp );
If (j = 1 ){
K ++;
}
Return;
}
Queue [top ++] = dian [s];
Comb (s + 1, n, m );
Top --;
Comb (s + 1, n, m );
}