Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
182
Task/Maze-solving/C-sharp/maze-solving.cs
Normal file
182
Task/Maze-solving/C-sharp/maze-solving.cs
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ proc m_maze pos . .
|
|||
show_maze
|
||||
d[] = [ 1 2 3 4 ]
|
||||
for i = 4 downto 1
|
||||
d = random i
|
||||
d = randint i
|
||||
dir = offs[d[d]]
|
||||
d[d] = d[i]
|
||||
if m[pos + dir] = 1 and m[pos + 2 * dir] = 1
|
||||
|
|
@ -43,7 +43,7 @@ proc make_maze . .
|
|||
m[n * i - n + 1] = 2
|
||||
m[n * n - n + i] = 2
|
||||
.
|
||||
h = 2 * random 15 - n + n * 2 * random 15
|
||||
h = 2 * randint 15 - n + n * 2 * randint 15
|
||||
m_maze h
|
||||
m[endpos] = 0
|
||||
.
|
||||
|
|
@ -68,7 +68,7 @@ proc solve dir0 pos . .
|
|||
found = 1
|
||||
return
|
||||
.
|
||||
of = random 4 - 1
|
||||
of = randint 4 - 1
|
||||
for h = 1 to 4
|
||||
dir = (h + of) mod1 4
|
||||
posn = pos + offs[dir]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import "os" for Process
|
||||
import "/ioutil" for File, FileUtil
|
||||
import "./ioutil" for File, FileUtil
|
||||
|
||||
/*
|
||||
* Makes the maze half as wide (i. e. "+---+" becomes "+-+"), so that
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue