Maze solving: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(10 intermediate revisions by 3 users not shown)
Line 676:
=={{header|C}}==
See [[Maze_generation#C|Maze generation]] for combined gen/solve code.
 
 
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
using System.Text;
 
public class Maze
{
private char[,] cells;
private char[,] hWalls; // Horizontal walls
private char[,] vWalls; // Vertical walls
private Random rand = new Random();
 
public Maze(int rows, int cols)
{
cells = new char[rows, cols];
hWalls = new char[rows + 1, cols]; // Include walls for the bottom
vWalls = new char[rows, cols + 1]; // Include walls for the right side
 
// Initialize walls
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
hWalls[i, j] = '-';
vWalls[i, j] = '|';
}
}
 
// Set the outer walls for the bottom and right
for (int i = 0; i < cols; i++)
{
hWalls[rows, i] = '-';
}
for (int i = 0; i < rows; i++)
{
vWalls[i, cols] = '|';
}
}
 
public override string ToString()
{
var builder = new StringBuilder();
 
for (int i = 0; i < cells.GetLength(0); i++)
{
// Top walls
for (int j = 0; j < cells.GetLength(1); j++)
{
builder.Append("+");
builder.Append(hWalls[i, j] == '-' ? "---" : " ");
}
builder.AppendLine("+");
 
// Side walls and cells
for (int j = 0; j < cells.GetLength(1); j++)
{
builder.Append(vWalls[i, j] == '|' ? "| " : " ");
char cell = cells[i, j] == '\0' ? ' ' : cells[i, j];
builder.Append(cell + " ");
}
builder.AppendLine("|");
}
 
// Bottom walls
for (int j = 0; j < cells.GetLength(1); j++)
{
builder.Append("+---");
}
builder.AppendLine("+");
 
return builder.ToString();
}
 
public void Generate()
{
Generate(rand.Next(cells.GetLength(0)), rand.Next(cells.GetLength(1)));
}
 
private void Generate(int r, int c)
{
cells[r, c] = ' ';
int[] dirs = { 0, 1, 2, 3 };
Shuffle(dirs);
 
foreach (int dir in dirs)
{
switch (dir)
{
case 0: // Up
if (r > 0 && cells[r - 1, c] == '\0')
{
hWalls[r, c] = ' ';
Generate(r - 1, c);
}
break;
case 1: // Down
if (r < cells.GetLength(0) - 1 && cells[r + 1, c] == '\0')
{
hWalls[r + 1, c] = ' ';
Generate(r + 1, c);
}
break;
case 2: // Right
if (c < cells.GetLength(1) - 1 && cells[r, c + 1] == '\0')
{
vWalls[r, c + 1] = ' ';
Generate(r, c + 1);
}
break;
case 3: // Left
if (c > 0 && cells[r, c - 1] == '\0')
{
vWalls[r, c] = ' ';
Generate(r, c - 1);
}
break;
}
}
}
 
private void Shuffle(int[] array)
{
for (int i = array.Length - 1; i > 0; i--)
{
int j = rand.Next(i + 1);
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
 
public void Solve(int startRow, int startCol, int endRow, int endCol)
{
if (Solve(startRow, startCol, endRow, endCol, -1))
{
cells[startRow, startCol] = 'S';
cells[endRow, endCol] = 'F';
}
}
 
private bool Solve(int r, int c, int endRow, int endCol, int dir)
{
if (r == endRow && c == endCol) return true;
 
// Up
if (dir != 1 && r > 0 && hWalls[r, c] == ' ' && Solve(r - 1, c, endRow, endCol, 0))
{
cells[r, c] = '^';
return true;
}
// Down
if (dir != 0 && r < cells.GetLength(0) - 1 && hWalls[r + 1, c] == ' ' && Solve(r + 1, c, endRow, endCol, 1))
{
cells[r, c] = 'v';
return true;
}
// Right
if (dir != 3 && c < cells.GetLength(1) - 1 && vWalls[r, c + 1] == ' ' && Solve(r, c + 1, endRow, endCol, 2))
{
cells[r, c] = '>';
return true;
}
// Left
if (dir != 2 && c > 0 && vWalls[r, c] == ' ' && Solve(r, c - 1, endRow, endCol, 3))
{
cells[r, c] = '<';
return true;
}
 
return false;
}
}
 
class Program
{
static void Main(string[] args)
{
var maze = new Maze(4, 7);
maze.Generate();
Random rand = new Random();
maze.Solve(rand.Next(4), rand.Next(7), rand.Next(4), rand.Next(7));
Console.WriteLine(maze);
}
}
</syntaxhighlight>
{{out}}
<pre>
+---+---+---+---+---+---+---+
| | | v S |
+ +---+ +---+ + +---+
| | | | > v |
+---+---+ + +---+---+ +
| | | | F |
+ +---+---+ + + + +
| | |
+---+---+---+---+---+---+---+
 
 
</pre>
 
=={{header|C++}}==
Line 1,434 ⟶ 1,636:
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=hZTbbqMwFEXf/RVb6ksuwjUpSIk0zI8gNKJgGitgRyZNm379yBeM3RnNvCT4XIy9zt7M4oujQl4SiQoH7DCbyB45GUycMTxjI5GB0XJLRi4x1Q0kdpDkCeS17S5vWr3LHowxctWqw3xWH7+m9ouDghIA3chbbR4GpSHMtrgp+L1MHIAYMNWiQQXmIwA+UWEjkCHfYlI95Jp6RKle3ONUp0alcTqd1tCk7hyf2GFAhgHPOOARr9ZKzbsbBuyQ03L591l7F/szj5xfwSjLCSVqGObaHLxGDoksxyaTWzQOxuRIXNW80Jjqq5rDRQMss+jDPge8oEATQyvQqw95U8j9eXpU0K3shbxBLDGhUcEeqa/7ZqFrnlGhr0XC25xqb3oaO5RW9iFopLAkIo5pSzSq6J6hOeVGCZe9yVdOPmZ2nlF74Ylg/qETL5I8zCKplWnVISzNG/8WQgZp1P5nSvrU2mVfd/Y2WcDnpa8zPWmCrFjObvAOgCNHSbg2WUXwhAWJvliYnRoXKtYNJpb64ZGEgxecCzo1klT/e6v4wuvfr2yD0N3I7fqFloSSt1G9tiMG627qva3GOzezZbGkxeCrVrFofnvXMmAL9zkxljio9P1OGA6Q3+I/W6oh0n9h1eTlcF7kUCSu2Jyxhxosujzkrmo2nz4nXGccoSOXmN4fP92Vg0HkN/U7LBtTu1+GU5hHu/1aF4GKuiM+EsfjMckEUlGYfneWK8qJOwezgjyQ3w== Run it]
[https://easylang.online/apps/_r_maze.html Run it]
 
<syntaxhighlight lang="text">size = 20
size = 15
n = 2 * size + 1
endpos = n * n - 2
startpos = n + 1
#
f = 100 / (n - 0.5)
len m[] n * n
#
background 000
funcproc show_maze . .
clear
for i range= 1 to len m[]
if m[i] = 0
x = (i - 1) mod n
y = (i - 1) div n
color 777999
move x * f - f / 2 y * f - f / 2
rect f * 1.5 f * 1.5
.
.
sleep 0.00101
.
offs[] = [ 1 n -1 (-n) ]
proc m_maze pos . .
brdc[] = [ n - 2 -1 1 -1 ]
m[pos] = 0
brdr[] = [ -1 n - 2 -1 1 ]
show_maze
#
d[] = [ 1 2 3 4 ]
func m_maze pos . .
m[pos] for i = 04 downto 1
d = randint i
call show_maze
d[] = [ 0 1dir 2 3= offs[d[d]]
for i = 3 downtod[d] 0= d[i]
d if m[pos + dir] = random1 (iand m[pos + 2 * dir] = 1)
dir = d m[dpos + dir] = 0
m_maze pos + 2 * dir
d[d] = d[i]
r = pos div n.
.
c = pos mod n
posn = pos + 2 * offs[dir]
if c <> brdc[dir] and r <> brdr[dir] and m[posn] <> 0
posn = pos + 2 * offs[dir]
m[(pos + posn) div 2] = 0
call m_maze posn
.
.
.
endpos = n * n - 1
func make_maze . .
proc make_maze . .
for i range len m[]
for m[i] = 1 to len m[]
m[i] = 1
.
.
call m_maze startpos
m[endpos] for i = 01 to n
m[i] = 2
m[n * i] = 2
m[n * i - n + 1] = 2
m[n * n - n + i] = 2
.
h = 2 * randint 15 - n + n * 2 * randint 15
m_maze h
m[endpos] = 0
.
call make_maze
call show_maze
#
funcproc mark pos col . .
x = (pos - 1) mod n
y = (pos - 1) div n
color col
move x * f + f / 4 y * f + f / 4
circle f / 43.5
.
func solve dir0 pos .global found .
proc solve call markdir0 pos 900. .
if found = 1
sleep 0.05
if pos = endpos return
found = 1.
mark pos 900
else
sleep 0.05
for dir range 4
if pos = endpos
found = 1
return
.
of = randint 4 - 1
for h = 1 to 4
dir = (h + of) mod1 4
posn = pos + offs[dir]
if dir <> dir0 and m[posn] = 0 and found = 0
call solve (dir + 21) mod 4 posn+ found1 posn
if found = 0
call mark posn 777888
sleep 0.0508
.
.
.
.
.
sleep 31
solve 0 n + 2
call solve -1 startpos found</syntaxhighlight>
</syntaxhighlight>
 
=={{header|EGL}}==
Line 5,600 ⟶ 5,808:
{{trans|Kotlin}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "os" for Process
import "./ioutil" for File, FileUtil
 
/*
1,983

edits