① 誰有javafx寫的電腦鼠迷宮的代碼,迷宮文件讀入後是,8*8的二位數組的字
通過坐標來判斷 比方說 4X4迷宮的終點坐標為 (3,3)(3,4)(4,3)(4,4)而16X16的終點坐標為(7,7)(7,8)(8,7)(8,8)
② 求C++老鼠走迷宮源代碼
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define stack_init_size 200
#define stack_increment 10
#define OVERFLOW 0
#define OK 1
#define ERROE 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct{
int x;
int y;
}PosType;
typedef struct {
int ord; // 通道塊在路徑上的"序號"
PosType seat; //通道塊在迷宮中的"坐標位置"
int di; //從此通道塊走向下一通道塊的"方向"
}SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int mg[20][20];
/*隨機生成迷宮的函數
/*為了能夠讓盡量能通過,將能通過的塊和不能通過的塊數量比大致為2:1*/
void Random(){
int i,j,k;
srand(time(NULL));
mg[1][0]=mg[1][1]=mg[18][19]=0; //將入口、出口設置為"0"即可通過
for(j=0;j<20;j++)
mg[0][j]=mg[19][j]=1; /*設置迷宮外圍"不可走",保證只有一個出口和入口*/
for(i=2;i<19;i++)
mg[i][0]=mg[i-1][19]=1; /*設置迷宮外圍"不可走",保證只有一個出口和入口*/
for(i=1;i<19;i++)
for(j=1;j<19;j++){
k=rand()%3; //隨機生成0、1、2三個數
if(k)
mg[i][j]=0;
else{
if((i==1&&j==1)||(i==18&&j==18)) /*因為距入口或出口一步的路是必經之路,故設該通道塊為"0"加大迷宮能通行的概率*/
mg[i][j]=0;
else
mg[i][j]=1;
}
}
}
//構造一個空棧
Status InitStack(SqStack &s){
s.base =(SElemType *)malloc(stack_init_size * sizeof(SElemType));
if(!s.base) return OVERFLOW;
s.top=s.base;
s.stacksize=stack_init_size;
return OK;
}
//當前塊可否通過
Status Pass(PosType e){
if (mg[e.x][e.y]==0) //0時可以通過
return OK; // 如果當前位置是可以通過,返回1
return OVERFLOW; // 其它情況返回0
}
//留下通過的足跡
Status FootPrint(PosType e){
mg[e.x][e.y]=7;
return OK;
}
//壓入棧
Status Push(SqStack &s,SElemType e){
if(s.top-s.base>=s.stacksize){
s.base=(SElemType *)realloc(s.base,(s.stacksize+stack_increment) *sizeof(SElemType));
if(!s.base)exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=stack_increment;
}
*s.top++=e;
return OK;
}
//出棧
Status Pop(SqStack &s,SElemType &e){
if(s.top==s.base)
return ERROE;
e=*--s.top;
return OK;
}
//下一步
PosType NextPos(PosType &e,int dir){
PosType E;
switch(dir){
case 1:E.x=e.x; //向下
E.y=e.y+1;
break;
case 2:E.x=e.x+1; //向右
E.y=e.y;
break;
case 3:E.x=e.x; //向上
E.y=e.y-1;
break;
case 4:E.x=e.x-1; //向左
E.y=e.y;
break;
}
return E;
}
//是否空棧
Status StackEmpty(SqStack s){
if (s.top==s.base)
return OK;
return OVERFLOW;
}
//留下不能通過的足跡
Status MarkPrint(PosType e){
mg[e.x][e.y]=3;
return OK;
}
//迷宮函數
// 若迷宮maze中從入口 start到出口 end的通道,則求得一條存放在棧中
// (從棧底到棧頂),並返回TRUE;否則返回FALSE
Status MazePath(int mg,PosType start,PosType end,SqStack &s){
PosType curpos;
InitStack(s);
SElemType e;
int curstep;
curpos=start; // 設定"當前位置"為"入口位置"
curstep=1; // 探索第一步
do{
if(Pass(curpos)){ // 當前位置可通過,即是未曾走到過的通道塊
FootPrint(curpos); // 留下足跡
e.di =1;
e.ord = curstep;
e.seat= curpos;
Push(s,e); // 加入路徑
if(curpos.x==end.x&&curpos.y==end.y){
printf("\n\n0∩_∩0 能到達終點!");
return TRUE;
}
curpos=NextPos(curpos,1); // 下一位置是當前位置的東鄰
curstep++; // 探索下一步
}
else{ // 當前位置不能通過
if(!StackEmpty(s)){
Pop(s,e);
while(e.di==4&&!StackEmpty(s)){
MarkPrint(e.seat);
Pop(s,e);
}
if(e.di<4){
e.di++;
Push(s,e); // 留下不能通過的標記,並退回一步
curpos=NextPos(e.seat,e.di); /* 當前位置設為新方向的相鄰塊*/
}//if
}//if
}//else
}while(!StackEmpty(s));
printf("\n\n囧 ! 不能到達終點!");
return FALSE;
}
//列印迷宮
void PrintMaze(){
int i,j;
printf("運行路徑:\n\n");
for(i=0;i<20;i++){
for(j=0;j<20;j++){
if(mg[i][j]==0)printf(" ");
else if(mg[i][j]==1) printf("■"); //迷宮的"牆"
else if(mg[i][j]==3) printf("◇"); //不通的路
else if(mg[i][j]==7)printf("○"); //通過的路徑
}
printf("\n");
}
printf("\n");
}
void main(){
SqStack S;
PosType start,end;
start.x=1;start.y=0; //起點坐標
end.x=18;end.y=19; //終點坐標
printf("\n==================迷宮游戲==================");
printf("\n說明:■不能走的區域\t◇走不通的區域");
printf("\n '空格'代表未到過的區域");
printf("\n ○代表能通過的路徑,指向終點");
printf("\n============================================");
Random();
printf("\n\nTest 1:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
system("pause");
Random();
printf("\nTest 2:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
system("pause");
Random();
printf("\nTest 3:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
printf("\n==========程序退出,感謝使用!==========\n");
}
③ VC6.0編一個走迷宮的程序
你需要的是迷宮的演算法?用STACK來實現,
用些邏輯判斷,可以再每個塊上標記是否可以通過還是不能通過,
讓後初始化人物在某一個位置,讓後讓他像左邊開始走,然後右邊。。
就是窮舉每個路徑,然後可以通過就放到STACK中,在放的時候判斷,
時候路徑在STACK已經存在,避免不是最短路徑,和死循環!
只需要用個遞歸來做深度遍歷就行了!其實也就是圖論的問題!
④ 跪求老鼠走迷宮游戲,必須用C++編寫,用棧來實現,因為是數據結構課程設計所以只要現成代碼,越快越好。
#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;
stack<int> path;
int mapIndex[rows*cols] = { 0,2,0,0,0,0,0,0, //材1
0,1,0,1,1,1,1,0, //材2
0,1,0,1,0,1,1,0, //材3
0,1,0,0,0,1,1,0, //材4
0,1,1,1,1,1,1,0, //材5
0,1,0,0,0,0,1,0, //材6
0,0,1,1,1,1,1,0, //材7
0,0,0,0,0,0,3,0 }; //材8
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;i<rows*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) //
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;
}
⑤ 程序設計 老鼠走迷宮
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*迷宮的數組*/
int maze[100][100];
/*迷宮的行數和列數*/
int m=7,n=7;
/*
*對迷宮進行初始化,用隨機數產生迷宮
*/
void InitMaze()
{
int i,j,temp;
srand((unsigned)time(NULL));
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
temp=rand()%100;
if(temp>30)
{
maze[i-1][j-1]=0;
}else
{
maze[i-1][j-1]=1;
}
}
maze[0][0]=0;
maze[m-1][n-1]=9;
}
/*
*定義棧和棧的節點
*/
typedef struct Node
{
int x;
int y;
struct Node *next;
}Node,*Stack;
/*
*初始化Stack
*/
void InitStack(Node *Stack)
{
Stack=(Node *)malloc(sizeof(Node));
if(Stack==NULL)
{
printf("分配空間失敗 ");
exit(0);
}else
{
Stack->next=NULL;
}
}
/*
*壓棧
*/
void push(Node *Stack,int x,int y)
{
Node *temp;
temp=(Node *)malloc(sizeof(Node));
if (!temp)
{
printf("分配內存空間錯誤");
return;
}
else
{
temp->x=x;
temp->y=y;
temp->next=Stack->next;
Stack->next=temp;
}
}
/*
*出棧
*/
void pop(Node *Stack,int *x,int *y)
{
Node *temp;
temp=Stack->next;
if(!temp){
return;
}else{
*x=temp->x;
*y=temp->y;
}
Stack->next=temp->next;
free(temp);
}
/*
*判斷棧是否為空
*/
int isEmpty(Node *Stasck)
{
return ((Stasck->next)==NULL);
}
/*
*判斷從該點時候可以向其他方向移動,並返回移動的方向
*/
int pass(int i,int j)
{
/*右方向*/
if(j<n-1&&(maze[i][j+1]==0||maze[i][j+1]==9))
{
return 2;
}
/*下方向*/
if(i<m-1&&(maze[i+1][j]==0||maze[i+1][j]==9))
{
return 3;
}
/*左方向*/
if(j>=1&&(maze[i][j-1]==0||maze[i][j-1]==9))
{
return 4;
}
/*上方向*/
if(i>=1&&(maze[i-1][j]==0||maze[i-1][j]==9))
{
return 5;
}
return -1;
}
/*
*對迷宮進行列印
*/
void printMaze()
{
int i=0,j=0;
printf(" ");
for(i=0;i<n;i++)
{
if(i+1>9)
printf("%d ",i+1);
else
printf(" %d",i+1);
}
printf(" ");
for(i=0;i<m;i++){
if(i+1>9)
printf("%d",i+1);
else
printf(" %d",i+1);
for(j=0;j<n;j++)
{
if(maze[i][j]==0||maze[i][j]==9||maze[i][j]==-1)
{
printf("a ");
}
else if(maze[i][j]==1)
{
printf("b ");
}else
if(maze[i][j]==2)
{
printf("D ");
}else if(maze[i][j]==3)
{
printf("X ");
}else if(maze[i][j]==4)
{
printf("A ");
}else if(maze[i][j]==5)
{
printf("W ");
}
}
printf(" ");
}
}
/*
*對迷宮進行路徑搜索
*數組的數字有以下含義
*0.該點沒有被探索過,且可行
*1.該點不可行
*2.該點是可行的,且進行了向東的探索
*3.該點是可行的,且進行了向南的探索
*4.該點是可行的,且進行了向西的探索
*5.該點是可行的,且進行了向北的探索
*6.該點是入口
*9.該點是出口
*-1.該點已經遍歷完畢四個方向,不能找到有效的路徑,則置為-1
*/
void FindPath()
{
int curx=0,cury=0;
int count=0;
int flag=0;
Node *Stacks=NULL;
InitStack(Stacks);
do{
if(maze[curx][cury]==9)
{
flag=1;
}
switch(pass(curx,cury)){
case 2:
maze[curx][cury]=2;
push(Stacks,curx,cury);
cury++;
break;
case 3:
maze[curx][cury]=3;
push(Stacks,curx,cury);
curx++;
break;
case 4:
maze[curx][cury]=4;
push(Stacks,curx,cury);
cury--;
break;
case 5:
maze[curx][cury]=5;
push(Stacks,curx,cury);
curx--;
break;
case -1:
maze[curx][cury]=-1;
if(!isEmpty(Stacks))
pop(Stacks,&curx,&cury);
break;
}
count++;
}while(!isEmpty(Stacks)&&flag==0);
if(flag==1)
{
printf("該迷宮的行走路徑如下: ");
printMaze();
}else
{
printf(" Sorry,該迷宮無解 ");
}
}
/*
*主函數
*要求輸入m,n的值,要符合要求
*程序列印出生成的迷宮,然後進行尋找路徑
*如果可以找到出口,則畫出該路徑
*如果該迷宮沒有出口,則提示迷宮無解
*/
int main()
{
/*printf("請輸入迷宮的行數m(大於0,小於100):");
scanf("%d",&m);
printf("請輸入迷宮的列數n(大於0,小於100):");
scanf("%d",&n);
if(m<0||m>100||n<0||n>100){
printf("輸入數據錯誤,程序退出 ");
exit(-1);
}*/
InitMaze();
printf("原迷宮為: ");
printMaze();
FindPath();
getch();
return 0;
}
⑥ 走迷宮的C語言版代碼求助 程序開始運行時顯示一個迷宮地圖,迷宮中央有一隻老鼠,迷宮的右下方有一個糧
/*註:本程序探索迷宮的優先順序=> 1-下、2-右、3-上、4-左 <=總體趨勢:下右,逆時針方向。因為出口就在右邊下方*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define stack_init_size 200
#define stack_increment 10
#define OVERFLOW 0
#define OK 1
#define ERROE 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct{
int x;
int y;
}PosType;
typedef struct {
int ord; // 通道塊在路徑上的"序號"
PosType seat; //通道塊在迷宮中的"坐標位置"
int di; //從此通道塊走向下一通道塊的"方向"
}SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int mg[20][20];
/*隨機生成迷宮的函數
/*為了能夠讓盡量能通過,將能通過的塊和不能通過的塊數量比大致為2:1*/
void Random(){
int i,j,k;
srand(time(NULL));
mg[1][0]=mg[1][1]=mg[18][19]=0; //將入口、出口設置為"0"即可通過
for(j=0;j<20;j++)
mg[0][j]=mg[19][j]=1; /*設置迷宮外圍"不可走",保證只有一個出口和入口*/
for(i=2;i<19;i++)
mg[i][0]=mg[i-1][19]=1; /*設置迷宮外圍"不可走",保證只有一個出口和入口*/
for(i=1;i<19;i++)
for(j=1;j<19;j++){
k=rand()%3; //隨機生成0、1、2三個數
if(k)
mg[i][j]=0;
else{
if((i==1&&j==1)||(i==18&&j==18)) /*因為距入口或出口一步的路是必經之路,故設該通道塊為"0"加大迷宮能通行的概率*/
mg[i][j]=0;
else
mg[i][j]=1;
}
}
}
//構造一個空棧
Status InitStack(SqStack &s){
s.base =(SElemType *)malloc(stack_init_size * sizeof(SElemType));
if(!s.base) return OVERFLOW;
s.top=s.base;
s.stacksize=stack_init_size;
return OK;
}
//當前塊可否通過
Status Pass(PosType e){
if (mg[e.x][e.y]==0) //0時可以通過
return OK; // 如果當前位置是可以通過,返回1
return OVERFLOW; // 其它情況返回0
}
//留下通過的足跡
Status FootPrint(PosType e){
mg[e.x][e.y]=7;
return OK;
}
//壓入棧
Status Push(SqStack &s,SElemType e){
if(s.top-s.base>=s.stacksize){
s.base=(SElemType *)realloc(s.base,(s.stacksize+stack_increment) *sizeof(SElemType));
if(!s.base)exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=stack_increment;
}
*s.top++=e;
return OK;
}
//出棧
Status Pop(SqStack &s,SElemType &e){
if(s.top==s.base)
return ERROE;
e=*--s.top;
return OK;
}
//下一步
PosType NextPos(PosType &e,int dir){
PosType E;
switch(dir){
case 1:E.x=e.x; //向下
E.y=e.y+1;
break;
case 2:E.x=e.x+1; //向右
E.y=e.y;
break;
case 3:E.x=e.x; //向上
E.y=e.y-1;
break;
case 4:E.x=e.x-1; //向左
E.y=e.y;
break;
}
return E;
}
//是否空棧
Status StackEmpty(SqStack s){
if (s.top==s.base)
return OK;
return OVERFLOW;
}
//留下不能通過的足跡
Status MarkPrint(PosType e){
mg[e.x][e.y]=3;
return OK;
}
//迷宮函數
// 若迷宮maze中從入口 start到出口 end的通道,則求得一條存放在棧中
// (從棧底到棧頂),並返回TRUE;否則返回FALSE
Status MazePath(int mg,PosType start,PosType end,SqStack &s){
PosType curpos;
InitStack(s);
SElemType e;
int curstep;
curpos=start; // 設定"當前位置"為"入口位置"
curstep=1; // 探索第一步
do{
if(Pass(curpos)){ // 當前位置可通過,即是未曾走到過的通道塊
FootPrint(curpos); // 留下足跡
e.di =1;
e.ord = curstep;
e.seat= curpos;
Push(s,e); // 加入路徑
if(curpos.x==end.x&&curpos.y==end.y){
printf("\n\n0∩_∩0 能到達終點!");
return TRUE;
}
curpos=NextPos(curpos,1); // 下一位置是當前位置的東鄰
curstep++; // 探索下一步
}
else{ // 當前位置不能通過
if(!StackEmpty(s)){
Pop(s,e);
while(e.di==4&&!StackEmpty(s)){
MarkPrint(e.seat);
Pop(s,e);
}
if(e.di<4){
e.di++;
Push(s,e); // 留下不能通過的標記,並退回一步
curpos=NextPos(e.seat,e.di); /* 當前位置設為新方向的相鄰塊*/
}//if
}//if
}//else
}while(!StackEmpty(s));
printf("\n\n囧 ! 不能到達終點!");
return FALSE;
}
//列印迷宮
void PrintMaze(){
int i,j;
printf("運行路徑:\n\n");
for(i=0;i<20;i++){
for(j=0;j<20;j++){
if(mg[i][j]==0)printf(" ");
else if(mg[i][j]==1) printf("■"); //迷宮的"牆"
else if(mg[i][j]==3) printf("◇"); //不通的路
else if(mg[i][j]==7)printf("○"); //通過的路徑
}
printf("\n");
}
printf("\n");
}
void main(){
SqStack S;
PosType start,end;
start.x=1;start.y=0; //起點坐標
end.x=18;end.y=19; //終點坐標
printf("\n==================迷宮游戲==================");
printf("\n說明:■不能走的區域\t◇走不通的區域");
printf("\n '空格'代表未到過的區域");
printf("\n ○代表能通過的路徑,指向終點");
printf("\n============================================");
Random();
printf("\n\nTest 1:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
system("pause");
Random();
printf("\nTest 2:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
system("pause");
Random();
printf("\nTest 3:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
printf("\n==========程序退出,感謝使用!==========\n");
}
⑦ 電腦鼠走迷宮問題
加一個全局變數,在遞歸的時候進行計數,回溯的的時候記得回減....然後在輸出路線的時候將此變數輸出。不知道這樣可不可以滿足你的要求...
⑧ 用C語言編個走迷宮程序,要求:1:迷宮的規模和地圖由程序隨機自動生成。入口和出口由用戶指定。
程序目的:
輸入一個任意大小的迷宮,用棧求出一條走出迷宮的路徑,並
顯示在屏幕上。
程序實現:
可以實現載入迷宮和保存迷宮,附帶文件中有4個測試迷宮路徑的
文件test1~4.dd。請將這些文件拷貝到TC當前目錄下,或者在載
入時寫明完全路徑。由於屏幕大小的限制,當用戶自己輸入迷宮
時一定要注意:迷宮大小是有限制的,不小於4*3,不大於30*20。
否則會出現錯誤信息。輸入開始時全是牆,用上下左右鍵移動,
用Del鍵刪除牆,形成通路,用Enter鍵添加牆。輸入結束時可以
將迷宮保存下來,以dd為擴展名。輸入完畢時用F9鍵來得到結果,
找到路徑時,屏幕下方會出現Path found,否則出現Path not found。
程序經Turbo C 2.0編譯調試成功。運行時不用添加任何運行庫。
不可以在VC上編譯。
下載DOS版和windows版的迷宮游戲全部代碼
用戶名:migong
----------------------------------------------------------------------------------
/**//*
MazePath Demo BY Turbo C 2.0
Copyright(c) RoverUnion. All right reserved.
Filename: Maze.c
Author Dongchengyu.
Ver 1.10
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#include <dos.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define F9 0x43
#define Esc 0x1b
#define Del 0x53
#define Home 0x47
#define End 0x4f
#define Space 0x20
#define Up 0x48
#define Down 0x50
#define Left 0x4b
#define Right 0x4d
#define Enter 0x0d
#define F2 0x3c
#define F3 0x3d
#define STACK_INIT_SIZE 200
#define STACKINCREMENT 10
typedef int Boolean;
typedef int Status;
typedef struct {
int x;
int y;
} PosType;
typedef struct {
int ord;
PosType seat;
int di;
} SElemType;
typedef struct {
int td;
int foot;
int mark;
} MazeType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
} Stack;
int Maze[20][30];
MazeType maze[20][30];
PosType StartPlace;
PosType EndPlace;
int count;
int m,n;
Boolean b_start=FALSE,b_end=FALSE;
void CreatMaze(void);
Status SaveMaze(char *filename);
Status LoadMaze(char *filename);
void Error(char *message);
Status InitStack(Stack *s);
Status DestroyStack(Stack *s);
Status ClearStack(Stack *s);
Boolean StackEmpty(Stack *s);
int StackLength(Stack *s);
Status Push(Stack *s,SElemType e);
SElemType Pop(Stack *s,SElemType e);
Status GetTop(Stack *s,SElemType *e);
Status StackTraverse(Stack *s,Status (* visit)(SElemType *se));
Boolean Pass(PosType curpos);
void MarkPrint(PosType seat);
void FootPrint(PosType curpos);
PosType NextPos(PosType seat,int di);
Status MazePath(PosType start,PosType end);
void CreatMaze(void)
/**//* Form the maze. */
{
void Error(char *message);
Status SaveMaze(char *filename);
Status LoadMaze(char *filename);
int i,j;
int x,y;
char c;
char savename[12],loadname[12];
Boolean flag=FALSE,load=FALSE;
clrscr();
printf("Menu:\n\n");
printf("1.Load Mazefile:(*.dd)\n\n");
printf("2.Input Maze:\n\n");
printf("Input your choice: ");
do
{
c=getch();
switch(c)
{
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''); break;
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''); break;
case Esc: sleep(1); exit(1);
default: break;
}
}
while(c!=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''&&c!=''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''') ;
if(c==''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''')
{
printf("\n\nLoadName: ");
scanf("%s",loadname);
if(LoadMaze(loadname))
{
sleep(1); load=TRUE;
}
else { gotoxy(1,9); printf("Load fail! "); }
}
if(!load)
{
printf("\nInput the maze''''''''''''''''''''''''''''''''s size:\n");
printf("\nInput Length :\n");
scanf("%d",&m);
printf("\nInput Width :\n");
scanf("%d",&n);
if(m<4||n<4) Error("Input");
if(m>30||n>20) Error("Maze too large");
for(i=0;i<30;i++)
for(j=0;j<20;j++)
Maze[j][i]=2;
StartPlace.x=0;
StartPlace.y=0;
EndPlace.x=0;
EndPlace.y=0;
clrscr();
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf(" #");
Maze[i-1][j-1]=0;
}
printf("\n");
}
}
gotoxy(65,5);
printf("''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''':Wall");
gotoxy(65,7);
printf("Start:Home");
gotoxy(65,9);
printf("End:End");
gotoxy(65,11);
printf("Delete Wall:Del");
gotoxy(65,13);
printf("Enter Wall:Enter");
gotoxy(65,15);
printf("Save Maze:F2");
gotoxy(65,17);
printf("Complete:F9");
gotoxy(65,19);
printf("Exit:Esc");
gotoxy(4,3);
x=4;y=3;
do
{
c=getch();
switch(c)
{
case Up: if(y>3) { y--; gotoxy(x,y); }
break;
case Down: if(y<n) { y++; gotoxy(x,y); }
break;
case Left: if(x>4) { x-=2; gotoxy(x,y); }
break;
case Right: if(x<2*m-2) { x+=2; gotoxy(x,y); }
break;
case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE;
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) b_end=FALSE;
putch('''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=1; gotoxy(x,y);
break;
case Enter: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) break;
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) break;
putch(''''''''''''''''''''''''''''''''#''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=0; gotoxy(x,y);
break;
case Home: if(Maze[y-2][x/2-1]&&!b_start)
{
StartPlace.x=x/2-1;
StartPlace.y=y-2;
putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
gotoxy(x,y);
b_start=TRUE;
}
break;
case End: if(Maze[y-2][x/2-1]&&!b_end)
{
EndPlace.x=x/2-1;
EndPlace.y=y-2;
putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
gotoxy(x,y);
b_end=TRUE;
}
break;
case Esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1);
case F9: if(b_start&&b_end) flag=TRUE; break;
case F2: gotoxy(2,22);
printf("Savename:");
scanf("%s",savename);
gotoxy(2,22);
if(SaveMaze(savename)) printf("Save OK! ");
else printf("Save fail! ");
sleep(1);
gotoxy(2,22);
printf(" ");
gotoxy(x,y);
break;
default: break;
}
}
while(!flag);
for(i=0;i<30;i++)
for(j=0;j<20;j++)
{
maze[j][i].td=Maze[j][i];
maze[j][i].mark=0;
maze[j][i].foot=0;
}
}
Status LoadMaze(char *file)
/**//* The maze has been loaded. */
{
FILE *fp;
char *buffer;
char ch;
int i=0,j,k;
Boolean len=FALSE,wid=FALSE;
if((fp=fopen(file,"r"))==NULL)
return ERROR;
buffer=(char *)malloc(600*sizeof(char));
ch=fgetc(fp);
while(ch!=EOF)
{
buffer[i]=ch;
i++;
ch=fgetc(fp);
}
m=30;n=20;
for(i=0;i<600;i++)
{
j=i/30; k=i%30;
if(buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!len){ m=i; len=TRUE; }
if(k==0&&buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!wid){ n=j; wid=TRUE; }
switch(buffer[i])
{
case ''''''''''''''''''''''''''''''''0'''''''''''''''''''''''''''''''': Maze[j][k]=0; break;
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': Maze[j][k]=1; break;
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': Maze[j][k]=2; break;
case ''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''': Maze[j][k]=1;
StartPlace.x=k;
StartPlace.y=j;
b_start=TRUE;
break;
case ''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''': Maze[j][k]=1;
EndPlace.x=k;
EndPlace.y=j;
b_end=TRUE;
break;
default : break;
}
}
fclose(fp);
clrscr();
for(i=0;i<30;i++)
for(j=0;j<20;j++)
{
maze[j][i].td=Maze[j][i];
maze[j][i].foot=0;
maze[j][i].mark=0;
if(Maze[j][i]==0)
{
gotoxy(2*i+2,j+2);
putch(''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''');
}
}
gotoxy(2*StartPlace.x+2,StartPlace.y+2);
putch(''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
gotoxy(2*EndPlace.x+2,EndPlace.y+2);
putch(''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
return OK;
}
Status SaveMaze(char *filename)
/**//* The maze has been saved. */
{
FILE *fp;
char *buffer;
int i,j,k;
fp=fopen(filename,"wb");
buffer=(char *)malloc(600*sizeof(char));
for(i=0;i<600;i++)
{
j=i/30; k=i%30;
switch(Maze[j][k])
{
case 0: buffer[i]=''''''''''''''''''''''''''''''''0''''''''''''''''''''''''''''''''; break;
case 1: buffer[i]=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''; break;
case 2: buffer[i]=''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''; break;
default : Error("Write"); break;
}
if(k==StartPlace.x&&j==StartPlace.y) buffer[i]=''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''';
if(k==EndPlace.x&&j==EndPlace.y) buffer[i]=''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''';
}
fwrite(buffer,600,1,fp);
free(buffer);
fclose(fp);
return OK;
}
void Error(char *message)
{
clrscr();
fprintf(stderr,"Error:%s\n",message);
exit(1);
} /**//* Error */
Status InitStack(Stack *s)
/**//* The stack s has been created and is initialized to be empty. */
{
s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base) Error("Overflow");
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /**//* InitStack */
Status DestroyStack(Stack *s)
/**//* The stack s has been destroyed. */
{
s->top=NULL;
s->stacksize=0;
free(s->base);
s->base=NULL;
return OK;
} /**//* DestroyStack */
Status ClearStack(Stack *s)
/**//* The stack has been clear to be maximum. */
{
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
} /**//* ClearStack */
Boolean StackEmpty(Stack *s)
/**//* Check if the stack s is empty. */
{
if(s->top==s->base) return TRUE;
else return FALSE;
} /**//* StackEmpty */
int StackLength(Stack *s)
/**//* Gain the length of the stack s. */
{
if(s->top>s->base) return (int)(s->top-s->base);
else return 0;
} /**//* StackLength */
Status Push(Stack *s,SElemType e)
/**//* The element e has been pushed into the stack s. */
{
if(s->top-s->base>=s->stacksize)
{
s->base=(SElemType *)realloc(s->base,
(s->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s->base) Error("Overflow");
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
} /**//* Push */
SElemType Pop(Stack *s,SElemType e)
/**//* The element e has been removed from the stack s. */
{
if(s->top==s->base) Error("Pop");
e=*--s->top;
return e;
} /**//* Pop */
Status GetTop(Stack *s,SElemType *e)
/**//* The element e has got to the top of the stack s.*/
{
if(s->top==s->base) Error("GetTop");
*e=*(s->top-1);
return OK;
} /**//* GetTop */
/**//* Traverse the stack s using ''''''''''''''''''''''''''''''''visiting'''''''''''''''''''''''''''''''' function. */
/**//* Status StackTraverse(Stack *s,Status (* visit)(SElemType *se))
{
SElemType p;
int result;
if(s->top==s->base) return ERROR;
p=s->base;
while(!(p==s->top))
{
result=(*visit)(p);
p++;
}
return OK;
} */
Boolean Pass(PosType curpos)
/**//* Check if the current position can be passed. */
{
if(maze[curpos.x][curpos.y].td==1&&
maze[curpos.x][curpos.y].foot==0&&maze[curpos.x][curpos.y].mark==0)
return TRUE;
else return FALSE;
} /**//* Pass */
void MarkPrint(PosType seat)
/**//* Mark the position seat. */
{
maze[seat.x][seat.y].mark=-1;
/**//* Marking ''''''''''''''''''''''''''''''''-1'''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */
} /**//* MarkPrint */
void FootPrint(PosType curpos)
/**//* The foot of the curpos of the maze has been set ''''''''''''''''''''''''''''''''true''''''''''''''''''''''''''''''''. */
{
maze[curpos.x][curpos.y].foot=1;
} /**//* FootPrint */
PosType NextPos(PosType seat,int di)
{
switch(di)
{
case 1: seat.y++; return seat; /**//* Eastward */
case 2: seat.x++; return seat; /**//* Southward */
case 3: seat.y--; return seat; /**//* Westward */
case 4: seat.x--; return seat; /**//* Northward */
default: seat.x=0; seat.y=0; return seat;
}
} /**//* NextPos */
/**//* The key to the program. */
/**//* Pre: The maze array & the startplace & the endplace.
Post: Find the one traverse of the maze and perform the mazepath.
Uses: The ADT stack class.
*/
Status MazePath(PosType start,PosType end)
{
PosType curpos;
int curstep;
SElemType e;
Stack *s,stack;
stack.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!stack.base) Error("Overflow");
stack.top=stack.base;
stack.stacksize=STACK_INIT_SIZE;
s=&stack;
curpos=start;
curstep=1;
do
{
if(Pass(curpos))
{
FootPrint(curpos);
e.ord=curstep; e.seat=curpos; e.di=1;
gotoxy((curpos.y+1)*2,curpos.x+2);
putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
delay(8000); /**//* pospone time. */
Push(s,e);
if(curpos.x==end.x&&curpos.y==end.y) /**//* Proceed recursively. */
{
DestroyStack(s);
return TRUE;
}
curpos=NextPos(curpos,1); /**//* Try next position. */
curstep++;
}
else
{
if(!StackEmpty(s))
{
e=Pop(s,e); /**//* Removed e from s. */
while(e.di==4&&!StackEmpty(s)) /**//* Four directions have been checked
and s is not empty. */
{
MarkPrint(e.seat);
gotoxy((e.seat.y+1)*2,e.seat.x+2);
putch(''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
delay(8000); /**//* Pospone time. */
gotoxy((e.seat.y+1)*2,e.seat.x+2);
putch('''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''');
e=Pop(s,e); /**//* Remove e from s. */
curstep--;
}
if(e.di<4) /**//* The current position hasnot been checked. */
{
e.di++;
Push(s,e); /**//* Insert e into s. */
curpos=NextPos(e.seat,e.di); /**//* Try next position. */
}
}
}
}
while(!StackEmpty(s));
DestroyStack(s);
return FALSE;
} /**//* MazePath */
void main()
{
PosType start,end;
CreatMaze();
start.x=StartPlace.y;
start.y=StartPlace.x;
end.x=EndPlace.y;
end.y=EndPlace.x;
if(MazePath(start,end))
{
gotoxy(2,22);
printf("Path found\n");
}
else
{
gotoxy(2,22);
printf("Path not found\n");
}
getch();
clrscr();
}
⑨ 電腦鼠的比賽規則
最新的電腦鼠比賽規則是2006 年國際電工和電子工程學會(IEEE)制定的電腦鼠走迷宮競賽規則,這個規則將會對我們製作電腦鼠具體方案的設計提供依據。
電腦鼠比賽標准迷宮由廣州周立功單片機發展有限公司設計和生產的電腦鼠比賽專用迷宮完全符合 IEEE 國際標准。針對不同的需求,目前共有兩種可供選擇的型號。
1. MicroMouse Maze 8×8:
四分之一迷宮,如圖所示。即該迷宮是標准迷宮的四分之一大小。該迷宮底板的尺寸為1.48m×1.48m,上面共有8×8 個標准迷宮單元格。該迷宮可以用來初期調試學習使用,也可以用來做學校課程設計、畢業設計和內部競賽的比賽迷宮。
2.MicroMouse Maze 16×16:
標准迷宮,如圖所示。該迷宮尺寸規格等完全符合IEEE 國際標准。迷宮底板的尺寸為2.96m×2.96m,上面共有16×16 個標准迷宮單元格。
如下圖所示,MicroMouse615 是由廣州致遠電子設計生產的一款電腦鼠,它的微控制器是由Luminary 公司生產的Cortex-M3 內核的ARM 處理器——LM3S615,它具有以下一些特點:·體積小,寬度只有迷宮格的一半;
·五組可測距的紅外線感測器,靈敏度方便現場調節;
·電機為步進電機,控制容易;
·電池為 2200mAh,7.4V 的可充電鋰電池;
·支持電池的電壓監測,避免電量不足帶來的麻煩;
·一個按鍵,完全滿足了實際需要;
·為用戶預留了 6 個GPIO 口,一個串口,一個SPI 介面。配套的開發工具
如圖所示,與MicroMouse615 配套的有充電器、LM LINK USB JTAG 調試器和SPI介面的鍵盤顯示模塊,使用戶開發調試更為方便。
文檔閱讀說明
本文以廣州致遠電子有限公司生產的MicroMouse615 型電腦鼠作為硬體開發平台,從硬體原理到程序設計都做了詳細分解。文中附了大量程序源代碼,在程序設計過程中,為了便於閱讀和編寫,使用了一套變數的定義方法。
數據類型定義
如程序清單1.1 所示,重新定義幾種常用的數據類型名。
//程序清單1.1 數據類型重定義
typedef unsigned char uint8; // 無符號8 位整型變數
typedef signed char int8; // 有符號8 位整型變數
typedef unsigned short uint16; // 無符號16 位整型變數
typedef signed short int16; // 有符號16 位整型變數
typedef unsigned int uint32; // 無符號32 位整型變數
typedef signed int int32; // 有符號32 位整型變數
typedef float fp32; // 單精度浮點數(32 位長度)
typedef double fp64; // 雙精度浮點數(64 位長度)
2. 局部變數定義
局部變數名包含變數類型和變數描述兩個部分,以局部變數Temp 為例,在不同類型下的定義如表1.1 所示。可以看出,在變數Temp 前加上了其類型的縮寫。