- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用这篇文章 Link 实现 A* 路径查找算法(现在是 Dijkstra 算法,即没有启发式算法) .但是我无法弄清楚我的代码有什么问题(它找到了不正确的路径)。
代替空的begin ... end;应该是这一步:
If it is on the open list already, check to see if this path to thatsquare is better, using G cost as the measure. A lower G cost meansthat this is a better path. If so, change the parent of the square tothe current square, and recalculate the G and F scores of the square.
但我认为这并不重要,因为没有对角线移动。
uses
crt;
const
MAXX = 20;
MAXY = 25;
type
TArr = array [0..MAXY, 0..MAXX] of integer;
TCell = record
x: integer;
y: integer;
end;
TListCell = record
x: integer;
y: integer;
G: integer;
parent: TCell;
end;
TListArr = array [1..10000] of TListCell;
TList = record
arr: TListArr;
len: integer;
end;
var
i, j, minind, ind, c: integer;
start, finish: TCell;
current: TListCell;
field: TArr;
opened, closed: TList;
procedure ShowField;
var
i, j: integer;
begin
textcolor(15);
for i := 0 to MAXX do
begin
for j := 0 to MAXY do
begin
case field[j, i] of
99: textcolor(8); // not walkable
71: textcolor(14); // walkable
11: textcolor(10); // start
21: textcolor(12); // finish
15: textcolor(2); // path
14: textcolor(5);
16: textcolor(6);
end;
write(field[j, i], ' ');
end;
writeln;
end;
textcolor(15);
end;
procedure AddClosed(a: TListCell);
begin
closed.arr[closed.len + 1] := a;
inc(closed.len);
end;
procedure AddOpened(x, y, G: integer);
begin
opened.arr[opened.len + 1].x := x;
opened.arr[opened.len + 1].y := y;
opened.arr[opened.len + 1].G := G;
inc(opened.len);
end;
procedure DelOpened(n: integer);
var
i: integer;
begin
AddClosed(opened.arr[n]);
for i := n to opened.len - 1 do
opened.arr[i] := opened.arr[i + 1];
dec(opened.len);
end;
procedure SetParent(var a: TListCell; parx, pary: integer);
begin
a.parent.x := parx;
a.parent.y := pary;
end;
function GetMin(var a: TList): integer;
var
i, min, mini: integer;
begin
min := MaxInt;
mini := 0;
for i := 1 to a.len do
if a.arr[i].G < min then
begin
min := a.arr[i].G;
mini := i;
end;
GetMin := mini;
end;
function FindCell(a: TList; x, y: integer): integer;
var
i: integer;
begin
FindCell := 0;
for i := 1 to a.len do
if (a.arr[i].x = x) and (a.arr[i].y = y) then
begin
FindCell := i;
break;
end;
end;
procedure ProcessNeighbourCell(x, y: integer);
begin
if (field[current.x + x, current.y + y] <> 99) then // if walkable
if (FindCell(closed, current.x + x, current.y + y) <= 0) then // and not visited before
if (FindCell(opened, current.x + x, current.y + y) <= 0) then // and not added to list already
begin
AddOpened(current.x + x, current.y + y, current.G + 10);
SetParent(opened.arr[opened.len], current.x, current.y);
// field[opened.arr[opened.len].x, opened.arr[opened.len].y]:=16;
end
else
begin
end;
end;
begin
randomize;
for i := 0 to MAXX do
for j := 0 to MAXY do
field[j, i] := 99;
for i := 1 to MAXX - 1 do
for j := 1 to MAXY - 1 do
if random(5) mod 5 = 0 then
field[j, i] := 99
else field[j, i] := 71;
// start and finish positions coordinates
start.x := 5;
start.y := 3;
finish.x := 19;
finish.y := 16;
field[start.x, start.y] := 11;
field[finish.x, finish.y] := 21;
ShowField;
writeln;
opened.len := 0;
closed.len := 0;
AddOpened(start.x, start.y, 0);
SetParent(opened.arr[opened.len], -1, -1);
current.x := start.x;
current.y := start.y;
repeat
minind := GetMin(opened);
current.x := opened.arr[minind].x;
current.y := opened.arr[minind].y;
current.G := opened.arr[minind].G;
DelOpened(minind);
ProcessNeighbourCell(1, 0); // look at the cell to the right
ProcessNeighbourCell(-1, 0); // look at the cell to the left
ProcessNeighbourCell(0, 1); // look at the cell above
ProcessNeighbourCell(0, -1); // look at the cell below
if (FindCell(opened, finish.x, finish.y) > 0) then
break;
until opened.len = 0;
// count and mark path
c := 0;
while ((current.x <> start.x) or (current.y <> start.y)) do
begin
field[current.x, current.y] := 15;
ind := FindCell(closed, current.x, current.y);
current.x := closed.arr[ind].parent.x;
current.y := closed.arr[ind].parent.y;
inc(c);
end;
ShowField;
writeln(c);
readln;
end.
编辑 2012 年 2 月 1 日:更新了代码,还修复了路径标记(应该有 or 而不是 and),看起来现在可以工作了:)
最佳答案
您应该重写程序以使用循环而不是剪切和粘贴来访问每个邻居。如果这样做,您将避免如下错误:
if (field[current.x, current.y - 1] <> 99) then
if (FindCell(closed, current.x, current.y - 1) <= 0) then
if (FindCell(opened, current.x + 1, current.y) <= 0) then
(见最后一行不一致的current.x + 1, current.y
。)
关于循环,我在想这样的事情(伪Python):
neighbor_offsets = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for offset in neighbor_offsets:
neighbor = current + offset
if is_walkable(neighbor) and not is_visited(neighbor):
# Open 'neighbor' with 'current' as parent:
open(neighbor, current)
# Perhaps check if the goal is reached:
if neighbor == finish:
goal_reached = True
break
如果你不写循环而只是重构为
ProcessCell(x+1, y);
ProcessCell(x-1, y);
ProcessCell(x, y-1);
ProcessCell(x, y-1);
那么这也是一个很大的改进。
关于algorithm - A*/Dijkstra算法简单实现(Pascal),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079969/
说我有两个文件,characters.pas和ogre.pas。食人魔是一个角色,但为了干净起见,我试图将两个文件分开。在characters.pas中,我有 unit Characters; {$m
我想要一个编程代码来打印范围之间的奇数来教学生。这里我取的范围是 1 到 10。所以我想打印 1 到 10 之间的奇数。 我写了这个代码来打印 1 到 10 之间的奇数 program printOd
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
我创建了一个函数,其中包含 2 个具有相同数据类型的参数,对此我没有问题。 但是我在处理不同的数据类型时遇到了问题 这是我的代码: uses crt; function inputscore(name
我只是想知道如何在 pascal 中进行类型检查?我已经搜索了几个小时,但我找不到任何有用的东西。 例子: var number: Integer; begin write('Enter a nu
我正在实现 an assemblinker for the 16-bit DCPU来自游戏 0x10c。 有人向我建议的一种技术是使用“覆盖,就像过去在 Turbo Pascal 中一样”,以便在运行
Q1:这是什么意思:WriteLn (#$0b) ? $0b应该是十六进制,如 0x0b ,但是 # 呢?标志? 问题 2: x:=readkey; if ( x = #5) do... 是否#5意思
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
对于在 Pascal 中返回值的函数,赋值 FunctionName := SomeVal;用来。我认为它不会像 return 那样在那个确切的地方停止函数执行在 C 中。有没有类似C的东西retur
我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程
我正在开展我的学校项目,我想使用动态(而不是静态)数组。我使用 ObjectPascal,所以我习惯了一些语法。但现在我在旧的 TurboPascal 中编程(我使用的是 Windows 版 Turb
对于这项任务,我有一个不太好的解决方案,但有人可以告诉我如何使用标准库或更优雅的方法。当前的解决方案基于这样的事实:对对象的过程的引用在内存中存储两个指针,一个指向过程代码的指针和一个指向对象的指针。
我正在运行一个 Fenuc Karel 机器人进行类作业,它使用 Pascal 的变体,但我们的机器人是在 1991-1993 年添加 random() 之前的。有谁知道如何在 Pascal 的旧 d
我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程
我认为展示问题的最简单方法是举个例子。编码: PROGRAM CONSTANTSTRING(OUTPUT); CONST C_MaxLength = 30; VAR small_str
我在 Windows 上运行 Lazarus。我真的很想制作“Beep”程序。看来你可以在 Pascal 中使用: windows.beep(300,500); 但不是在拉撒路!我可以使用另一个命令吗
我正在对一个 18 岁的 pascal 程序进行维护。为了帮助我理解一切是如何组合在一起的,我想绘制一个调用图。但是我找不到任何可以为 pascal 源绘制调用图的软件。我目前正在使用 Turbo P
我正在使用帕斯卡。我在处理读取文件时遇到问题。 我有一个带有整数的文件。我读取文件的 pascal 是: read(input, arr[i]); 如果我的文件内容是 1 2 3那么它很好,但如果它是
当分配运算符 := 时会发生什么?在 Object Pascal 中重载?我主要是指首先评估什么,更重要的是如何(如果可能)更改此顺序。这是一个让我烦恼的例子: 我声明TMyClass因此: TMyC
如何使用 writeln 在 Pascal 中打印撇号符号功能? 例子: writeln('My brother's book'); 无法工作,因为 s book没有“写”函数,所以编译器返回一个错误
我是一名优秀的程序员,十分优秀!