c# 解决面向对象八皇后问题的实例

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

namespace eightqueen
{
  
    class Queen
    {
        private int row;
        private int column;
        private Queen neightbor;
        //初始化
       public  Queen(int c, Queen n)
        {
            row =1;
            column = c;
            neightbor = n;
        }
        //找到解决方案
        public Boolean findSolution()
        {
            while (neightbor != null && neightbor.canAttack(row, column))
             if(!advance ())   return false;
            return true;
        }
        //前进到下一位置
        public Boolean advance()
        {
            if (row < 8)
            {
                row += 1;
                return this.findSolution();
            }
            if (neightbor != null && !neightbor.advance())
            {
                return false ;
            }
            row = 1;
            return this.findSolution();
        }
        //使用生产器,生成所有可能的部分解决方案和过滤掉无法实现最终目标的解决方案
        private Boolean canAttack(int testRow, int testColumn)
        {
            if (row == testRow) return true;
            //测试对角线
           
           int  columnDifference = testColumn - column;
            if ((row+columnDifference ==testRow ) || (row-columnDifference ==testRow ))
                return true ;
           
            return  neightbor != null &&  neightbor.canAttack(testRow, testColumn);
        }
        public void  print()
        {
            if (neightbor != null)
                neightbor.print();
          
            Getcolumnrow +=  "column:" + column.ToString () +";row:" + row.ToString () +"/n";
            }
        private static  string Getcolumnrow="";
       public   string getcolumnrow()
        {
           return Getcolumnrow ; }


    }

}
在窗体中建立一个textbox1

调用类

   private void Form1_Load(object sender, EventArgs e)
        {
            Queen lastqueen = null;

            for (int i = 1; i <= 8; i++)
            {

                lastqueen = new Queen(i, lastqueen);
                if (!lastqueen.findSolution())
                    MessageBox.Show("no solution/n");


            }
            lastqueen.print();
            textBox1.Text = lastqueen.getcolumnrow();

        }

C++面向对象编程八皇后问题 BOOL CMyqueenApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyqueenDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMyqueenView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; VERIFY( 1 == InitSkinMagicLib( AfxGetInstanceHandle(), "Demo" , NULL, NULL ) ); VERIFY( 1 == LoadSkinFromResource( AfxGetInstanceHandle() , "DEFAULT" ,"DEFAULT") ); VERIFY( 1 == SetWindowSkin( m_pMainWnd->m_hWnd , "MainFrame" )); VERIFY( 1 == SetDialogSkin( "Dialog" ) ); //((CMainFrame*)m_pMainWnd)->m_bSkinned = TRUE; //((CMainFrame*)m_pMainWnd)->m_nIndex = 0; m_pMainWnd->SetWindowText("八皇后问题演示"); // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值