Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Stack1 objstack1 = new Stack1();
            Stack2 objstack2 = new Stack2();
            int    input     = int.Parse(Console.ReadLine());

            for (int i = 0; i < input; i++)
            {
                string   val    = Console.ReadLine();
                string[] values = val.Split(' ');
                if (values[0] == "1")
                {
                    objstack1.push(int.Parse(values[1]));
                }
                else if (values[0] == "2")
                {
                    int chk;
                    if (objstack2.empty())
                    {
                        while ((chk = objstack1.pop()) != 0)
                        {
                            objstack2.push(chk);
                        }
                    }
                    objstack2.pop();
                    //int chk2;
                    //while ((chk2= objstack2.pop()) != 0)
                    //{

                    //    objstack1.push(chk2);
                    //}
                }
                else
                {
                    int chk3;
                    if (objstack2.empty())
                    {
                        while ((chk3 = objstack1.pop()) != 0)
                        {
                            objstack2.push(chk3);
                        }
                    }
                    Console.WriteLine(objstack2.print());
                    //int chk4;
                    //while ((chk4=objstack2.pop() )!= 0)
                    //{

                    //    objstack1.push(chk4);
                    //}
                }
            }
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void dfs(int x, int y, int step)
        {
            if (x == ex && y == ey)
            {
                if (step < min)
                {
                    min = step;
                    ShowPath();
                }
                return;
            }
            int tx, ty;

            for (int dir = 0; dir < 4; dir++)
            {
                tx = x + next[dir, 0];
                ty = y + next[dir, 1];

                if (tx < 0 || tx > map[0].Length - 1 || ty <0 || ty> map.Length - 1)
                {
                    continue;
                }
                if (map[ty][tx] == 0 && vis[ty, tx] == 0)
                {
                    vis[ty, tx] = 1;
                    stack.push(new note(tx, ty, stack.top, step + 1)); //加入堆疊
                    dfs(tx, ty, step + 1);
                    stack.pop();                                       // 搜索完畢,移出堆疊
                    vis[ty, tx] = 0;
                }
            }
            return;
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("DFS最短步數與路徑搜尋,使用堆疊");
            Console.WriteLine("輸入檔案名稱(跟執行檔不同資料夾請輸入絕對路徑)\nEx: maze.txt、maze1.txt、maze2.txt");
            path = Console.ReadLine();

            text = GetFileText(path);

            map = Get2DMap(text, " ", "", new string[] { "\r\n" });
            vis = new int[map.Length, map[0].Length];

            stack = new Stack2();

            Console.WriteLine($"路徑:{path}");
            Console.WriteLine($"讀入檔案\n{text}");
            Console.WriteLine("讀入陣列");
            for (int r = 0; r < map.Length; r++)
            {
                for (int c = 0; c < map[r].Length; c++)
                {
                    Console.Write(map[r][c]);
                }
                Console.Write("\n");
            }

            Console.WriteLine("\n輸入起始位置、終點位置(以空白分隔)EX:0 0 2 3");
            temp = Console.ReadLine().Split(' ');
            sx   = int.Parse(temp[0]); sy = int.Parse(temp[1]);
            ex   = int.Parse(temp[2]); ey = int.Parse(temp[3]);

            // 加入起始位置
            vis[sy, sx] = 1;
            stack.push(new note(sx, sy, stack.top, 0));
            dfs(sx, sy, 0);

            Console.WriteLine($"最短步數:{min}");

            Console.ReadKey();
        }