📄 النص الكامل للصفحة
يمكن استخدام الدالة التالية لاستدعاء قائمة تحتوي على كل الخلايا الفارغة والمجاورة لخلية محددة في أي متاهة:
def get_accessible_neighbors(maze:np.ndarray, cell:tuple):
# list of accessible neighbors, initialized to empty
neighbors=[]
x,y=cell
# for each adjacent cell position
for i,j in [(x-1,y-1), (x-1,y), (x-1,y+1), (x,y-1), (x,y+1), (x+1,y-1), (x+1,y), (x+1,y+1)]:
# if the adjacent cell is within the bounds of the grid and is not occupied by a block
if i>=0 and j>=0 and i<len(maze) and j<len(maze[0]) and \
maze[(i,j)]==0:
neighbors.append(((i,j),1))
return neighbors
يفترض هذا التطبيق أن كل عملية انتقال من خلية إلى أخرى مجاورة سواء أفقياً أو رأسياً أو قطرياً يتم بتكلفة مقدارها وحدة واحدة فقط. سيتم إعادة النظر في هذه الفرضية في وقت لاحق من هذا الدرس بعرض حالات أكثر تعقيداً مع شروط انتقال متغيرة.
تستخدم كل خوارزميات البحث دالة ()get_accessible_neighbors في محاولة حل المتاهة. في الأمثلة التالية تستخدم المتاهة 3×3 المصممة بالأعلى للتحقق من أن الدالة تستدعي الخلية الصحيحة الفارغة والمجاورة للخلية المحددة.
# this cell is the northwest corner of the grid and has only 2 accessible neighbors
get_accessible_neighbors(small_maze, (0,0))
[((0, 1), 1), ((1, 0), 1)]
# the starting cell (in the southwest corner) has only 1 accessible neighbor
get_accessible_neighbors(small_maze, (2,0))
[((1, 0), 1)]
بعد أن تعلمت كيفية إنشاء المتاهات، وكذلك استدعاء الخلايا المجاورة لأي خلية في المتاهة، فإن الخطوة التالية هي تطبيق خوارزميات البحث التي يمكنها حل المتاهة من خلال إيجاد المسار الأقصر من خلية البداية إلى خلية الهدف المحددة.
--- SECTION: شكل 2.20: الخلايا المجاورة ---
شكل 2.20: الخلايا المجاورة
وزارة التعليم
111
2023 - 1447
--- VISUAL CONTEXT ---
**TABLE**: Adjacent Cell Positions
Description: A 3x3 grid illustrating the relative coordinates of a cell's 8 neighbors and itself, centered around (x, y).
Table Structure:
Headers: N/A
Rows:
Row 1: x-1, y-1 | x-1, y | x-1, y+1
Row 2: x, y-1 | x, y | x, y+1
Row 3: x+1, y-1 | x+1, y | x+1, y+1
Data: The grid shows the relative (x,y) coordinates for a cell's neighbors. The center cell is (x, y). The cells are arranged as follows: top-left (x-1, y-1), top-middle (x-1, y), top-right (x-1, y+1), middle-left (x, y-1), middle-right (x, y+1), bottom-left (x+1, y-1), bottom-middle (x+1, y), bottom-right (x+1, y+1).
Key Values: x-1, y-1, x-1, y, x-1, y+1, x, y-1, x, y, x, y+1, x+1, y-1, x+1, y, x+1, y+1
Context: This visual explains the `for i,j in [...]` loop in the `get_accessible_neighbors` Python function, showing the 8 possible relative positions for neighbors around a central cell (x, y) in a grid.
**DIAGRAM**: Untitled
Description: A 3x3 grid representing a small maze. The grid has row and column indices from 0 to 2. Some cells are light blue (representing empty/accessible paths), and others are dark grey (representing blocked cells).
X-axis: Column Index
Y-axis: Row Index
Data: The grid cells are indexed (row, column). The accessible (light blue) cells are (0,0), (0,1), (0,2), (1,0), (2,0). The blocked (dark grey) cells are (1,1), (1,2), (2,1), (2,2).
Key Values: Grid size: 3x3, Accessible cells: (0,0), (0,1), (0,2), (1,0), (2,0), Blocked cells: (1,1), (1,2), (2,1), (2,2)
Context: This grid serves as the `small_maze` example for demonstrating the `get_accessible_neighbors` function, specifically for the call `get_accessible_neighbors(small_maze, (0,0))` which refers to the top-left corner.
**DIAGRAM**: الخلايا المجاورة
Description: A 3x3 grid representing a small maze, identical in structure to the previous one, but with specific cells highlighted and labeled with callout boxes to illustrate a starting cell and its accessible neighbor.
X-axis: Column Index
Y-axis: Row Index
Data: The grid cells are indexed (row, column). The accessible (light blue) cells are (0,0), (0,1), (0,2), (1,0), (2,0). The blocked (dark grey) cells are (1,1), (1,2), (2,1), (2,2). A blue circle highlights cell (2,0) labeled 'خلية البداية' (Starting Cell). Another blue circle highlights cell (1,0) labeled 'الخلية المجاورة' (Adjacent Cell), with an arrow pointing from (2,0) to (1,0).
Key Values: Grid size: 3x3, Starting Cell: (2,0) - 'خلية البداية', Adjacent Cell: (1,0) - 'الخلية المجاورة', Accessible cells: (0,0), (0,1), (0,2), (1,0), (2,0), Blocked cells: (1,1), (1,2), (2,1), (2,2)
Context: This grid, labeled as 'شكل 2.20: الخلايا المجاورة', serves as another `small_maze` example for the `get_accessible_neighbors` function. It specifically illustrates the call `get_accessible_neighbors(small_maze, (2,0))`, showing the starting cell in the southwest corner and its single accessible neighbor.