Ejemplo n.º 1
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.º 2
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();
        }