走迷宫C#版(一)

本文介绍了一个使用C#编写的CMaze类,该类包含了迷宫的构造、绘图、路径搜索等功能。CMaze类通过Stack存储路径,采用深度优先搜索策略探索迷宫,并提供Run方法来自动解决迷宫。此外,还提供了Reset方法以重新开始迷宫,以及ToString方法用于输出字符形式的迷宫地图。
摘要由CSDN通过智能技术生成

//迷宫类相关

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;

namespace MazeDemo
{
 /// <summary>
 /// 迷宫类
 /// </summary>
 public class CMaze
 {
  bool[,] mg;  //地图格子
  Stack stack; //堆栈
  Point in_p;  //入口点
  Point out_p; //出口点
  Point start_p; //绘制迷时候的起始点
  Size boxsize; //每个格子的大小
  int step_count; //共走多少步

  public CMaze()
  {
   stack=new Stack();
   this.start_p=new Point(0,0);
   this.boxsize=new Size(50,50);
   step_count=0;
  }

  public CMaze(bool[,] _mg):this()
  {
   this.mg=_mg;
  }

  public CMaze(bool[,] _mg,Point _in,Point _out):this()
  {
   this.mg=_mg;
   this.in_p=_in;
   this.out_p=_out;
   Stack way=this.Test(this.in_p,_in);
   stack.Push(new CCoor(this.in_p,way));
   this.step_count++;
  }


  /// <summary>
  /// 绘制迷宫时窗口的起始坐标
  /// </summary>
  public Point StartPoint
  {
   set{this.start_p=value;}
   get{return this.start_p;}
  }

  /// <summary>
  /// 当前迷宫共走多少步
  /// </summary>
  public int StepCount
  {
   get{return this.step_count;}
  }

  /// <summary>
  /// 迷宫格子大小
  /// </summary>
  public Size BoxSize
  {
   set{this.boxsize=value;}
   get{return this.boxsize;}
  }

  /// <summary>
  /// 堆栈数据个数
  /

好的,以下是一个简单的 C# 实现走迷宫的算法: ```csharp using System; using System.Collections.Generic; class Program { static void Main(string[] args) { int[,] maze = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 1, 0, 0, 0, 1, 0, 1}, {1, 0, 0, 1, 0, 0, 0, 1, 0, 1}, {1, 0, 0, 0, 0, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; bool[,] visited = new bool[10, 10]; Point start = new Point(1, 1); Point end = new Point(8, 8); List<Point> path = FindPath(maze, visited, start, end); if (path != null) { Console.WriteLine("找到了一条路径:"); foreach (Point p in path) { Console.Write("({0}, {1}) -> ", p.X, p.Y); } Console.WriteLine("结束"); } else { Console.WriteLine("没有找到路径!"); } } static List<Point> FindPath(int[,] maze, bool[,] visited, Point start, Point end) { Queue<List<Point>> queue = new Queue<List<Point>>(); List<Point> path = new List<Point>(); path.Add(start); queue.Enqueue(path); while (queue.Count > 0) { List<Point> currPath = queue.Dequeue(); Point currPoint = currPath[currPath.Count - 1]; if (currPoint.Equals(end)) { return currPath; } visited[currPoint.X, currPoint.Y] = true; List<Point> neighbors = GetNeighbors(maze, visited, currPoint); foreach (Point neighbor in neighbors) { List<Point> newPath = new List<Point>(currPath); newPath.Add(neighbor); queue.Enqueue(newPath); } } return null; } static List<Point> GetNeighbors(int[,] maze, bool[,] visited, Point p) { List<Point> neighbors = new List<Point>(); if (p.X > 0 && maze[p.X - 1, p.Y] == 0 && !visited[p.X - 1, p.Y]) { neighbors.Add(new Point(p.X - 1, p.Y)); } if (p.X < maze.GetLength(0) - 1 && maze[p.X + 1, p.Y] == 0 && !visited[p.X + 1, p.Y]) { neighbors.Add(new Point(p.X + 1, p.Y)); } if (p.Y > 0 && maze[p.X, p.Y - 1] == 0 && !visited[p.X, p.Y - 1]) { neighbors.Add(new Point(p.X, p.Y - 1)); } if (p.Y < maze.GetLength(1) - 1 && maze[p.X, p.Y + 1] == 0 && !visited[p.X, p.Y + 1]) { neighbors.Add(new Point(p.X, p.Y + 1)); } return neighbors; } } class Point { public int X; public int Y; public Point(int x, int y) { X = x; Y = y; } public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return false; } Point other = (Point)obj; return X == other.X && Y == other.Y; } public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } } ``` 上述代码实现了一个简单的迷宫,其中 1 表示障碍物,0 表示可以通过的路。我们使用 BFS 算法来寻找从起点到终点的最短路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值