From g, 1 Month ago, written in Plain Text.
This paste is a reply to Untitled from g - view diff
Embed
Hits: 125
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 &quot;Exit found at row \$row, column \$col.&quot;
14.         exit 0
15.     fi
16.
18.     for ((i = 0; i &lt; 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=\${#labyrinth[0]}
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.     local entrance_row=0
52.     local entrance_col=0
53.     while [ \${labyrinth[\$entrance_row,\$entrance_col]} -ne 1 ]; do
54.         ((entrance_row++))
55.     done
56.
57.     explore \$entrance_row \$entrance_col
58.
59.     echo &quot;No exit found.&quot;
60.     exit 1
61. }
62.
63. main

# Replies to Re: Untitled

Title Name Language When
Re: Re: Untitled g text 1 Month ago.