16,748
社区成员
发帖
与我相关
我的任务
分享
unit GoogleHunt2u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Spin;
type
TForm1 = class(TForm)
SpinEdit1: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
SpinEdit2: TSpinEdit;
Button1: TButton;
Edit1: TEdit;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
VArr: array [0..100, 0..100] of string;
function BigIntAdd(V1, V2: string): string;
var
i: integer;
max1, max2: integer;
max: integer;
v: integer;
begin
if V1[1] = '0' then
max1 := Length(V1) - 1
else
max1 := Length(V1);
if V2[1] = '0' then
max2 := Length(V2) - 1
else
max2 := Length(V2);
if max1 > max2 then
max := max1 + 1
else
max := max2 + 1;
while Length(V1) < max do
begin
V1 := '0' + V1;
end;
while Length(V2) < max do
begin
V2 := '0' + V2;
end;
SetLength(Result, max);
v := 0;
for i := max downto 1 do
begin
v := Byte(V1[i]) + Byte(V2[i])-Byte('0')*2 + v;
Result[i] := Char((v mod 10) + Byte('0'));
v := v div 10;
end;
end;
function CountRoute(W, H: integer): string;
begin
if (W = 1) or (H = 1) then
Result := '1'
else
begin
if VArr[W-1][H] = '' then
begin
VArr[W-1][H] := CountRoute(W-1, H);
VArr[H][W-1] := VArr[W-1][H];
end;
if VArr[W][H-1] = '' then
begin
VArr[W][H-1] := CountRoute(W, H-1);
VArr[H-1][W] := VArr[W][H-1];
end;
Result := BigIntAdd(VArr[W-1][H], VArr[W][H-1]);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
W, H: integer;
begin
W := SpinEdit1.Value;
H := SpinEdit2.Value;
Edit1.Text := CountRoute(W, H);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FillChar(VArr, sizeof(VArr), 0);
end;
end.