① 谁有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 前加上了其类型的缩写。