⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queendlg.cpp

📁 N皇后问题!图形界面演示
💻 CPP
字号:
// QueenDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Queen.h"
#include "QueenDlg.h"

#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueenDlg dialog

CQueenDlg::CQueenDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQueenDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQueenDlg)
	N = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQueenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQueenDlg)
	DDX_Text(pDX, IDC_EDIT1, N);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQueenDlg, CDialog)
	//{{AFX_MSG_MAP(CQueenDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnBegin)
	ON_BN_CLICKED(IDC_BUTTON3, OnDrawnChessBoard)
	ON_BN_CLICKED(IDC_BUTTON4, OnCancel)
	ON_BN_CLICKED(IDC_BUTTON2, OnAgain)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueenDlg message handlers

BOOL CQueenDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	sum=0;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQueenDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CQueenDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CQueenDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CQueenDlg::OnBegin() 
{
	//int *p=new int [N+1];
	UpdateData(TRUE);
	for(int i=0;i<=N;i++)
		x[i]=0;
	//x=p;
	Backtrack(1);//回朔
	//填充皇后
	CRect Chess;
	for(i=1;i<=N;i++)
	{
		Chess.left=140+(y[i]-1)*ChessNumber;
		Chess.right=Chess.left+ChessNumber;
		Chess.top=20+(i-1)*ChessNumber;
		Chess.bottom=Chess.top+ChessNumber;
		CClientDC dc(this);
		CBrush BackGround(RGB(0,0,0));
		dc.SelectObject(&BackGround);
		dc.FillRect(Chess,&BackGround);
	}
	//delete [] p;

}

bool CQueenDlg::Place(int k)//用来测试若将皇后k放在x[k]列是否与前面已放置的k-1个皇后都不在同一列,且不在同一直线上
{
	for(int j=1;j<k;j++)
		if((abs(k-j)==abs(x[j]-x[k]))||(x[j]==x[k])) return false;
	return true;

}

void CQueenDlg::Backtrack(int t)//递归回朔搜索
{
	if(t>N)
	{
		sum++;
	    //int x1,y1,x2,y2;
		for(int i=1;i<=N;i++) y[i]=x[i];
/*		CRect Chess;
		
		for(int i=1;i<=N;i++)
		{
			Chess.left=140+(x[i]-1)*ChessNumber;
			Chess.right=Chess.left+ChessNumber;
			Chess.top=20+(i-1)*ChessNumber;
			Chess.bottom=Chess.top+ChessNumber;
			CClientDC dc(this);
			CBrush BackGround(RGB(0,0,0));
			dc.SelectObject(&BackGround);
			dc.FillRect(Chess,&BackGround);
		}*/
        /*for(int i=1;i<=N;i++)
		{
			x1=140+(x[i]-1)*ChessNumber;
			y1=20+(i-1)*ChessNumber;
			x2=140+x[i]*ChessNumber;
			y2=20+i*ChessNumber;
			CPaintDC dc(this);
			CPen pen;
			pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
			dc.Ellipse(x1,y1,x2,y2);
		}*/
		//DrawChess();
		return;
	}
	else
	{	for(int i=1;i<=N;i++)
		{
			x[t]=i;
			if(Place(t)) Backtrack(t+1);
		}
	}
	
}
/*void CQueenDlg::Backtrack(void)//迭代回朔
{
	x[1]=0;
	int k=1;
    while(k>0)
	{
		x[k]+=1;
		while((x[k]<=N)&&!(Place(k))) x[k]+=1;
		if(x[k]<=N)
			if(k==N) sum++;
			else 
			{
				k++;
				x[k]=0;
			}
		else k--;
	}
	
}*/

void CQueenDlg::OnDrawnChessBoard() //绘制棋盘
{
	UpdateData(TRUE);

    if (N>=3&&N<=32)
		{
			//背景布色
			CRect Plate;
			Plate.left=140;
			Plate.bottom=500;
			Plate.top=20;
			Plate.right=620;
			CClientDC dcc(this);
			CBrush BackGround(RGB(255,255,255));
			dcc.SelectObject(&BackGround);
			dcc.FillRect(Plate,&BackGround);

		    int a=N;
			CClientDC dc(this);
			CPoint StartPoint,EndPoint;
			StartPoint.x=140;//起始点
			StartPoint.y=20;
			ChessNumber=(long)480/a;
			for(int i=0;i<=a;i++)
				{
					dc.MoveTo(StartPoint); 
					EndPoint.x=StartPoint.x;//终点
					EndPoint.y=500;
					dc.LineTo(EndPoint);
					StartPoint.x+=ChessNumber;
				}
			StartPoint.x=140;
			StartPoint.y=20;
			for(i=0;i<=a;i++)
				{
					dc.MoveTo(StartPoint); 
					EndPoint.y=StartPoint.y;
					EndPoint.x=620;
					dc.LineTo(EndPoint);
					StartPoint.y+=ChessNumber;
				}
			
		}
		else if(N>32)
			{
				MessageBox("为演示方便,请您输入一小于64的数!","输入错误");
				UpdateData(FALSE);
			}
		else
			{
				MessageBox("您输入的数必须大于3,否则无解!","输入错误");
				UpdateData(FALSE);
			}
	
}

/*void CQueenDlg::DrawChess()//填充皇后
{
	int x1,y1,x2,y2;

    for(int i=1;i<=N;i++)
	{
		x1=140+(x[i]-1)*ChessNumber;
		y1=20+(i-1)*ChessNumber;
		x2=140+x[i]*ChessNumber;
		y2=20+i*ChessNumber;
		CPaintDC dc(this);
	    CPen pen;
	    pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
		dc.Ellipse(x1,y1,x2,y2);
	}
	return;
}*/

void CQueenDlg::OnCancle() 
{
	OnCancel();	
}

void CQueenDlg::OnAgain() 
{
	CDialog::Invalidate(FALSE);
    OnInitDialog();	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -