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

📄 中国地图着色dlg.cpp

📁 一般回溯算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 中国地图着色Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "中国地图着色.h"
#include "中国地图着色Dlg.h"
#include "fstream.h"
#include <time.h>
ofstream fout("result.txt");

#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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_EDIT2, m_size);
	DDV_MinMaxUInt(pDX, m_size, 0, 1000);
	DDX_Text(pDX, IDC_EDIT4, m_MutateProbability);
	DDV_MinMaxDouble(pDX, m_MutateProbability, 0., 1.);
	DDX_Text(pDX, IDC_EDIT3, m_CrossProbability);
	DDV_MinMaxDouble(pDX, m_CrossProbability, 0., 1.);
	DDX_Text(pDX, IDC_EDIT5, m_GENERATION);
	DDV_MinMaxLong(pDX, m_GENERATION, 0, 100000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
//const CPoint center[32]={(371,168),(173,179),(255,293),(253,325),(322,413),(280,352),(178,324),
//							(94,297),(70,380),(122,362),(171,371),(225,382),(284,418),(289,443),
//							(313,458),(301,469),(341,523),(374,540),(407,551),(268,471),(233,434),
//							(177,438),(144,427),(74,438),(145,467),(209,475),(203,496),(193,526),
//							(179,514),(114,509),(31,403),(88,532)};
char *province[32]={"新疆","西藏","青海","甘肃","内蒙古","宁夏","四川","云南","广西","贵州",
		"重庆","陕西","山西","河北","北京","天津","辽宁","吉林","黑龙江","山东","河南","湖北",
		"湖南","广东","江西","安徽","江苏","上海","浙江","福建","海南","台湾"};
char *colorname[4]={"红色","绿色","蓝色","粉红色"};
int center_x[32]={371,173,255,253,322,280,178,94,70,122,171,225,284,289,313,301,341,374,
	407,268,233,177,144,74,145,209,203,193,179,114,31,88};
int center_y[32]={168,179,293,325,413,352,324,297,380,362,371,382,418,443,458,469,523,
	540,551,471,434,438,427,438,467,475,496,526,514,509,403,532};

int color[32]={0};
CString info;
char NeighbourMatrix[32][32]={							//为对称矩阵,这里只列出一半
							100,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,100,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,100,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,100,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,100,1,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,100,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,100,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,100,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,1,1,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,1,1,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,1,1,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,1,0,0,0,0,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,0,1,1,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,1,1,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,1,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,1,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,1,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,
							0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100};

BOOL CMyDlg::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<32;i++)			//填满邻居矩阵的另一半
		for(int j=i;j<32;j++)
			if(NeighbourMatrix[i][j]==1)
				NeighbourMatrix[j][i]=1;

	m_size=20;
	m_CrossProbability=0.8;
	m_MutateProbability=0.5;
	m_GENERATION=20000;
	UpdateData(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMyDlg::Open() 
{
	BYTE * pBuffer;

	CFile bmp("地图.bmp",CFile::modeRead);
	bmp.Seek(54 ,CFile::begin);
	pBuffer=new BYTE[624*512*3];
	bmp.Read(pBuffer,624*512*3);
	bmp.Close();

	CClientDC dc(this);
	int r,g,b;
	int i,j;
	for(i=0;i<512;i++)
	{
		for(j=0;j<624;j++)
		{
			b=*pBuffer;
		    g=*(pBuffer+1);
	    	r=*(pBuffer+2);
			pBuffer=pBuffer+3;

			if((r+g+b)/3>50)
				bmpTwo[i][j]=255;
			else
				bmpTwo[i][j]=0;

			dc.SetPixel(j+11,10+512-i,RGB(bmpTwo[i][j],bmpTwo[i][j],bmpTwo[i][j]));
			bmpTwo[i][j]=255-bmpTwo[i][j];
		}
	}
}

void CMyDlg::Thining()
{
	 int i=0,j;
	 int flag = 1;
	 BYTE **bmpThin;
	 bmpThin = new BYTE *[512];
	 for(i=0;i<512;i++)
		 bmpThin[i] = new BYTE[624];

     for(i=0;i<512;i++)
		 for(j=0;j<624;j++)//初始化
			 bmpThin[i][j]= 0;

     while(flag==1) //flag=0时迭代结束
	 {
		flag=0;
		for( i=2;i<512-2;i++)
		{
			for(j=2;j<624-2;j++)
			{				
				if(bmpTwo[i-1][j-1]==0xff	//模板a
                && bmpTwo[i-1][j+1]==0
                && bmpTwo[i][j-1]==0xff
                && bmpTwo[i][j-0]==0
                && bmpTwo[i][j+1]==0
                && bmpTwo[i][j+2]==0
                && bmpTwo[i+1][j-1]==0xff
                && bmpTwo[i+1][j+1]==0)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-1][j-0]==0		//模板b
                && bmpTwo[i-1][j+1]==0
                && bmpTwo[i][j-1]==0xff
                && bmpTwo[i][j-0]==0
                && bmpTwo[i][j+1]==0
                && bmpTwo[i][j+2]==0
                && bmpTwo[i+1][j-1]==0xff
                && bmpTwo[i+1][j-0]==0xff)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-1][j-1]==0xff	//模板c
                && bmpTwo[i-1][j-0]==0xff
                && bmpTwo[i][j-1]==0xff
                && bmpTwo[i][j-0]==0
                && bmpTwo[i][j+1]==0
                && bmpTwo[i][j+2]==0
                && bmpTwo[i+1][j-0]==0
                && bmpTwo[i+1][j+1]==0)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-1][j-1]==0xff	//模板d
                && bmpTwo[i-1][j-0]==0xff
                && bmpTwo[i-1][j+1]==0xff
                && bmpTwo[i][j-0]==0
                && bmpTwo[i+1][j-1]==0
                && bmpTwo[i+1][j-0]==0
                && bmpTwo[i+1][j+1]==0)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-1][j-1]==0		//模板e
				&& bmpTwo[i-1][j+1]==0xff
				&& bmpTwo[i][j-1]==0
				&& bmpTwo[i][j-0]==0
				&& bmpTwo[i][j+1]==0xff
				&& bmpTwo[i+1][j-1]==0
				&& bmpTwo[i+1][j+1]==0xff)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-1][j-0]==0		//模板f
                && bmpTwo[i][j-1]==0
                && bmpTwo[i][j-0]==0
                && bmpTwo[i][j+1]==0xff
                && bmpTwo[i+1][j-0]==0xff
                && bmpTwo[i+1][j+1]==0xff)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-1][j-0]==0xff	//模板g
                && bmpTwo[i-1][j+1]==0xff
                && bmpTwo[i][j-1]==0
                && bmpTwo[i][j-0]==0
                && bmpTwo[i][j+1]==0xff
                && bmpTwo[i+1][j-0]==0)
				{
					bmpThin[i][j]=0xff;
                    flag=1;
                    continue;
				}
                        
                if(bmpTwo[i-2][j-0]==0		//模板h
                && bmpTwo[i-1][j-1]==0
                && bmpTwo[i-1][j-0]==0
                && bmpTwo[i-1][j+1]==0
                && bmpTwo[i][j-0]==0
                && bmpTwo[i+1][j-1]==0xff
                && bmpTwo[i+1][j-0]==0xff
                && bmpTwo[i+1][j+1]==0xff)
				{
					bmpThin[i][j]=0xff;
					flag=1;
					continue;
				}
			}
		}
        for(i=0;i<512;i++)
		{
			for(j=0;j<624;j++)
                if(bmpThin[i][j]==0xff)
					bmpTwo[i][j]=0xff;
        }
	}

    for( i=2;i<512-2;i++)		 //第二次串行细化
    {
        for(j=2;j<624-2;j++)
        {
            //缩小后的模板a
            if(bmpTwo[i-1][j-1]==0xff
            && bmpTwo[i-1][j+1]==0
            && bmpTwo[i][j-1]==0xff
            && bmpTwo[i][j-0]==0
            && bmpTwo[i][j+1]==0
            && bmpTwo[i+1][j-1]==0xff
            && bmpTwo[i+1][j+1]==0)
            {
                    bmpTwo[i][j]=0xff;
                    flag=1;
                    continue;
            }
            //缩小后的模板h
            if(bmpTwo[i-1][j-1]==0
            && bmpTwo[i-1][j-0]==0
            && bmpTwo[i-1][j+1]==0
            && bmpTwo[i][j-0]==0
            && bmpTwo[i+1][j-1]==0xff
            && bmpTwo[i+1][j-0]==0xff
            && bmpTwo[i+1][j+1]==0xff)
            {
                    bmpTwo[i][+j]=0xff;
                    flag=1;
                    continue;
            }
		}
    }		
	delete []bmpThin;
	CClientDC dc(this);

//	for(i=0;i<512;i++)
	for(i=511;i>=0;i--)
	{
		for(j=0;j<624;j++)
		{
			if(bmpTwo[i][j]==0xff)
				dc.SetPixel(j+11,512+10-i,RGB(255,255,255));
			else
				dc.SetPixel(j+11,512+10-i,RGB(0,0,0));
		}
	}
}

void CMyDlg::RegionGrow(BYTE *bwImage,int l)
{

	static int nDx[]={-1,0,1,0};
	static int nDy[]={ 0,1,0,-1};

	// 定义堆栈的起点和终点
	// 当nStart=nEnd, 表示堆栈中只有一个点
	long int nStart=0 ;
	long int nEnd=0   ;
//	p_bw=new BYTE(512*624);


	int *GrowRegionx = new int [512*624];
	int *GrowRegiony = new int [512*624];

	// 当前正在处理的象素
	int nCurrx ;
	int nCurry ;

	// 循环控制变量
	int k ;

	// 图象的横纵坐标,用来对当前象素的4邻域进行遍历
	int xx;
	int yy;

	CClientDC dc(this);

	memset(p_bw,0,sizeof(BYTE)*319488);	
	nStart=0;
	nEnd=0;
	Sleep(500);
	// 把种子点的坐标压入栈
	GrowRegionx[nEnd] = center_y[l];
	GrowRegiony[nEnd] = center_x[l];
	while (nStart<=nEnd)
	{
		// 当前种子点的坐标
		nCurrx = GrowRegionx[nStart];
		nCurry = GrowRegiony[nStart];

		// 对当前点的4邻域进行遍历
		for (k=0; k<4; k++)	
		{	
			// 4邻域象素的坐标
			xx = nCurrx+nDx[k];
			yy = nCurry+nDy[k];
			
			// 判断象素(xx,yy) 是否在图像内部
			if (	(xx < 624) && (xx>=0) && (yy<512) && (yy>=0) 
					 &&p_bw[yy*624+xx]==0
					 &&bwImage[yy*624+xx] ==255)
			{ 
				// 堆栈的尾部指针后移一位
				 nEnd++;

				// 象素(xx,yy) 压入栈
				GrowRegionx[nEnd] = xx;		

⌨️ 快捷键说明

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