栈和队列 迷宫求解

用WPF做演示

1.用Stack记录和回溯

在现实生活中,在你迷路的时候,总是记录一些可记忆的建筑物作为返回的标志(比如你出去玩,总得回家的吧,那么就得记得回家的路)

(1)画迷宫

public class MazeElement : FrameworkElement
{
    public MazeElement(int[,] mg)
    {
        this.mg = mg;
    }

    int[,] mg=null;

    protected override void OnRender(DrawingContext dc)
    {
        var width = 40;
        dc.DrawRectangle(Brushes.Black, null, new Rect(0, 0, 10 * width, 10 * width));
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                if (mg[i, j] == 1)
                {
                    dc.DrawRectangle(Brushes.Gray, null, new Rect(j * (width), i * (width), width - 1, width - 1));
                }
                else
                {
                    dc.DrawRectangle(Brushes.White, null, new Rect(j * (width), i * (width), width - 1, width - 1));
                }
            }
        }
    }
}

image

(2)白色表示可以通过的坐标,从坐标1,1寻找坐标(8,8)点

一个块的的数据结构如下,即坐标点加下个可寻找点的方向

public class Block
{
    public Point Point { get; set; }

    public int Direction { get; set; }
}

(3)若找到的下个是白点且没有重复的话,则继续寻找,否则就回溯,找另外的出口,其实就是穷举法了…

算法如下

public void MgPath(Point start, Point end)
{

    Block temp = null;
    bool find = false;
    while (st.Count > 0)
    {
        temp = st.Peek();
        _point = temp.Point;

        Console.WriteLine(temp.Point.ToString());
        var point = temp.Point;
        var di = temp.Direction;

        //UI Refresh don't care
        System.Threading.Thread.Sleep(100);
        Dispatcher.BeginInvoke(new Action(() =>
        {
            this.InvalidateVisual();
        }), DispatcherPriority.Render);

        if (point.Equals(end))
        {
            finished = true;
            break;
        }

        //not find
        find = false;
        //find next block
        while (di < 4 && !find)
        {
            di++;
            switch (di)
            {
                case 0://top
                    point.Y = temp.Point.Y - 1;
                    point.X = temp.Point.X;
                    break;
                case 1://right
                    point.X = temp.Point.X + 1;
                    point.Y = temp.Point.Y;
                    break;
                case 2://bottom
                    point.Y = temp.Point.Y + 1;
                    point.X = temp.Point.X;
                    break;
                case 3://left
                    point.X = temp.Point.X - 1;
                    point.Y = temp.Point.Y;
                    break;
            }
            if (mg[(int)point.X, (int)point.Y] == 0) find = true;
        }
        //if find
        if (find)
        {
            temp.Direction = di;
            st.Push(new Block() { Point = point, Direction = -1 });
            //mark already visit
            mg[(int)point.X, (int)point.Y] = -1;
        }
        else
        {
            //not find
            mg[(int)temp.Point.X, (int)temp.Point.Y] = 0;
            st.Pop();
        }
        break;
    }
}

这个例子还是发上源码吧,做演示用

http://cid-e61fe49e5e4bce66.office.live.com/self.aspx/.Public/wpfdemo/maze1.rar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值