关于一个求最短路径(不带权)的算法,初学算法,请多指教~!(200分)

  • 主题发起人 iknowabc
  • 开始时间
b[i,j] i,j不是x,y坐标吗?哪里弄错了?
 
要注意读文件时坐标的变化顺序,这个并不是按照我们平常所用的坐标轴来的
 
谢谢AI_Player,你的程序我大概懂了。
to LeeChange:
看了看你的程序,BFS大概是看懂了,但是关于移动方向还是不懂
Move: array [1..4, 1..2] of Integer = ((0, 1), (1, 0), (0, -1), (-1, 0));
...
case List[Index].d of
1: Write('e');
2: Write('s');
3: Write('w');
4: Write('n')
end
...
for d:=1 to 4do
begin
NewNode.x:=List[Close].x+Move[d, 1];
NewNode.y:=List[Close].y+Move[d, 2];
...
这里d是代表方向,1是e就是东,向右,也就是x坐标应该加一,但是为什么Move[1]是y坐标加一?
2是s就是南,向下,也就是应该y坐标减一,为什么Move[2]是x坐标加一?
呵呵,希望你能解释一下。
 
这里d是代表方向,1是e就是东,向右,也就是x坐标应该加一,也就是矩阵中列加一,不就是加y吗.
2是s就是南,向下,也就是应该y坐标减一,也就是矩阵的行加一,不就是加x吗.
 
谢谢Leechange的指教!
能否多提一个问题,就是怎么将你的程序改动一下,能否使用类似回溯的方式列出所有
能达到目标点的路径??不求最优,但求最全。
再次感谢LeeChange,这里还要多问一下,请指点!谢谢!
 
那岂不是数量太多了?
不信你自己算算大概有多少条
 
呵呵,如果地图满足要求,条数应该不多,而且是计算机算,又不是人工计算。
(如果地图通路不成环,不相交,比如就只有独立的三条,应该可以都找到吧)

还要请教各位老师!
 
如果地图保证通路不成环,不相交,那么程序应该怎么写?
如果对地图无要求,应该怎么考虑!
继续请教!
 
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.
全部路径,
但我指定了在一条路径中,同一点不能经过两次.否则有无数条.
 
多人接受答案了。
 
感谢AI_player与LeeChange的回答。
特别是LeeChange,感谢!
 

Similar threads

D
回复
0
查看
734
DelphiTeacher的专栏
D
D
回复
0
查看
705
DelphiTeacher的专栏
D
D
回复
0
查看
677
DelphiTeacher的专栏
D
顶部