📄 mazesdlg.cpp
字号:
// mazesDlg.cpp : implementation file
//
//
#include "stdafx.h"
#include "mazes.h"
#include "mazesDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define XM 601
#define YM 401
#define XS 0
#define YS 201
#define XE 600
#define YE 201
#define RED RGB(255,0,0)
enum dirt{E,W,N,S};
int flags[XM][YM];
int mazelab[XM][YM];
bool search(int x,int y,int dir);
// int sum=0;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMazesDlg dialog
CMazesDlg::CMazesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMazesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMazesDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMazesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMazesDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMazesDlg, CDialog)
//{{AFX_MSG_MAP(CMazesDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_BN_CLICKED(IDC_BUTGET, OnButget)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMazesDlg message handlers
BOOL CMazesDlg::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
first=false;
success=false;
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMazesDlg::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 CMazesDlg::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
{if(!first){
// CPaintDC dc(this); // device context for painting
m_bitmap.LoadBitmap(IDB_BITMAP1);
memdc.CreateCompatibleDC(NULL);
memdc.SelectObject(&m_bitmap);
m_bitmap.GetObject(sizeof(bm),&bm);
CClientDC clientdc(this);
clientdc.BitBlt(10,10,bm.bmWidth+10,bm.bmHeight+10,&memdc,0,0,SRCCOPY);
hdc=memdc;
first=true;
}
CClientDC clientdc(this);
clientdc.BitBlt(10,10,bm.bmWidth+10,bm.bmHeight+10,&memdc,0,0,SRCCOPY);
if(success) setcolor(XS,YS);
CDialog::OnPaint();
}
}
//DEL void CMazesDlg::OnMouseMove(UINT nFlags, CPoint point)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL int red,green,blue;
//DEL CString ress;
//DEL rgbResult=GetPixel(hdc,point.x,point.y);
//DEL if(rgbResult==0) ress.Format("%s","Black");else ress.Format("%s","White");
//DEL red=GetRValue(rgbResult);
//DEL green=GetGValue(rgbResult);
//DEL blue=GetBValue(rgbResult);
//DEL m_pos.Format("x= %d ,y= %d,color =%s,red= %d,green= %d,blue= %d",point.x,point.y,ress,red,green,blue);
//DEL UpdateData(false);
//DEL
//DEL }
void CMazesDlg::OnButget()
{
success=false;
int x,y;
MessageBox("开始将迷宫转换为矩阵!","转化",MB_OK);
for(x=0;x<601;x++)
for(y=0;y<401;y++)
{if(GetPixel(hdc,x,y)==0)
mazelab[x][y]=1;
else
mazelab[x][y]=0;
flags[x][y]=0;
}
mazelab[0][200]=1,
mazelab[0][202]=1,
mazelab[600][200]=1,
mazelab[600][202]=1;
MessageBox("成功将迷宫转换为矩阵!","成功转化",MB_OK);
}
void CMazesDlg::OnOK()
{
// TODO: Add extra validation here
MessageBox("开始搜索迷宫路径!","提示",MB_OK);
mazelab[XS][YS]=-1;
//mazelab[XS][YS]=-3;
if(search(XS,YS,E))
{MessageBox("搜索迷宫路径成功,路径如红色所示!","成功提示",MB_OK);
CMazesDlg::setcolor(XS,YS);
success=true;
}
}
void CMazesDlg::setcolor(int x,int y)
{int dir;
CDC* cdc=GetDC();
CPen newPen;
newPen.CreatePen(PS_SOLID,1, RED);
CPen *oldPen = cdc->SelectObject(&newPen);
cdc->MoveTo(x+5,y+10);
while(x!=XE|| y!=YE){
if(mazelab[x][y]<0)
{
cdc->LineTo(x+10,y+10);
cdc->MoveTo(x+10,y+10);
dir=mazelab[x][y];
} //画迷宫路径
if(dir==-1) x++;
if(dir==-2) y++;
if(dir==-3) x--;
if(dir==-4) y--;
}
cdc->LineTo(XE+15,YE+10);
cdc->SelectObject(oldPen);
newPen.DeleteObject();
}
bool search(int x,int y,int dir)
{bool subway=false,noway=false,east=false,west=false,north=false,south=false;
while(!subway && !noway)
{
if(x==XE && y==YE) return true;
switch(dir){
case E: //go to east
x++;
if(x==XM) noway=true;
if(mazelab[x+1][y]){noway=true,east=false;} else east=true;
if(y<(YM-1) && !(mazelab[x][y+1])){south=true,subway=true;}
if(y>1 && !(mazelab[x][y-1])){north=true,subway=true;}
break;
case W: //go to west
x--;
if(x==-1) noway=true;
if(mazelab[x-1][y]){noway=true,west=false;} else west=true;
if(y<(YM-1) && !(mazelab[x][y+1])){south=true,subway=true;}
if(y>1 && !(mazelab[x][y-1])){north=true,subway=true;}
break;
case S: //go to south
y++;
if(y==YM) noway=true;
if(mazelab[x][y+1]){noway=true,south=false;} else south=true;
if(x<(XM-1) && !(mazelab[x+1][y])){east=true,subway=true;}
if(x>1 && !(mazelab[x-1][y])){west=true,subway=true;}
break;
case N: //go to north
y--;
if(y==-1) noway=true;
if(mazelab[x][y-1]){noway=true,north=false;} else north=true;
if(x<(XM-1) && !(mazelab[x+1][y])){east=true,subway=true;}
if(x>1 && !(mazelab[x-1][y])){west=true,subway=true;}
break;
}
}
if(x==XE && y==YE) return true; //reach exit
if(!subway && noway) return false ; // no way to go
if(!flags[x][y]) flags[x][y]=1; else return false ; //have come here
if(subway)
{
if(south) south=search(x,y,S);
if(east) east=search(x,y,E);
if(west) west=search(x,y,W);
if(north) north=search(x,y,N);
}
if(east) mazelab[x][y]=-1;
if(west) mazelab[x][y]=-3;
if(south) mazelab[x][y]=-2;
if(north) mazelab[x][y]=-4;
return (east||west||south||north) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -