Luo shu: The ancient name of the turtle book, it is said that there are turtles out of Luo Shui, the turtle shell has this image, the structure is wearing nine to one, left three to seven, two four shoulder, six to eight for the foot, in the center of five, the five sides of the white circle are all positive, and the four corners of the black spots are overcast.
Design algorithms to search for this image
- 1-9 numbers cannot be repeated
- Sum of horizontal numbers: 15
- Sum of vertical numbers: 15
- The sum of oblique numbers is 15.
The Code is as follows:
// Use F # To derive loose // see more infomation on http://www.1wuyou.com for more help. let example = [| [| 0; 0; 0 |]; [| 0; 0; 0 |]; [| 0; 0 |]; |] Let size2 = 9; let size = 3let rec invalidnine (M: int array) (Index: INT) Shu = Match index with | 9-> false | index when M. [index/3]. [index % 3] = Shu-> true | _-> invalidnine M (index + 1) shulet rec invalidh (M: int array) S x Y (Shu: INT) (Num: INT) (sum: INT) (DIR: INT) = If num = 3 then if sum = 15 then some (false) else some (true) else match s, x, Y with | some (true), X, Y-> some (true) | none, X, Y-> none | S, x, y when M. [X]. [y] = 0 & num> 0-> none | _-> let addnum = If num = 0 then Shu else M. [X]. [y] match dir with | 1-> invalidh m s (x + 1) % 3) y Shu (Num + 1) (sum + addnum) dir // horizontal | 2-> invalidh m s x (Y + 1) % 3) Shu (Num + 1) (sum + addnum) dir // vertical | 3-> If (x + y) % 2 = 1 then none else invalidh m s (x + 1) % 3) (Y + 1) % 3) Shu (Num + 1) (sum + addnum) dir // bottom right | 4-> If (x + y) % 2 = 1 then none else invalidh m s (x + 1) % 3) (Y-1 + 3) % 3) Shu (Num + 1) (sum + addnum) dir // Let tinvalid m x y Shu = invalidnine M 0 Shu | invalidh M (some (false) x y Shu 0 0 1 = some (true) | invalidh M (some (false) x y Shu 0 0 2 = some (true) | invalidh M (some (false )) x y Shu 0 0 3 = some (true) | invalidh M (some (false) x y Shu 0 0 4 = some (true) let rec search m x y f accu = match X, Y with | x, y when x = 3-> Search M 0 (Y + 1) f ACCU | 0, Y when y = 3-> printfn "find one. "// example |> array. ITER (fun t-> printfn "% A" t) f ACCU | X, Y-> let aux ACCU n = If tinvalid m x y n then ACCU else (M. [X]. [y] <-N; let ACCU = search m (x + 1) y f accu in M. [X]. [y] <-0; ACCU) in list. fold aux ACCU [1 .. 9] exception exit [<entrypoint>] Let main argv = Try printfn "began to discover loose" Search example (0 + 0) 0 (fun S-> If example. [0]. [0] = 4 & example. [0]. [2] = 2 then raise exit) () with | exit-> printfn "Easy to find true loose" example |> array. ITER (fun t-> printfn "% A" t) 0
View code
Output result
He began to find a book. find. find. easy to find the true loose [| 4; 9; 2 |] [| 3; 5; 7 |] [| 8; 1; 6 |] Press any key to continue...
Result Analysis:
As a matter of fact, we have got 8 books. These books are like a transparent square poker with eight forms, but there is only one real book, we directly output the final matching judgment,
Why does the only book have only one copy? This is indeed a question worth thinking about all the time,
| 4 Gong |
9 Li Gong |
2 Kun Gong |
| 3 earthquake Palace |
5 Palace |
7 ONG |
| 8 Gong |
1 canomiya |
6 Palace |
This table is relatively closely related to the acquired gossip sequence. The sequence and orientation of the table are as follows: the location of the Chinese Earth is south, hot, cold, West, high, and low, the time-space sequence, such as the four seasons of the sun, is the same.