输入地图时:1代表不可通行,0代表可通行,自动增加边框(墙壁),且左上角左边一格为入口,右下角右边一格为出口
成都创新互联:从2013年开始为各行业开拓出企业自己的“网站建设”服务,为超过千家公司企业提供了专业的成都网站制作、做网站、外贸营销网站建设、网页设计和网站推广服务, 按需网站策划由设计师亲自精心设计,设计的效果完全按照客户的要求,并适当的提出合理的建议,拥有的视觉效果,策划师分析客户的同行竞争对手,根据客户的实际情况给出合理的网站构架,制作客户同行业具有领先地位的。
编译通过,代码如下:
#includestdio.h
#includestdlib.h
#define N 50
int **maze;
int row;
int col;
int stack[50];//存放路径的栈
void CreateMaze()//用于动态创建迷宫
{
int i,j;
printf("请输入迷宫的行数:");
scanf("%d",row);
printf("请输入迷宫的列数:");
scanf("%d",col);
if(row=0||col=0)
{
printf("输入的行数或列数不符合规则!\n");
exit(1);
}
//利用指针的指针动态创建二维数组
maze=(int **)malloc((row+2)*sizeof(int *));
for(i=0;irow+2;i++)
{
maze[i]=(int *)malloc((col+2)*sizeof(int));
}
//加边墙
for(i=0;irow+2;i++)
{
maze[i][0]=1;
maze[i][col+1]=1;
}
for(i=0;icol+2;i++)
{
if(i==1)
{
maze[0][i]=0;
}
else maze[0][i]=1;
if(i==col)
{
maze[row+1][col]=0;
}
else maze[row+1][i]=1;
}
for(i=1;i=row;i++)
{
for(j=1;j=col;j++)
{
printf("请输入第%d行的第%d个数:\n",i,j);
scanf("%d",maze[i][j]);
}
}
//输入下一个当前加边墙的迷宫,以验证输入是否正确
printf("输入完毕!当前加边墙的迷宫为:\n");
for(i=0;irow+2;i++)
{
for(j=0;jcol+2;j++)
{
printf("%d ",maze[i][j]);
}
printf("\n");
}
}
void ShowMaze()//输出迷宫
{
int i,j;
for(i=1;i=row;i++)
{
for(j=1;j=col;j++)
{
printf("%d ",maze[i][j]);
}
printf("\n");
}
}
//释放迷宫数组
void DestroyMaze()
{
int i;
for(i=0;irow+2;i++)
free(maze[i]);
free(maze);
}
//用DFS方法来实现回溯,找到迷宫的一条解路径
int FindPath()
{
int i,j,k,count,x,y,direction;
count=0;
x=1,y=1;
direction=0;
j=0,k=0;
for(i=0;iN;i++)
{
stack[i]=0;
}
i=0;
while(1)
{
count=0;//用count判断是否有路可走
{
if(x==1y==1)
maze[x][y]=2;
if(maze[x][y+1]==0)//东
{
count++;
maze[x][y+1]=2;
y=y+1;
stack[i]=-1;
i++;
if(x==rowy==col)
return 1;
}
else if(maze[x+1][y]==0)//南
{
if(maze[x+1][y]==0)
count++;
{
maze[x+1][y]=2;
x=x+1;
stack[i]=-2;
i++;
if(x==rowy==col)
return 1;
}
}
else if(maze[x][y-1]==0)//西
{
count++;
if(maze[x][y-1]==0)
{
maze[x][y-1]=2;
y=y-1;
stack[i]=-3;
i++;
if(x==rowy==col)
return 1;
}
}
else if(maze[x-1][y]==0)//北
{
count++;
if(maze[x-1][y]==0)
{
maze[x-1][y]=2;
x=x-1;
stack[i]=-4;
i++;
if(x==rowy==col)
return 1;
}
}
}
if(count==0)
{
if(i0)
return 0;
direction=stack[i--];
switch(direction)
{
case -1:y=y-1;break;
case -2:x=x-1;break;
case -3:y=y+1;break;
case -4:x=x+1;break;
default:break;
}
}
}
}
int main()
{
CreateMaze();
if(FindPath())
{
printf("已经找到了一条路径,如下:\n");
ShowMaze();
}
else
{
printf("没有合适的路径走出当前迷宫!\n");
}
DestroyMaze();
}
非常难。思路:
1、设老鼠的行进路线都是优先选择下-右-上-左。
2、设老鼠很聪明,走过的路线走撒泡尿,表示鼠大爷到此一游,我们可以把数组的值改为3,表示走过,但走不通。
3、这是一个int[8][8]的二位数组,那么开始位置下标是1,1,结束位置是6,6。行和列分别用、j表示。
4、实际路线我们可以设置2表示,我们可以使用递归,让老鼠不断测试路线。
5、最后打印数组,看老鼠的实际路线。
#include "stdafx.h"
#include stack
using namespace std;
const int rows = 8,cols = 8;
HINSTANCE hInst;
HBITMAP ball;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
char *str;
int nowPos,prePos;
bool find;
stackint path;
int mapIndex[rows*cols] = { 0,2,0,0,0,0,0,0, //材1#59049;
0,1,0,1,1,1,1,0, //材2#59049;
0,1,0,1,0,1,1,0, //材3#59049;
0,1,0,0,0,1,1,0, //材4#59049;
0,1,1,1,1,1,1,0, //材5#59049;
0,1,0,0,0,0,1,0, //材6#59049;
0,0,1,1,1,1,1,0, //材7#59049;
0,0,0,0,0,0,3,0 }; //材8#59049;
int record[rows*cols];
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while( msg.message!=WM_QUIT )
{
if( PeekMessage( msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( msg );
DispatchMessage( msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre = 100)
MyPaint(hdc);
}
}
return msg.wParam;
}
//****注册窗口*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;
return RegisterClassEx(wcex);
}
//****初始化*************************************
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
hInst = hInstance;
hWnd = CreateWindow("canvas", "迷宫" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,430,450,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc,cols*50,rows*50);
SelectObject(mdc,bmp);
HBITMAP tile;
int rowNum,colNum;
int i,x,y;
tile = (HBITMAP)LoadImage(NULL,"tile.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
ball = (HBITMAP)LoadImage(NULL,"ball.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
for (i=0;irows*cols;i++)
{
record[i] = mapIndex[i];
rowNum = i / cols;
colNum = i % cols;
x = colNum * 50;
y = rowNum * 50;
SelectObject(bufdc,tile);
if(!mapIndex[i])
BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);
else
{
if(mapIndex[i] == 2)
{
nowPos = i;
path.push(i);
record[i] = 0;
}
BitBlt(mdc,x,y,50,50,bufdc,0,0,WHITENESS);
}
}
prePos = cols * rows + 1;
MyPaint(hdc);
return TRUE;
}
//****核心代码*********************************
void MyPaint(HDC hdc)
{
int rowNum,colNum;
int x,y;
int up,down,left,right;
rowNum = prePos / cols;
colNum = prePos % cols;
x = colNum * 50;
y = rowNum * 50;
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,50,50,bufdc,0,0, WHITENESS);
rowNum = nowPos / cols;
colNum = nowPos % cols;
x = colNum * 50;
y = rowNum * 50;
SelectObject(bufdc,ball);
BitBlt(mdc,x,y,50,50,bufdc,0,0, SRCCOPY);
if(!find)
{
str = "迷宫入口";
up = nowPos - cols;
down = nowPos + cols;
left = nowPos - 1;
right = nowPos + 1;
if(up=0 record[up])
{
path.push(up);
record[up] = 0;
prePos = nowPos;
nowPos = up;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(down=cols*rows-1 record[down])
{
path.push(down);
record[down] = 0;
prePos = nowPos;
nowPos = down;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(left=rowNum*cols record[left])
{
path.push(left);
record[left] = 0;
prePos = nowPos;
nowPos = left;
if(mapIndex[nowPos] == 3)
find = true;
}
else if(right=(rowNum+1)*cols-1 record[right])
{
path.push(right);
record[right] = 0;
prePos = nowPos;
nowPos = right;
if(mapIndex[nowPos] == 3)
find = true;
}
else
{
if(path.size() = 1) //#59076;#59343;#58864;#58892;
str = "xxxxx";
else
{
path.pop();
prePos = nowPos;
nowPos = path.top();
}
}
}
else
{
str = "找到出口";
}
TextOut(mdc,0,0,str,strlen(str));
BitBlt(hdc,10,10,cols*50,rows*50,mdc,0,0,SRCCOPY);
tPre = GetTickCount();
}
//****消息函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
if(wParam==VK_ESCAPE)
PostQuitMessage(0);
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(ball);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 可以运行 请采纳
有不懂的可以联系我
这个可是标准c++的 这是结果
这是源代码
这是我之前课程设计做的一个迷宫,Swing做的,发在javaeye的博客上了,有打包成jar的,安装了jdk可以直接双击运行,有源码,还有我写的一个说明文档,网址如下:
自己下载下吧,我就不给你发到邮箱了。
简单点说叫做寻路,涉及到深度优先搜索和广度优先搜索,当然在存在类似“战争迷雾”效果的同时还要涉及到动态规划和回溯。