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

📄 daam2_1dlg.cpp

📁 N-Queens made in SST
💻 CPP
字号:
// DAAM2_1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "DAAM2_1.h"
#include "DAAM2_1Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CDAAM2_1Dlg dialog

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

void CDAAM2_1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDAAM2_1Dlg)
	DDX_Control(pDX, IDC_LIST1, m_list1);
	DDX_Control(pDX, IDC_EDIT1, m_edit1);
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_grid1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDAAM2_1Dlg, CDialog)
	//{{AFX_MSG_MAP(CDAAM2_1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDAAM2_1Dlg message handlers

BOOL CDAAM2_1Dlg::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
	for(int i=0; i<8; i++)
	{
		m_grid1.SetColWidth(i,600);		
		m_grid1.SetRowHeight(i,600);

	}



	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDAAM2_1Dlg::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 CDAAM2_1Dlg::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 CDAAM2_1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDAAM2_1Dlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

class Queen
{
public:
	int **_data,_max;
	int _solution[1000][10][10];
	int _jumlahsolution;
	CMSFlexGrid * _Grid;

	Queen()
	{
		int i,j;
		_max=8;
		_data = new int * [_max];
		for(i=0; i<_max; i++)
		{
			_data[i] = new int[_max];
		}

		for(i=0; i<_max; i++)
		{
			for(j=0; j<_max; j++)
			{
				_data[i][j]=0;
			}
		}
		_jumlahsolution = 0;
	}
	void init()
	{
		

		for(int i=0; i<_max; i++)
		{
			for(int j=0; j<_max; j++)
			{
				_data[i][j]=0;
			}
		}
		_jumlahsolution = 0;
	}
	void save()
	{
		for(int j=0;j<_max;j++)
		{
			for(int i=0; i<_max;i++)
			{
				
				_solution[_jumlahsolution][j][i] = _data[j][i];
			}
		}
		_jumlahsolution++;
	}
	~Queen()
	{
		for(int i=0; i<_max;i++)
			delete[] _data[i];
		delete [] _data;
	}

	void setmax(int jum)
	{
		int i,j;
		for(i=0; i<_max;i++)
			delete[] _data[i];
		delete [] _data;		
		_max=jum;
		_data = new int * [_max];
		for(i=0; i<_max; i++)
		{
			_data[i] = new int[_max];
		}

		for(i=0; i<_max; i++)
		{
			for(j=0; j<_max; j++)
			{
				_data[i][j]=0;
			}
		}		
	}

	void cetak()
	{
	/*	CString x;
		for(int i=0; i<_max; i++)
		{
			for(int j=0; j<_max; j++)
			{
				x.Format("%d",_data[i][j]);
				_Grid->SetRow(i);
				_Grid->SetCol(j);
				_Grid->SetText(x);
			}
		}*/
		//AfxMessageBox("Pause");
	}

	bool allow(int x, int level) //x nandakan col, level nandakan row
	{
		int i,row,col;
		row = level-1;
		col = x;


		//cek column
		for(i=0; i<row; i++)
		{
			if(_data[i][col] == 1)
				return (false);
		}


		//cek diagonal 1 
		for(i=1; i<=col; i++)
		{
			if(col-i < 0 || row-i < 0)
				break;
			if(_data[row-i][col-i] == 1)
				return (false);			
		}

		//cek diagonal 2
		for(i=1; i<=_max-col; i++)
		{
			if(col+i > _max-1 || row-i < 0)
				break;
			if(_data[row-i][col+i] == 1)
				return (false);			
		}

		return (true);

	}

	int backtrack(int level)
	{
		if(level == _max+1) 
		{
		
				//Backtracking
				//return true;
				//SST
				save();
		}
		else
		{
			for(int x=0; x<_max; x++)
			{
				
				if(allow(x,level))
				{
					_data[level-1][x] = 1; //isi data
					cetak();
					if(backtrack(level+1))
					{
						return (true);
					}
					else
						_data[level-1][x] = 0; //hapus data jika tidak return true
				}
			}
		}
		return (false);
	}
};
Queen eq;

void CDAAM2_1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
	eq._Grid = (CMSFlexGrid *) GetDlgItem(IDC_MSFLEXGRID1);
	CString x;
	m_edit1.GetWindowText(x);
	eq.init();
	int i,j,n,k;
	n=0;
	for(i=x.GetLength()-1, j=0; i>=0; i--,j++)
	{
		n=n+ (x[i]-48) * int(pow(10,j));
	}
	eq.setmax(n);

	m_grid1.SetCols(n);
	m_grid1.SetRows(n);

	for(i=0; i<n; i++)
	{
		m_grid1.SetColWidth(i,6000/n);		
		m_grid1.SetRowHeight(i,6000/n);

	}
	if(eq.backtrack(1))
	{
		
		for(int i=0; i<eq._max; i++)
		{
			for(int j=0; j<eq._max; j++)
			{
				x.Format("%d",eq._data[i][j]);
				m_grid1.SetRow(i);
				m_grid1.SetCol(j);
				m_grid1.SetText(x);				
			}
		}
		AfxMessageBox("Done");
	}
	k=m_list1.GetCount();
	for(i = 0; i <k; i++)
	{
		m_list1.DeleteString(0);
	}
	for(i=0;i<eq._jumlahsolution;i++)
	{
		x.Format("%d",i);
		m_list1.AddString(x);
	}
}

void CDAAM2_1Dlg::OnSelchangeList1() 
{
	// TODO: Add your control notification handler code here
	int i,n,j;
	CString x;
	CString y;

	n=m_list1.GetCurSel();
	for(i=0; i<eq._max; i++)
	{
		for(j=0; j<eq._max; j++)
		{
			x.Format("%d",eq._solution[n][i][j]);
			m_grid1.SetTextMatrix(i,j,x);
		}
	}
}

⌨️ 快捷键说明

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