Facebook
From g, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 107
  1. #!/bin/bash
  2.  
  3. # Function to recursively explore the labyrinth
  4. explore() {
  5.     local row=$1
  6.     local col=$2
  7.  
  8.     # Mark the current cell as visited
  9.     visited[$row,$col]=1
  10.  
  11.     # If we reached the center, exit
  12.     if [ $row -eq $((rows / 2)) ] && [ $col -eq $((cols / 2)) ]; then
  13.         echo "Exit found at row $row, column $col."
  14.         exit 0
  15.     fi
  16.  
  17.     # Check adjacent cells
  18.     for ((i = 0; i < 4; i++)); do
  19.         local next_row=$((row + delta_row[i]))
  20.         local next_col=$((col + delta_col[i]))
  21.  
  22.         # Check if the next cell is valid
  23.         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
  24.             explore $next_row $next_col
  25.         fi
  26.     done
  27. }
  28.  
  29. # Main function
  30. main() {
  31.     # Define the delta for movement (up, down, left, right)
  32.     delta_row=(-1 1 0 0)
  33.     delta_col=(0 0 -1 1)
  34.  
  35.     # Define the labyrinth association table (1 represents pathway)
  36.     labyrinth=(
  37.         "1 0 1 1 1"
  38.         "1 1 1 0 1"
  39.         "0 0 0 1 1"
  40.         "1 1 1 1 0"
  41.         "1 0 1 0 1"
  42.     )
  43.  
  44.     rows=${#labyrinth[@]}
  45.     cols=$(echo "${labyrinth[0]}" | wc -w)
  46.  
  47.     # Declare an associative array to keep track of visited cells
  48.     declare -A visited
  49.  
  50.     # Start exploring from the entrance (side of the table)
  51.     for ((i = 0; i < $rows; i++)); do
  52.         if [ "${labyrinth[$i]}" == "1" ]; then
  53.             explore $i 0
  54.         fi
  55.     done
  56.  
  57.     echo "No exit found."
  58.     exit 1
  59. }
  60.  
  61. main