水管道游戏

来源:互联网 发布:哈利波特知乎 编辑:程序博客网 时间:2024/04/27 16:33

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

 struct note    {        public int x;//横坐标        public int y;        public int s;    }    class Program    {        static int[,] book = new int[20, 20];        static int[,] a = new int[20, 20];        static int n = 5, m = 4, flag;        static note[] s = new note[2500];        static int top = 0;        static void Main(string[] args)        {            a[1, 1] = 5;            a[1, 2] = 3;            a[1, 3] = 5;            a[1, 4] = 3;            a[2, 1] = 1;            a[2, 2] = 5;            a[2, 3] = 3;            a[2, 4] = 0;            a[3, 1] = 2;            a[3, 2] = 3;            a[3, 3] = 5;            a[3, 4] = 1;            a[4, 1] = 6;            a[4, 2] = 1;            a[4, 3] = 1;            a[4, 4] = 5;            a[5, 1] = 1;            a[5, 2] = 5;            a[5, 3] = 5;            a[5, 4] = 4;            //打印构建的AA数组            for (int i = 1; i <= 5; i++)            {                for (int j = 1; j <= 4; j++)                {                    Console.Write(a[i, j]);                }                Console.WriteLine();            }            dfs(1, 1, 1);            if (flag == 0)            {                Console.WriteLine("没有找到管道");            }            else            {                Console.WriteLine("有找到管道");            }            Console.ReadKey();        }        public static void dfs(int x, int y, int front)        {            //判断是否到达终点,            if (x == n && y == m + 1)            {                flag = 1;                for (int i = 1; i <= top; i++)                {                    Console.Write("{" + s[i].x + "," + s[i].y + "}");                }                return;            }            //判断是否越界            if (x < 1 || x > n || y < 1 || y > m)            {                return;            }            //判断管道是否在路径中出现过啊            if (book[x, y] == 1)                return;            //标记当前位置已使用            book[x, y] = 1;            //将当前位置放入栈中            top++;            s[top].x = x;            s[top].y = y;            //当水管为直管的情况            if (a[x, y] >= 5 && a[x, y] <= 6)            {                if (front == 1)//进水口在左边的情况                {                    dfs(x, y + 1, 1);//只能使用5号这种摆放方式                }                if (front == 2)                {                    dfs(x + 1, y, 2);                }                if (front == 3)                {                    dfs(x, y - 1, 3);                }                if (front == 4)                {                    dfs(x - 1, y, 4);                }            }            //当水管为弯管的情况            if (a[x, y] >= 1 && a[x, y] <= 4)            {                if (front == 1)                {                    dfs(x + 1, y, 2);                    dfs(x - 1, y, 4);                }                if (front == 2)                {                    dfs(x, y + 1, 1);                    dfs(x, y - 1, 3);                }                if (front == 3)                {                    dfs(x - 1, y, 4);                    dfs(x + 1, y, 2);                }                if (front == 4)                {                    dfs(x, y + 1, 1);                    dfs(x, y - 1, 3);                }            }            book[x, y] = 0;            top--;            return;        }

运行结果

这里写图片描述

文章引之:《啊哈!算法》

0 0