unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TIntArr = array of Integer;
TForm1 = class(TForm)
Image1: TImage;
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function Cal: TIntArr;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
Move: array [1..8, 1..2] of Integer = ((-1, 0), (-1, 1), (0, 1), (1, 1),
(1, 0), (1, -1), (0, -1), (-1, -1));
function TForm1.Cal: TIntArr;
type
TNode = record
x, y: Integer
end;
var
List: array [1..801*601] of TNode;
Close, Open: Integer;
Mark: array [0..800, 0..600] of Boolean;
i, j, d: Integer;
x, y: Integer;
Sum: Integer;
function InBorder(x, y: Integer): Boolean;
var
i: Integer;
xx, yy: Integer;
begin
Result:=False;
for i:=1 to 8 do
begin
xx:=x+Move[i, 1];
yy:=y+Move[i, 2];
if (xx>=0) and (xx<=Image1.Width) and (yy>=0) and (yy<=Image1.Height) then
if Image1.Canvas.Pixels[xx, yy]<>clWhite then
begin
Result:=True;
Exit
end
end
end;
begin
SetLength(Result, 0);
FillChar(Mark, SizeOf(Mark), 0);
for i:=0 to Image1.Width do
for j:=0 to Image1.Height do
if not Mark[i, j] then
begin
SetLength(Result, Length(Result)+1);
Sum:=0;
Close:=0;
Open:=1;
List[1].x:=i;
List[1].y:=j;
repeat
Inc(Close);
for d:=1 to 8 do
begin
x:=List[Close].x+Move[d, 1];
y:=List[Close].y+Move[d, 2];
if (x>=0) and (x<=Image1.Width) and (y>=0) and (y<=Image1.Height) then
if Image1.Canvas.Pixels[x, y]=clWhite then
if not Mark[x, y] then
begin
Inc(Open);
List[Open].x:=x;
List[Open].y:=y;
if InBorder(x, y) then
Inc(Sum)
end
end
until Close=Open;
Result[Length(Result)-1]:=Sum
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
a: TIntArr;
begin
a:=Cal;
for i:=Low(a) to High(a) do
ListBox1.Items.Append(Format('%d: %d', [i+1, a]))
end;
end.