C# 使用递归DFS打印迷宫中所有可能的路径
这就是任务:给你一个迷宫,它由N x N个正方形组成,每个正方形都可以通行,也可以不通行。可通过的单元格由“a”和“z”之间的较低拉丁字母以及不可通过的–“#”组成。在其中一个广场上是杰克。它标有“*” 两个正方形是相邻的,如果它们有共同的墙。杰克可以一步一步地从一个可通行的广场通过到相邻的可通行广场。当杰克经过可通行的广场时,他写下每个广场上的字母。在每个出口他都会听到一句话。编写一个程序,从给定的迷宫打印单词,杰克从所有可能的出口得到。 输入数据从名为迷宫的文本文件中读取。在文件的第一行有数字N(2C# 使用递归DFS打印迷宫中所有可能的路径,c#,algorithm,recursion,depth-first-search,C#,Algorithm,Recursion,Depth First Search,这就是任务:给你一个迷宫,它由N x N个正方形组成,每个正方形都可以通行,也可以不通行。可通过的单元格由“a”和“z”之间的较低拉丁字母以及不可通过的–“#”组成。在其中一个广场上是杰克。它标有“*” 两个正方形是相邻的,如果它们有共同的墙。杰克可以一步一步地从一个可通行的广场通过到相邻的可通行广场。当杰克经过可通行的广场时,他写下每个广场上的字母。在每个出口他都会听到一句话。编写一个程序,从给定的迷宫打印单词,杰克从所有可能的出口得到。 输入数据从名为迷宫的文本文件中读取。在文件的第一行有数
6
a##km#
z#ada#
a*m###
#d####
rifid#
#d#d#t
到目前为止,我已经做到了:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
public class Maze
{
private const string InputFileName = "Labyrinth.in";
private const string OutputFileName = "Labyrinth.out";
StringBuilder path = new StringBuilder();
public class Cell
{
public int Row { get; set; }
public int Column { get; set; }
public Cell(int row, int column)
{
this.Row = row;
this.Column = column;
}
}
private char[,] maze;
private int size;
private Cell startCell = null;
public void ReadFromFile(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
{
// Read maze size and create maze
this.size = int.Parse(reader.ReadLine());
this.maze = new char[this.size, this.size];
// Read the maze cells from the file
for (int row = 0; row < this.size; row++)
{
string line = reader.ReadLine();
for (int col = 0; col < this.size; col++)
{
this.maze[row, col] = line[col];
if (line[col] == '*')
{
this.startCell = new Cell(row, col);
}
}
}
}
}
public void FindAllPathsAndPrintThem()
{
if (this.startCell == null)
{
// Start cell is missing -> no path
SaveResult(OutputFileName, "");
return;
}
VisitCell(this.startCell.Row,
this.startCell.Column, path);
if (path.Length == 0)
{
// We didn't reach any cell at the maze border -> no path
SaveResult(OutputFileName, "");
return;
}
}
private void VisitCell(int row, int column, StringBuilder path)
{
if (row < 0 || row > maze.GetLength(0) - 1 ||
column < 0 || column > maze.GetLength(1) - 1)
{
SaveResult(OutputFileName, path.ToString());
return;
}
if (this.maze[row, column] != 'x' && this.maze[row, column] != '#')
{
// The cell is free --> visit it
if (this.maze[row, column] != '*')
{
path.Append(this.maze[row, column]);
this.maze[row, column] = 'x';
}
VisitCell(row, column + 1, path);
VisitCell(row, column - 1, path);
VisitCell(row + 1, column, path);
VisitCell(row - 1, column, path);
}
}
public void SaveResult(string fileName, string result)
{
using (StreamWriter writer = new StreamWriter(fileName))
{
writer.WriteLine(result);
}
}
static void Main()
{
Maze maze = new Maze();
maze.ReadFromFile(InputFileName);
maze.FindAllPathsAndPrintThem();
}
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用系统文本;
公共类迷宫
{
私有常量字符串InputFileName=“迷宫.in”;
私有常量字符串OutputFileName=“迷宫.out”;
StringBuilder路径=新建StringBuilder();
公共类单元
{
公共int行{get;set;}
公共int列{get;set;}
公共单元格(int行,int列)
{
this.Row=行;
this.Column=Column;
}
}
私有字符[,]迷宫;
私有整数大小;
专用单元startCell=null;
public void ReadFromFile(字符串文件名)
{
使用(StreamReader=新StreamReader(文件名))
{
//读取迷宫大小并创建迷宫
this.size=int.Parse(reader.ReadLine());
this.maze=新字符[this.size,this.size];
//从文件中读取迷宫单元
for(int row=0;row无路径
保存结果(OutputFileName,“”);
返回;
}
VisitCell(this.startCell.Row,
this.startCell.Column,path);
if(path.Length==0)
{
//我们没有到达迷宫边界的任何细胞->没有路径
保存结果(OutputFileName,“”);
返回;
}
}
私有void VisitCell(int行、int列、StringBuilder路径)
{
如果(行<0 | |行>maze.GetLength(0)-1||
列<0 | |列>maze.GetLength(1)-1)
{
SaveResult(OutputFileName,path.ToString());
返回;
}
if(this.maze[row,column]!='x'&&this.maze[row,column]!='#')
{
//该单元格是免费的-->请访问它
if(this.maze[行,列]!='*'))
{
Append(this.maze[row,column]);
this.maze[行,列]='x';
}
VisitCell(行、列+1、路径);
VisitCell(行,列-1,路径);
VisitCell(行+1、列、路径);
VisitCell(行1、列、路径);
}
}
public void SaveResult(字符串文件名、字符串结果)
{
使用(StreamWriter=newstreamwriter(文件名))
{
writer.WriteLine(结果);
}
}
静态void Main()
{
迷宫=新迷宫();
maze.ReadFromFile(InputFileName);
maze.findallpath并打印它们();
}
}
很抱歉问了这么长时间。需要有一个小错误,但我不知道它在哪里。
输出为Madifiddrzaadamk。提前谢谢。我想出了一个解决方案。它记录了在一次穿越迷宫的过程中访问过的细胞,但不是所有的尝试。这可以通过使函数返回一个
IEnumerable
来实现,该函数将表示迷宫中当前点的出口路径。首先检查你是否已经离开迷宫的边缘,并且什么也不返回。然后检查你是否在一堵墙上,如果是的话,没有路径返回。否则,检查是否在边缘,如果在边缘,则返回仅由当前单元格表示的路径。然后,您必须将当前单元格标记为已访问,然后尝试在4个方向中的每个方向上查找所有路径,并将当前单元格连接到找到的任何单元格,并生成它们。然后在最后,您将该单元标记为未访问,以便它可以用于其他尝试
private static IEnumerable<string> VisitCell(int row, int column, bool[,] visited)
{
if (row < 0 || column < 0 || row >= maze.GetLength(0) || column >= maze.GetLength(1))
yield break;
if (maze[row, column] == '#' || visited[row, column])
yield break;
if (row == 0 || row == maze.GetLength(0) - 1 ||
column == 0 || column == maze.GetLength(1) - 1)
{
yield return maze[row, column].ToString();
}
visited[row, column] = true;
foreach (var path in VisitCell(row, column + 1, visited))
{
yield return maze[row, column] + path;
}
foreach(var path in VisitCell(row, column - 1, visited))
{
yield return maze[row, column] + path;
}
foreach (var path in VisitCell(row + 1, column, visited))
{
yield return maze[row, column] + path;
}
foreach (var path in VisitCell(row - 1, column, visited))
{
yield return maze[row, column] + path;
}
visited[row, column] = false;
}
为了得到这个结果
*夫人
*马达姆克
*麦德
*马德姆
*a
*阿兹
*阿扎
*迪菲德
*迪尔
*做过
您可以调整它以从每个路径的开头删除星形。以下是正确的代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
public class Maze
{
private const string InputFileName = "Labyrinth.in";
private const string OutputFileName = "Labyrinth.out";
public class Cell
{
public int Row { get; set; }
public int Column { get; set; }
public Cell(int row, int column)
{
this.Row = row;
this.Column = column;
}
}
private char[,] maze;
private int size;
private Cell startCell = null;
public void ReadFromFile(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
{
// Read maze size and create maze
this.size = int.Parse(reader.ReadLine());
this.maze = new char[this.size, this.size];
// Read the maze cells from the file
for (int row = 0; row < this.size; row++)
{
string line = reader.ReadLine();
for (int col = 0; col < this.size; col++)
{
this.maze[row, col] = line[col];
if (line[col] == '*')
{
this.startCell = new Cell(row, col);
}
}
}
}
}
public void FindAllPathsAndPrintThem()
{
if (this.startCell == null)
{
// Start cell is missing -> no path
SaveResult(OutputFileName, "");
return;
}
VisitCell(this.startCell.Row,
this.startCell.Column, "");
}
private void VisitCell(int row, int column, string path)
{
if (row < 0 || column < 0 || row >= maze.GetLength(0) || column >= maze.GetLength(1))
{
return;
}
if (row < 0 || row > maze.GetLength(0) - 1 ||
column < 0 || column > maze.GetLength(1) - 1)
{
SaveResult(OutputFileName, path);
return;
}
if (this.maze[row, column] != '#')
{
// The cell is free --> visit it
char x = this.maze[row, column];
this.maze[row, column] = '#';
VisitCell(row, column + 1, path + x);
VisitCell(row, column - 1, path + x);
VisitCell(row + 1, column, path + x);
VisitCell(row - 1, column, path + x);
this.maze[row, column] = x;
}
}
public void SaveResult(string fileName, string result)
{
using (StreamWriter writer = new StreamWriter(fileName, true))
{
writer.WriteLine(result);
}
}
static void Main()
{
Maze maze = new Maze();
maze.ReadFromFile(InputFileName);
maze.FindAllPathsAndPrintThem();
}
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用系统文本;
公共类迷宫
{
私有常量字符串InputFileName=“迷宫.in”;
私有常量字符串OutputFileName=“迷宫.out”;
公共类单元
{
公共int行{get;set;}
公共int列{get;set;}
公共单元格(int行,int列)
{
this.Row=行;
this.Column=Column;
}
}
私有字符[,]迷宫;
私有整数大小;
专用单元startCell=null;
public void ReadFromFile(字符串文件名)
{
使用(StreamReader=新StreamReader(文件名))
{
//读取迷宫大小和c
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
public class Maze
{
private const string InputFileName = "Labyrinth.in";
private const string OutputFileName = "Labyrinth.out";
public class Cell
{
public int Row { get; set; }
public int Column { get; set; }
public Cell(int row, int column)
{
this.Row = row;
this.Column = column;
}
}
private char[,] maze;
private int size;
private Cell startCell = null;
public void ReadFromFile(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
{
// Read maze size and create maze
this.size = int.Parse(reader.ReadLine());
this.maze = new char[this.size, this.size];
// Read the maze cells from the file
for (int row = 0; row < this.size; row++)
{
string line = reader.ReadLine();
for (int col = 0; col < this.size; col++)
{
this.maze[row, col] = line[col];
if (line[col] == '*')
{
this.startCell = new Cell(row, col);
}
}
}
}
}
public void FindAllPathsAndPrintThem()
{
if (this.startCell == null)
{
// Start cell is missing -> no path
SaveResult(OutputFileName, "");
return;
}
VisitCell(this.startCell.Row,
this.startCell.Column, "");
}
private void VisitCell(int row, int column, string path)
{
if (row < 0 || column < 0 || row >= maze.GetLength(0) || column >= maze.GetLength(1))
{
return;
}
if (row < 0 || row > maze.GetLength(0) - 1 ||
column < 0 || column > maze.GetLength(1) - 1)
{
SaveResult(OutputFileName, path);
return;
}
if (this.maze[row, column] != '#')
{
// The cell is free --> visit it
char x = this.maze[row, column];
this.maze[row, column] = '#';
VisitCell(row, column + 1, path + x);
VisitCell(row, column - 1, path + x);
VisitCell(row + 1, column, path + x);
VisitCell(row - 1, column, path + x);
this.maze[row, column] = x;
}
}
public void SaveResult(string fileName, string result)
{
using (StreamWriter writer = new StreamWriter(fileName, true))
{
writer.WriteLine(result);
}
}
static void Main()
{
Maze maze = new Maze();
maze.ReadFromFile(InputFileName);
maze.FindAllPathsAndPrintThem();
}
}