• 基于VC++2012图形化解决皇后问题


     


    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题。该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。 高斯认为有76种方案。1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果。计算机发明后,有多种方法可以解决此问题。

    我们用图形来解决之、

    看效果

    #pragma once
    #include<math.h>
    
    
    namespace Queen {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary>
    	/// Form1 摘要
    	///
    	/// 警告: 如果更改此类的名称,则需要更改
    	///          与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的
    	///          “资源文件名”属性。否则,
    	///          设计器将不能与此窗体的关联
    	///          本地化资源正确交互。
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: 在此处添加构造函数代码
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// 清理所有正在使用的资源。
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::ComboBox^  comboBox1;
    	private: System::Windows::Forms::TextBox^  textBox1;
    	private: System::Windows::Forms::Button^  button1;
    	private: System::Windows::Forms::Label^  label1;
    	private: System::Windows::Forms::Label^  label2;
    
    	private:
    		/// <summary>
    		/// 必需的设计器变量。
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// 设计器支持所需的方法 - 不要
    		/// 使用代码编辑器修改此方法的内容。
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
    			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->label1 = (gcnew System::Windows::Forms::Label());
    			this->label2 = (gcnew System::Windows::Forms::Label());
    			this->SuspendLayout();
    			// 
    			// comboBox1
    			// 
    			this->comboBox1->FormattingEnabled = true;
    			this->comboBox1->Location = System::Drawing::Point(134, 602);
    			this->comboBox1->Name = L"comboBox1";
    			this->comboBox1->Size = System::Drawing::Size(121, 20);
    			this->comboBox1->TabIndex = 0;
    			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
    			// 
    			// textBox1
    			// 
    			this->textBox1->Location = System::Drawing::Point(321, 602);
    			this->textBox1->Name = L"textBox1";
    			this->textBox1->Size = System::Drawing::Size(60, 21);
    			this->textBox1->TabIndex = 1;
    			this->textBox1->Text = L"6";
    			// 
    			// button1
    			// 
    			this->button1->BackColor = System::Drawing::SystemColors::Control;
    			this->button1->Location = System::Drawing::Point(396, 602);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(75, 20);
    			this->button1->TabIndex = 2;
    			this->button1->Text = L"确定";
    			this->button1->UseVisualStyleBackColor = false;
    			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// label1
    			// 
    			this->label1->AutoSize = true;
    			this->label1->Location = System::Drawing::Point(265, 607);
    			this->label1->Name = L"label1";
    			this->label1->Size = System::Drawing::Size(53, 12);
    			this->label1->TabIndex = 3;
    			this->label1->Text = L"输入N:";
    			// 
    			// label2
    			// 
    			this->label2->AutoSize = true;
    			this->label2->Location = System::Drawing::Point(91, 606);
    			this->label2->Name = L"label2";
    			this->label2->Size = System::Drawing::Size(41, 12);
    			this->label2->TabIndex = 3;
    			this->label2->Text = L"方案:";
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(592, 623);
    			this->Controls->Add(this->label2);
    			this->Controls->Add(this->label1);
    			this->Controls->Add(this->button1);
    			this->Controls->Add(this->textBox1);
    			this->Controls->Add(this->comboBox1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    		static int N=6,num=0;
    		static array<int>^ site=gcnew array<int>(20);
    		static array<int,2>^ result=gcnew array<int,2>(1000,1000);
    		static array<System::Windows::Forms::Button^,2>^ button=gcnew array<System::Windows::Forms::Button^,2>(100,100);
    		int Isable(int row)
    		{
    			for(int i=1;i<row;i++)
    				if((site[i]==site[row])||(abs(site[i]-site[row])==(row-i)))
    					return 0;
    			return 1;
    		}
    
    		void Queen(int row)
    		{ 
    			for(int cols=1;cols<=N;cols++)
    			{
    				site[row]=cols;
    				if(Isable(row))
    				{
    					if(row==N)   
    					{
    						num++; 
    						this->comboBox1->Items->Add("方案 "+num);
    						for(int i=1;i<=N;i++)
    							result[num,i]=site[i];
    						return;
    
    					}
    					Queen(row+1);					
    				}
    
    			}
    
    
    		}
    
    		void Erase()
    		{
    			for(int i=1;i<=N;i++)
    				for(int j=1;j<=N;j++)
    					this->Controls->Remove(this->button[i,j]);
    			this->comboBox1->Items->Clear();
    			N=Convert::ToInt16(this->textBox1->Text);
    			num=0;
    			for(int i=1;i<=N;i++)
    				site[i]=0;
    		}
    		void Show()
    		{
    			int a;
    			a=600/N;
    			for(int i=1;i<=N;i++)
    				for(int j=1;j<=N;j++)
    				{
    					this->button[i,j] = (gcnew System::Windows::Forms::Button());
    					this->button[i,j]->Location = System::Drawing::Point((i-1)*a, (j-1)*a);
    					this->button[i,j]->Name = L"button1";
    					this->button[i,j]->Size = System::Drawing::Size(a, a);
    					this->button[i,j]->TabIndex = 2;
    					this->button[i,j]->Text = L"确定";
    					this->button[i,j]->UseVisualStyleBackColor = true;
    					this->Controls->Add(this->button[i,j]);
    				}
    		}
    		void ShowQueen()
    		{
    			for(int i=1;i<=N;i++)
    				for(int j=1;j<=N;j++)
    					//this->button[i,j]->BackColor=System::Drawing::SystemColors::Control;
    					this->button[i,j]->UseVisualStyleBackColor = true;
    			for(int i=1;i<=N;i++)
    				for(int j=1;j<=N;j++)
    				{
    					if(result[this->comboBox1->SelectedIndex+1,i]==j)
    						this->button[i,j]->BackColor=System::Drawing::Color::Red;
    				}
    		}
    
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    				 Erase();
    				 Show();
    				 Queen(1);
    			 }
    	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
    				 ShowQueen();
    			 }
    	};
    }
    
    


     

    代码下载

    http://download.csdn.net/detail/yincheng01/4811529

  • 相关阅读:
    yii2 页面渲染方法解析
    JavaScript 编码小技巧
    Ansible Playbooks入门介绍
    CentOS 7 源码安装Ansible 2.x
    GitLab 安装与入门
    SpringBoot 悲观锁 与 乐观锁
    SpringBoot 事务隔离性和传播性
    SpringBoot 定义通过字段验证
    SpringBoot 密码MD5加密
    SpringBoot MockMVC
  • 原文地址:https://www.cnblogs.com/new0801/p/6177630.html
Copyright © 2020-2023  润新知