Guest User

dfs_match3_algo

a guest
Apr 29th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | Source Code | 0 0
  1. def find_lines(grid):
  2.     lines = []
  3.     rows = len(grid)
  4.     cols = len(grid[0])
  5.  
  6.     # Check horizontal lines
  7.     for r in range(rows):
  8.         count = 1
  9.         for c in range(1, cols):
  10.             if grid[r][c] == grid[r][c - 1] and grid[r][c] in ('b', 'c'):
  11.                 count += 1
  12.             else:
  13.                 if count >= 2:
  14.                     lines.append([(r, c - i) for i in range(count)])
  15.                 count = 1
  16.         if count >= 2:
  17.             lines.append([(r, cols - i - 1) for i in range(count)])
  18.  
  19.     # Check vertical lines
  20.     for c in range(cols):
  21.         count = 1
  22.         for r in range(1, rows):
  23.             if grid[r][c] == grid[r - 1][c] and grid[r][c] in ('b', 'c'):
  24.                 count += 1
  25.             else:
  26.                 if count >= 2:
  27.                     lines.append([(r - i, c) for i in range(count)])
  28.                 count = 1
  29.         if count >= 2:
  30.             lines.append([(rows - i - 1, c) for i in range(count)])
  31.  
  32.     return lines
  33.  
  34. #Check Adjacency -- NOT USING
  35. def is_adjacent(d_pos, a_pos):
  36.     return (abs(d_pos[0] - a_pos[0]) <= 1 and abs(d_pos[1] - a_pos[1]) <= 1)
  37.  
  38.  
  39. #Path Traversal:
  40. def traverse_lines(grid, d_pos, lines):
  41.     visited = set()
  42.     stack = [d_pos]
  43.    
  44.     while stack:
  45.         current = stack.pop()
  46.         if current in visited:
  47.             continue
  48.         visited.add(current)
  49.  
  50.         # Check if current position is part of any line
  51.         for line in lines:
  52.             if current in line:
  53.                 # Mark the line as visited
  54.                 for pos in line:
  55.                     visited.add(pos)
  56.                 # Add adjacent positions to stack
  57.                 for pos in line:
  58.                     for adj in get_adjacent_positions(pos):
  59.                         if adj not in visited:
  60.                             stack.append(adj)
  61.  
  62.  
  63. # Return all adjacent positions (up, down, left, right)
  64. def get_adjacent_positions(pos):
  65.     return [(pos[0] + dx, pos[1] + dy) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]]
  66.  
  67.  
  68. def main(grid):
  69.     d_pos = None
  70.     a_pos = None
  71.     for r in range(len(grid)):
  72.         for c in range(len(grid[0])):
  73.             #print(r, "   ", c)
  74.             if grid[r][c] == 'd':
  75.                 d_pos = (r, c)
  76.             elif grid[r][c] == 'a':
  77.                 a_pos = (r, c)
  78.  
  79.     lines = find_lines(grid)
  80.     print("lines:   ", lines)
  81.     traverse_lines(grid, d_pos, lines)
  82.     print("Traversal complete.")
  83.  
  84. # Example grid
  85. grid = [
  86.     ['a', 'a', 'b', 'a', 'a'],
  87.     ['a', 'a', 'b', 'a', 'a'],
  88.     ['a', 'a', 'b', 'b', 'd'],
  89.     ['c', 'c', 'c', 'c', 'c'],
  90.     ['a', 'a', 'c', 'a', 'a'],
  91.     ['a', 'a', 'c', 'a', 'a']
  92. ]
  93.  
  94. if __name__ == '__main__':
  95.     main(grid)
  96.  
  97.  
  98.  
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment