打开/关闭菜单
打开/关闭个人菜单
未登录
如果您进行任何编辑,您的IP地址会公开展示。

用户:JesseM1024:修订间差异

576次编辑2023年6月30日 (星期五)注册
(创建页面,内容为“代码备份如下 == 机械动力动力合成器矩阵周期最小化 ==”)
 
无编辑摘要
第2行: 第2行:


== 机械动力动力合成器矩阵周期最小化 ==
== 机械动力动力合成器矩阵周期最小化 ==
<code>def bfs_depth(grid, ini_pos):</code>
<code>   <nowiki>'''</nowiki></code>
<code>   grid: 2d np array, only 0, 1</code>
<code>   ini_pos: 2-len python int list, position vec [a,b]</code>
<code>   returns the depth 2d array</code>
<code>   <nowiki>'''</nowiki></code>
<code>   if not grid.size:</code>
<code>       return np.array([], dtype=int).reshape(0, 0)</code>
<code>   </code>
<code>   # 初始化访问数组并全部填充为-1</code>
<code>   visited = np.full_like(grid, -1, dtype=int)</code>
<code>   temp = ini_pos.copy()</code>
<code>   temp.append(0)</code>
<code>   # 起始点</code>
<code>   queue = deque([tuple(temp)])  # (row, col, depth)</code>
<code>   </code>
<code>   # 定义四个方向的移动</code>
<code>   directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]</code>
<code>   </code>
<code>   while queue:</code>
<code>       row, col, depth = queue.popleft()</code>
<code>       </code>
<code>       # 如果当前点不是障碍物(假设0表示障碍物),则更新访问数组</code>
<code>       if grid[row, col] != 0:</code>
<code>           visited[row, col] = depth</code>
<code>           </code>
<code>           # 检查相邻的点</code>
<code>           for dx, dy in directions:</code>
<code>               new_row, new_col = row + dx, col + dy</code>
<code>               </code>
<code>               # 如果相邻点在网格内且未被访问</code>
<code>               if 0 <= new_row < grid.shape[0] and 0 <= new_col < grid.shape[1] and visited[new_row, new_col] == -1:</code>
<code>                   queue.append((new_row, new_col, depth + 1))</code>
<code>   </code>
<code>   return visited</code>
<code>def edge_detector(grid):</code>
<code>   row, col = grid.shape[0], grid.shape[1]</code>
<code>   ava_out_pos_list = []</code>
<code>   directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]</code>
<code>   </code>
<code>   for i in range(row):</code>
<code>       for j in range(col):</code>
<code>           if grid[i,j]==1:</code>
<code>               if i==0 or i==row-1 or j==0 or j==col-1:</code>
<code>                   ava_out_pos_list.append([i,j])</code>
<code>               else:</code>
<code>                   exist_0 = False</code>
<code>                   for dx, dy in directions:</code>
<code>                       new_row, new_col = i + dx, j + dy</code>
<code>                       if grid[new_row, new_col] == 0:</code>
<code>                           exist_0 = True</code>
<code>                           break</code>
<code>                   if exist_0 :</code>
<code>                       ava_out_pos_list.append([i,j])</code>
<code>   return ava_out_pos_list</code>

2024年7月30日 (二) 21:05的版本

代码备份如下

机械动力动力合成器矩阵周期最小化

def bfs_depth(grid, ini_pos):

   '''

   grid: 2d np array, only 0, 1

   ini_pos: 2-len python int list, position vec [a,b]

   returns the depth 2d array

   '''

   if not grid.size:

       return np.array([], dtype=int).reshape(0, 0)

   

   # 初始化访问数组并全部填充为-1

   visited = np.full_like(grid, -1, dtype=int)

   temp = ini_pos.copy()

   temp.append(0)

   # 起始点

   queue = deque([tuple(temp)])  # (row, col, depth)

   

   # 定义四个方向的移动

   directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]

   

   while queue:

       row, col, depth = queue.popleft()

       

       # 如果当前点不是障碍物(假设0表示障碍物),则更新访问数组

       if grid[row, col] != 0:

           visited[row, col] = depth

           

           # 检查相邻的点

           for dx, dy in directions:

               new_row, new_col = row + dx, col + dy

               

               # 如果相邻点在网格内且未被访问

               if 0 <= new_row < grid.shape[0] and 0 <= new_col < grid.shape[1] and visited[new_row, new_col] == -1:

                   queue.append((new_row, new_col, depth + 1))

   

   return visited


def edge_detector(grid):

   row, col = grid.shape[0], grid.shape[1]

   ava_out_pos_list = []

   directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]

   

   for i in range(row):

       for j in range(col):

           if grid[i,j]==1:

               if i==0 or i==row-1 or j==0 or j==col-1:

                   ava_out_pos_list.append([i,j])

               else:

                   exist_0 = False

                   for dx, dy in directions:

                       new_row, new_col = i + dx, j + dy

                       if grid[new_row, new_col] == 0:

                           exist_0 = True

                           break

                   if exist_0 :

                       ava_out_pos_list.append([i,j])

   return ava_out_pos_list