輸入地圖時(shí):1代表不可通行,0代表可通行,自動(dòng)增加邊框(墻壁),且左上角左邊一格為入口,右下角右邊一格為出口
成都創(chuàng)新互聯(lián):從2013年開始為各行業(yè)開拓出企業(yè)自己的“網(wǎng)站建設(shè)”服務(wù),為超過千家公司企業(yè)提供了專業(yè)的成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)和網(wǎng)站推廣服務(wù), 按需網(wǎng)站策劃由設(shè)計(jì)師親自精心設(shè)計(jì),設(shè)計(jì)的效果完全按照客戶的要求,并適當(dāng)?shù)奶岢龊侠淼慕ㄗh,擁有的視覺效果,策劃師分析客戶的同行競爭對手,根據(jù)客戶的實(shí)際情況給出合理的網(wǎng)站構(gòu)架,制作客戶同行業(yè)具有領(lǐng)先地位的。
編譯通過,代碼如下:
#includestdio.h
#includestdlib.h
#define N 50
int **maze;
int row;
int col;
int stack[50];//存放路徑的棧
void CreateMaze()//用于動(dòng)態(tài)創(chuàng)建迷宮
{
int i,j;
printf("請輸入迷宮的行數(shù):");
scanf("%d",row);
printf("請輸入迷宮的列數(shù):");
scanf("%d",col);
if(row=0||col=0)
{
printf("輸入的行數(shù)或列數(shù)不符合規(guī)則!\n");
exit(1);
}
//利用指針的指針動(dòng)態(tài)創(chuàng)建二維數(shù)組
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個(gè)數(shù):\n",i,j);
scanf("%d",maze[i][j]);
}
}
//輸入下一個(gè)當(dāng)前加邊墻的迷宮,以驗(yàn)證輸入是否正確
printf("輸入完畢!當(dāng)前加邊墻的迷宮為:\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");
}
}
//釋放迷宮數(shù)組
void DestroyMaze()
{
int i;
for(i=0;irow+2;i++)
free(maze[i]);
free(maze);
}
//用DFS方法來實(shí)現(xiàn)回溯,找到迷宮的一條解路徑
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("已經(jīng)找到了一條路徑,如下:\n");
ShowMaze();
}
else
{
printf("沒有合適的路徑走出當(dāng)前迷宮!\n");
}
DestroyMaze();
}
非常難。思路:
1、設(shè)老鼠的行進(jìn)路線都是優(yōu)先選擇下-右-上-左。
2、設(shè)老鼠很聰明,走過的路線走撒泡尿,表示鼠大爺?shù)酱艘挥?,我們可以把?shù)組的值改為3,表示走過,但走不通。
3、這是一個(gè)int[8][8]的二位數(shù)組,那么開始位置下標(biāo)是1,1,結(jié)束位置是6,6。行和列分別用、j表示。
4、實(shí)際路線我們可以設(shè)置2表示,我們可以使用遞歸,讓老鼠不斷測試路線。
5、最后打印數(shù)組,看老鼠的實(shí)際路線。
#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();
}
//****消息函數(shù)***********************************
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;
}
// ?可以運(yùn)行 ? 請采納
有不懂的可以聯(lián)系我
這個(gè)可是標(biāo)準(zhǔn)c++的 ? 這是結(jié)果
這是源代碼?
這是我之前課程設(shè)計(jì)做的一個(gè)迷宮,Swing做的,發(fā)在javaeye的博客上了,有打包成jar的,安裝了jdk可以直接雙擊運(yùn)行,有源碼,還有我寫的一個(gè)說明文檔,網(wǎng)址如下:
自己下載下吧,我就不給你發(fā)到郵箱了。
簡單點(diǎn)說叫做尋路,涉及到深度優(yōu)先搜索和廣度優(yōu)先搜索,當(dāng)然在存在類似“戰(zhàn)爭迷霧”效果的同時(shí)還要涉及到動(dòng)態(tài)規(guī)劃和回溯。
新聞標(biāo)題:老鼠走迷宮java代碼 java走迷宮代碼解析
路徑分享:http://www.rwnh.cn/article16/hiihgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、微信小程序、手機(jī)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)