program Chess;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
MaxSize = 100;
InputFileName = 'Input.txt';
Move: array [1..4, 1..2] of Integer = ((0, 1), (1, 0), (0, -1), (-1, 0));
type
TNode = record
x, y: Integer;
d: Integer
end;
var
Maze: array [1..MaxSize, 1..MaxSize] of Boolean;
Dx, Dy: Integer;
Count: Integer;
Stack: array [1..MaxSize*MaxSize] of TNode;
Top: Integer;
NewNode: TNode;
function Init: Boolean;
var
InputFile: Text;
s: string;
x, y: Integer;
begin
Count:=0;
Top:=1;
x:=0;
Assign(InputFile, InputFileName);
Reset(InputFile);
while not Eof(InputFile)do
begin
ReadLn(InputFile, s);
Inc(x);
if (Length(s)>MaxSize) or (x>MaxSize) then
begin
WriteLn('Maze size error!');
Result:=False;
Exit
end;
WriteLn(s);
for y:=1 to Length(s)do
if UpCase(s[y])='X' then
Maze[x, y]:=False
else
begin
Maze[x, y]:=True;
if UpCase(s[y])='S' then
begin
Stack[1].x:=x;
Stack[1].y:=y
end
else
if UpCase(s[y])='D' then
begin
Dx:=x;
Dy:=y
end
end
end;
Stack[1].d:=0;
CloseFile(InputFile);
Result:=True
end;
procedure Print;
var
i: Integer;
begin
Inc(Count);
Write(Count: 3, ': ');
for i:=1 to Topdo
case Stack.d of
1: Write('e');
2: Write('s');
3: Write('w');
4: Write('n')
end;
WriteLn
end;
functiondo
uble: Boolean;
var
i: Integer;
begin
Result:=False;
for i:=1 to Topdo
if (Stack.x=NewNode.x) and (Stack.y=NewNode.y) then
begin
Result:=True;
Exit
end
end;
begin
if Init then
begin
while Top>0do
begin
while Stack[Top].d<4do
begin
Inc(Stack[Top].d);
NewNode.x:=Stack[Top].x+Move[Stack[Top].d, 1];
NewNode.y:=Stack[Top].y+Move[Stack[Top].d, 2];
if (NewNode.x>0) and (NewNode.x<=MaxSize) and (NewNode.y>0) and (NewNode.y<=MaxSize)
and Maze[NewNode.x, NewNode.y] and (notdo
uble) then
begin
Inc(Top);
Stack[Top]:=NewNode;
Stack[Top].d:=0;
if (NewNode.x=Dx) and (NewNode.y=Dy) then
begin
Print;
Dec(Top)
end
end
end;
Dec(Top)
end;
if Count=0 then
WriteLn('Not found!')
end;
ReadLn
end.
全部路径,
但我指定了在一条路径中,同一点不能经过两次.否则有无数条.