Facebook
From g, 8 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 196
  1.  # Check adjacent cells
  2.     for ((i = 0; i < 4; i++)); do
  3.         local next_row=$((row + delta_row[i]))
  4.         local next_col=$((col + delta_col[i]))
  5.  
  6.         # Check if the next cell is valid
  7.         if [ $next_row -ge 0 ] && [ $next_row -lt $rows ] && [ $next_col -ge 0 ] && [ $next_col -lt $cols ] && [ ${labyrinth[$next_row,$next_col]} -eq 1 ] && [ ${visited[$next_row,$next_col]} -eq 0 ]; then
  8.             explore $next_row $next_col
  9.         fi
  10.     done
  11. }
  12.  
  13. # Main function
  14. main() {
  15.     # Define the delta for movement (up, down, left, right)
  16.     delta_row=(-1 1 0 0)
  17.     delta_col=(0 0 -1 1)
  18.  
  19.     # Define the labyrinth association table (1 represents pathway)
  20.     labyrinth=(
  21.         (1 0 1 1 1)
  22.         (1 1 1 0 1)
  23.         (0 0 0 1 1)
  24.         (1 1 1 1 0)
  25.         (1 0 1 0 1)
  26.     )
  27.  
  28.     rows=${#labyrinth[@]}
  29.     cols=${#labyrinth[0]}
  30.  
  31.     # Declare an associative array to keep track of visited cells
  32.     declare -A visited
  33.  
  34.     # Start exploring from the entrance (side of the table)
  35.     for ((i = 0; i < $rows; i++)); do
  36.         if [ ${labyrinth[$i,0]} -eq 1 ]; then
  37.             explore $i 0
  38.         fi
  39.     done
  40.  
  41.     echo "No exit found."
  42.     exit 1
  43. }
  44.  
  45. main

Replies to Untitled rss

Title Name Language When
Re: Untitled g text 8 Months ago.