//穷举 俺苯 俺只会穷举
//反正机子快 不到1秒
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i1, i2, i3, i4, i5, i6, i7, i8, i9: integer;
mydata: array[1..3, 1..3] of integer;
from: set of 1..9;
function check: boolean;
var
i, j: integer;
sum: integer;
begin
result := false;
for i := 1 to 3do
begin
sum := 0;
for j := 1 to 3do
sum := sum + mydata[j];
if sum <> 15 then
exit;
end;
for i := 1 to 3do
begin
sum := 0;
for j := 1 to 3do
sum := sum + mydata[j];
if sum <> 15 then
exit;
end;
sum := 0;
for i := 1 to 3do
sum := sum + mydata;
if sum <> 15 then
exit;
sum := 0;
for i := 1 to 3do
sum := sum + mydata[4 - i];
if sum <> 15 then
exit;
result := true;
end;
function checkFirst: boolean;
var
myCount: array[1..9] of integer;
i, idx: integer;
begin
result := false;
for i := 1 to 9do
myCount := 0;
for i := 1 to 9do
begin
idx := mydata[(i - 1) div 3 + 1][i mod 3 + 1];
myCount[idx] := myCount[idx] + 1;
if myCount[idx] > 1 then
exit;
end;
result := true;
end;
begin
from := [1, 2, 3, 4, 5, 6, 7, 8, 9];
for i1 := 1 to 9do
for i2 := 1 to 9do
if i2 in from - [i1] then
for i3 := 1 to 9do
if i3 in from then
for i4 := 1 to 9do
if i4 in from - [i1] - [i2] - [i3] then
for i5 := 1 to 9do
if i5 in from - [i1] - [i2] - [i3] - [i4] then
for i6 := 1 to 9do
if i6 in from - [i1] - [i2] - [i3] - [i4] - [i5] then
for i7 := 1 to 9do
if i7 in from - [i1] - [i2] - [i3] - [i4] - [i5] - [i6] then
for i8 := 1 to 9do
if i8 in from - [i1] - [i2] - [i3] - [i4] - [i5] - [i6] - [i7] then
for i9 := 1 to 9do
if i9 in from - [i1] - [i2] - [i3] - [i4] - [i5] - [i6] - [i7] - [i8] then
begin
mydata[1, 1] := i1;
mydata[1, 2] := i2;
mydata[1, 3] := i3;
mydata[2, 1] := i4;
mydata[2, 2] := i5;
mydata[2, 3] := i6;
mydata[3, 1] := i7;
mydata[3, 2] := i8;
mydata[3, 3] := i9;
// if checkFirst then
if check then
begin
Memo1.lines.Add('----');
Memo1.Lines.Add(format('%d %d %d', [mydata[1, 1], mydata[1, 2], mydata[1, 3]]));
Memo1.Lines.Add(format('%d %d %d', [mydata[2, 1], mydata[2, 2], mydata[2, 3]]));
Memo1.Lines.Add(format('%d %d %d', [mydata[3, 1], mydata[3, 2], mydata[3, 3]]));
exit;
end;
end;
end;
end.