用DFS解决八皇后问题(非递归)(c#)

需要注意的是,比如如下代码:

  Node tmp5 = new Node();
                        tmp5.col = node.col;
                        tmp5.row = node.row;

                        stackk.Push(tmp5);

为什么不是直接 stackk.Push(node);不是更方便吗
由于本人知识浅薄,还没悟出其中缘由,脑海里依稀认为 若是直接“=”赋值便是赋值而不是引用,node的变化应该不会改变stackk.Peek()的值才对,如果要修改应该要加入ref类似的标记,但是事实却是如果 node=stackk.Peek();修改 node的值之后,stackk.Peek()值也会发生改变,所以需要上面代码这么写才不会改变栈里面原来的值

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EightQueenNotRecursive
{
    class Program
    {
        int[] a = new int[8];
        int solution = 1;
       public class Node
        {
            public int row;
            public int col;
        }
        public bool isOK(Node node)
        {
            for (int i = 0; i <node.row; i++)
            {
                if (a[i]==node.col|| node.row - i == Math.Abs(a[i]-node.col))
                {
                    return false;
                }
            }
            return true;
        }
        public void Print()
        {
            Console.WriteLine("第{0}种解法:", solution++);
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (a[i]==j)
                    {
                        Console.Write("Q");
                    }else
                    {
                        Console.Write("#");
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("--------");
        }
        void DSF()
        {
            Node node=new Node();
            Stack stackk = new Stack();
         
            node.row = 0;
            node.col = 0;

            stackk.Push(node);

            while (stackk.Count !=0)
            {
                Node tmp = new Node();
                tmp= (Node)stackk.Peek();
                node.row = tmp.row;
                node.col = tmp.col;
                
       

                while (!isOK(node) && node.col < 8)
                {
                    node.col++;
                }
         
                if (node.col < 8)
                {
                    
                    //forward
                    if (node.row <7)
                    {
               
                        //把ok的节点放到当前层
                        a[node.row] = node.col;
                        stackk.Pop();
   
                        Node tmp4 = new Node();
                        tmp4.col = node.col;
                        tmp4.row = node.row;

                        stackk.Push(tmp4);

                        
                       
                        //进入下一层的第一个节点
                        node.row++;
             
                        node.col = 0;
                  

                        Node tmp3 = new Node();
                        tmp3.col = node.col;
                        tmp3.row = node.row;

                        stackk.Push(tmp3);
                       
                        



                        
                    }
                    else
                    {


                        a[node.row] = node.col;
                        //done
                       
                        Print();
                        node.col++;
                        stackk.Pop();

                       
                        Node tmp6 = new Node();
                        tmp6.col = node.col;
                        tmp6.row = node.row;

                        stackk.Push(tmp6);

                    }
                }
                else
                {

                  stackk.Pop();


                    if (stackk.Count == 0)
                    {

                        return;




                    }

                    Node tmp1 = new Node();
                        tmp1 = (Node)stackk.Peek();
                        node.row = tmp1.row;
                        node.col = tmp1.col;

                        node.col++;
                        stackk.Pop();


                        Node tmp5 = new Node();
                        tmp5.col = node.col;
                        tmp5.row = node.row;

                        stackk.Push(tmp5);



                    
                    //back
                   
                }
            }

        }
        static void Main(string[] args)
        {
            Program p = new Program();
            foreach(int i in p.a)
            {
                p.a[i] = 0;
            }
           

            p.DSF();
            Console.ReadKey();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容