gpt4 book ai didi

java - 我应该如何以与放置它们的方式相反的顺序存储我希望访问的对象

转载 作者:行者123 更新时间:2023-11-30 05:51:55 25 4
gpt4 key购买 nike

我在这里遵循本指南:http://www.mazeworks.com/mazegen/mazetut/index.htm

或者更具体地说

create a CellStack (LIFO) to hold a list of cell locations 
set TotalCells = number of cells in grid
choose a cell at random and call it CurrentCell
set VisitedCells = 1

while VisitedCells < TotalCells

find all neighbors of CurrentCell with all walls intact
if one or more found
choose one at random
knock down the wall between it and CurrentCell
push CurrentCell location on the CellStack
make the new cell CurrentCell
add 1 to VisitedCells else
pop the most recent cell entry off the CellStack
make it CurrentCell endIf

endWhile

我是用 java 写的,我的问题是。

我应该如何存储我访问过的单元格,以便我可以按与放置它们时相反的顺序访问它们。

像这样?

List<Location> visitedCells = new ArrayList<Location>();

Then do I grab with visitedCells.get(visitedCells.size()-1)?

位置存储 x、y 和 z。不是我想问你的事情。

最佳答案

你可以使用 Stack为此目的:

Stack<Location> visitedCells = new Stack<Location>();
visitedCells.push(myLocation1);
visitedCells.push(myLocation2);

// Get last one in but DONT remove
Location location2 = visitedCells.peek();

// Get last one in and remove
location2 = visitedCells.pop();

关于java - 我应该如何以与放置它们的方式相反的顺序存储我希望访问的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com