My code is written on PascalABC.NET.
(我的代码写在PascalABC.NET上。)
Function (RandomRoute) is working, but when I try to run it several times in cycle it doesn't work in a proper way. (函数(RandomRoute)正在运行,但是当我尝试以周期方式运行几次时,它无法以适当的方式运行。)
Every iteration it has to generate a new sequence. (每次迭代都必须生成一个新序列。)
.
(。)
On this picture you can see what I mean.
(在这张照片上,您可以看到我的意思。)
I don't understand where the problem is. (我不明白问题出在哪里。)
PS sorry for my english (PS对不起我的英语)
function RandomRoute (n : integer; a : TMatrix) : List<integer>;
var
k,i,j,iRand, pathLength: integer;
flag : boolean;
tempList : List<integer> := new List<integer>;
Begin
randomize;
for k := 0 to (n-1) do
begin
repeat
iRand := random(0, (n-1));
if (tempList.Contains(iRand)) then
begin
flag := false;
end
else
begin
flag := true;
tempList.Add(iRand);
end;
until (flag);
end;
tempList.Add(tempList[0]);
pathLength := 0;
for var z : integer := 0 to (n-1) do
begin
i := tempList[z]; j := tempList[z+1];
pathLength := pathLength + a[i,j];
end;
tempList.Add(pathLength);
Result := tempList;
End;
MAIN FUNCTION
(主功能)
BEGIN
var randRoute : List<integer> := new List<integer>;
var minRoute : List<integer> := new List<integer>;
routeLength := 0;
writeln ('City Count:');
readln (n);
readln (firstcity);
MatFullRandom(n, mass);
massCopy := mass;
DispMat (n, mass);
GreedyAlg(firstcity, n, mass);
Writeln ('RANDOM ROUTES:');
randRoute := RandomRoute(n, massCopy);
minRoute := randRoute;
for var z : integer := 0 to 10 do
begin
randRoute := RandomRoute(n, massCopy);
if (randRoute.Last < minRoute.Last) then minRoute := randRoute;
writeln('Random Route ', randRoute);
end;
writeln('The best from randomRoute: ',minRoute);
MatrixRandomRoute(n, minRoute, massCopy, massRed);
DispMat(n, MassRed);
DrawGraph(n, mass);
END.
ask by Lika Barken translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…