n_ss = [ ... rc + 1, cc + 1, S2 ; ... rc + 1, cc + 0, 1 ; ... rc + 1, cc - 1, S2 ; ... rc + 0, cc - 1, 1 ; ... rc - 1, cc - 1, S2 ; ... rc - 1, cc - 0, 1 ; ... rc - 1, cc + 1, S2 ; ... rc - 0, cc + 1, 1 ; ... ];
时间: 2024-02-15 14:16:44 浏览: 31
这段代码是一个八方向移动的迷宫搜索算法的一部分,其中 n_ss 是一个包含八个元素的数组。数组中每个元素代表一种移动方式,其中第一个元素表示向右下方移动,第二个元素表示向下方移动,第三个元素表示向左下方移动,第四个元素表示向左方移动,第五个元素表示向左上方移动,第六个元素表示向上方移动,第七个元素表示向右上方移动,第八个元素表示向右方移动。
数组中每个元素由三个值组成,分别是移动后的行号、列号以及移动的代价。在这段代码中,代价分别为 1 和 S2,其中 1 表示在水平或垂直方向上的移动代价为 1,S2 表示在对角线方向上的移动代价为 S2。这种移动方式通常被用于路径规划中,其中 S2 可以被设置为 1.4 或 sqrt(2),表示在对角线方向上的移动距离为水平或垂直方向上的移动距离的 sqrt(2) 倍。
相关问题
while any(openSet(:) > 0) % Find the minimum fScore within the open set [~, current] = min(fScore(:)); % If we've reached the goal if current == goal % Get the full path and return it final = get_path(cameFrom, current); return end % Linear index -> row, col subscripts rc = rem(current - 1, mapSize(1)) + 1; cc = (current - rc) / mapSize(1) + 1; % Remove CURRENT from openSet openSet(rc, cc) = false; % Place CURRENT in closedSet closedSet(rc, cc) = true; fScore(rc, cc) = inf; gScoreCurrent = gScore(rc, cc) + costs(rc, cc); % Get all neighbors of CURRENT. Neighbors are adjacent indices on % the map, including diagonals. % Col 1 = Row, Col 2 = Col, Col 3 = Distance to the neighbor n_ss = [ ... rc + 1, cc + 1, S2 ; ... rc + 1, cc + 0, 1 ; ... rc + 1, cc - 1, S2 ; ... rc + 0, cc - 1, 1 ; ... rc - 1, cc - 1, S2 ; ... rc - 1, cc - 0, 1 ; ... rc - 1, cc + 1, S2 ; ... rc - 0, cc + 1, 1 ; ... ]; % keep valid indices only valid_row = n_ss(:,1) >= 1 & n_ss(:,1) <= mapSize(1); valid_col = n_ss(:,2) >= 1 & n_ss(:,2) <= mapSize(2); n_ss = n_ss(valid_row & valid_col, :); % subscripts -> linear indices neighbors = n_ss(:,1) + (n_ss(:,2) - 1) .* mapSize(1); % only keep neighbors in the map and not in the closed set ixInMap = map(neighbors) & ~closedSet(neighbors); neighbors = neighbors(ixInMap); % distance to each kept neighbor dists = n_ss(ixInMap, 3); % Add each neighbor to the open set openSet(neighbors) = true; % TENTATIVE_GSCORE is the score from START to NEIGHBOR. tentative_gscores = gScoreCurrent + costs(neighbors) .* dists; % IXBETTER indicates where a better path was found ixBetter = tentative_gscores < gScore(neighbors); bestNeighbors = neighbors(ixBetter); % For the better paths, update scores cameFrom(bestNeighbors) = current; gScore(bestNeighbors) = tentative_gscores(ixBetter); fScore(bestNeighbors) = gScore(bestNeighbors) + compute_cost(mapSize, bestNeighbors, gr, gc); end % while end
这段代码是 A* 算法的核心实现部分,用于寻找两点之间的最短路径。具体来说,它依次对起点周围的格子进行探索,计算每个格子到起点的代价(gScore)以及到终点的估计代价(fScore),并将其加入 openSet 中。然后从 openSet 中选取 fScore 最小的格子进行探索,直到找到终点或 openSet 中没有格子可供探索为止。
在探索每个格子时,首先将其从 openSet 中移除并加入 closedSet 中,然后计算该格子与周围格子的代价,并将未被探索过的格子加入 openSet 中。如果发现了新的更优路径,就更新该格子到起点的代价和 fScore,并将其加入到 cameFrom 列表中,表示它是从哪个格子转移而来的。
最终,如果找到了终点,就从 cameFrom 列表中回溯路径。
class Hour: def __init__(self): self.__hour = 0 def set_hour(self, hour): if hour < 0 or hour >= 24: return False self.__hour = hour return True def get_hour(self): return self.__hour class Minute: def __init__(self): self.__minute = 0 def set_minute(self, minute): if minute < 0 or minute >= 60: return False self.__minute = minute return True def get_minute(self): return self.__minute class Second: def __init__(self): self.__second = 0 def set_second(self, second): if second < 0 or second >= 60: return False self.__second = second return True def get_second(self): return self.__second class Clock: def __init__(self): self.__hour = Hour() self.__minute = Minute() self.__second = Second() def set_time(self, hour, minute, second): if not self.__hour.set_hour(hour): return False if not self.__minute.set_minute(minute): return False if not self.__second.set_second(second): return False return True def tick(self): self.__second.set_second(self.__second.get_second() + 1) if self.__second.get_second() >= 60: self.__second.set_second(0) self.__minute.set_minute(self.__minute.get_minute() + 1) if self.__minute.get_minute() >= 60: self.__minute.set_minute(0) self.__hour.set_hour(self.__hour.get_hour() + 1) if self.__hour.get_hour() >= 24: self.__hour.set_hour(0) def time(self): hour = str(self.__hour.get_hour()).rjust(2, '0') minute = str(self.__minute.get_minute()).rjust(2, '0') second = str(self.__second.get_second()).rjust(2, '0') return hour + ':' + minute + ':' + second
这是一个时钟类的实现,包括 Hour、Minute、Second 和 Clock 四个类。Hour、Minute、Second 分别表示时、分、秒,实现了设置和获取对应值的方法,并且对输入值进行了校验。Clock 类组合了 Hour、Minute、Second 类,实现了设置时间、时钟走时和获取当前时间的方法。
其中 tick 方法实现了时钟走时的逻辑,每调用一次,秒数加一,如果秒数达到 60,则秒数清零,分钟加一,如果分钟达到 60,则分钟清零,小时加一,如果小时达到 24,则小时清零。
time 方法返回当前时间的字符串表示,格式为 HH:MM:SS。其中 str.rjust(n, char) 方法是将字符串填充到指定长度 n,填充字符为 char。这里用 0 进行左填充,确保输出的时间格式一致,比如 09:05:03。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)