c语言编写迷宫游戏(如何用c语言编写迷宫游戏)

本文目录
- 如何用c语言编写迷宫游戏
- 用C语言迷宫求解
- C语言 老鼠走迷宫
- C语言 走迷宫游戏程序
- 关于C语言编写一个迷宫
- c语言的走迷宫游戏的程序(VC6用的)
- 求助,用C语言并用回溯算法编写的有关蛇吃苹果走迷宫的程序,题目如下:
如何用c语言编写迷宫游戏
#include 《graphics.h》
#include 《stdlib.h》
#include 《stdio.h》
#include 《conio.h》
#include 《dos.h》
#define N 20/*
迷宫的大小,可改变
*/
int oldmap;/*
递归用的数组
,
用全局变量节约时间
*/
int yes=0;/*yes
是判断是否找到路的标志
,1
找到,
0
没找到
*/
int way,wayn=0;/*way
数组是显示路线用的
,wayn
是统计走了几个格
子
*/
void Init(void);/*
图形初始化
*/
void Close(void);/*
图形关闭
*/
void DrawPeople(int *x,int *y,int n);/*
画人工探索物图
*/
void PeopleFind(int (*x));/*
人工探索
*/
void
WayCopy(int
(*x),int
(*y));/*
为了
8
个方向的递归,把旧迷宫图
拷贝给新数组
*/
int FindWay(int (*x),int i,int j);/*
自动探索函数
*/
void MapRand(int (*x));/*
随机生成迷宫函数
*/
void PrMap(int (*x));/*
输出迷宫图函数
*/
void Result(void);/*
输出结果处理
*/
void Find(void);/*
成功处理
*/
void NotFind(void);/*
失败处理
*/
void main(void)/*
主函数
*/
{
int map; /*
迷宫数组
*/
char ch;
clrscr();
printf("\n Please select hand(1) else auto\n");/*
选择探索方式
*/
scanf("%c",&ch);
Init(); /*
初始化
*/
MapRand(map);/*
生成迷宫
*/
PrMap(map);/*
显示迷宫图
*/
if(ch==’1’)
PeopleFind(map);/*
人工探索
*/
else
FindWay(map,1,1);/*
系统自动从下标
1,1
的地方开始探索
*/
Result();/*
输出结果
*/
Close();
}
void Init(void)/*
图形初始化
*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc"); }
void DrawPeople(int *x,int *y,int n)/*画人工控制图*/ {/*如果将以下两句注释掉,则显示人工走过的路径,*/
setfillstyle(SOLID_FILL,WHITE); /*设置白色实体填充样式*/ bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6); /*恢复原通路*/
switch(n)/*判断x,y的变化,8个方向的变化*/ {
case 1: (*x)--;break; /*上*/
case 2: (*x)--;(*y)++;break /*右上*/ case 3: (*y)++;break; /*右*/
case 4: (*x)++;(*y)++;break; /*右下*/ case 5: (*x)++;break; /*下*/
case 6: (*x)++;(*y)--;break; /*左下*/ case 7: (*y)--;break; /*左*/
case 8: (*x)--;(*y)--;break; /*左上*/ }
setfillstyle(SOLID_FILL,RED);/*新位置显示探索物*/
bar(100+(*y)*15-6,50+(*x)*15-6,100+(*y)*15+6,50+(*x)*15+6); }
void PeopleFind(int (*map))/*人工手动查找*/ {
int x,y;
char c=0;/*接收按键的变量*/ x=y=1;/*人工查找的初始位置*/ setcolor(11);
line(500,200,550,200); outtextxy(570,197,"d"); line(500,200,450,200); outtextxy(430,197,"a"); line(500,200,500,150); outtextxy(497,130,"w"); line(500,200,500,250); outtextxy(497,270,"x"); line(500,200,450,150); outtextxy(445,130,"q"); line(500,200,550,150); outtextxy(550,130,"e"); line(500,200,450,250); outtextxy(445,270,"z"); line(500,200,550,250);
outtextxy(550,270,"c");/*以上是画8个方向的控制介绍*/
setcolor(YELLOW);
outtextxy(420,290,"Press ’Enter’ to end");/*压回车键结束*/ setfillstyle(SOLID_FILL,RED);
bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6);/*入口位置显示*/ while(c!=13)/*如果按下的不是回车键*/ {
c=getch();/*接收字符后开始各个方向的探索*/ if(c==’w’&↦!=1) DrawPeople(&x,&y,8); /*左上*/ }
setfillstyle(SOLID_FILL,WHITE); /*消去红色探索物,恢复原迷宫图*/ bar(100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6); if(x==N-2&&y==N-2)/*人工控制找成功的话*/ yes=1; /*如果成功标志为1*/ }
void WayCopy(int (*oldmap))/*拷贝迷宫数组 */ {
int i,j;
for(i=0;i《N;i++) for(j=0;j《N;j++) oldmap; }
int FindWay(int (*map),int i,int j)/*递归找路*/ {
if(i==N-2&&j==N-2)/*走到出口*/ {
yes=1;/*标志为1,表示成功*/ return; }
map=1;/*走过的地方变为1*/ WayCopy(oldmap,map); /*拷贝迷宫图*/
if(oldmap==0&&!yes)/*判断右下方是否可走*/ {
FindWay(oldmap,i+1,j+1); if(yes)/*如果到达出口了,再把值赋给显示路线的way数组,也正是这个原因,所以具体路线是从最后开始保存*/ { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断下方是否可以走,如果标志yes已经是1也不用找下去了*/ {
FindWay(oldmap,i+1,j); if(yes) { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断右方是否可以走*/ {
FindWay(oldmap,i,j+1); if(yes) { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断上方是否可以走*/ {
FindWay(oldmap,i-1,j); if(yes) { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断右上方是否可以走*/ {
FindWay(oldmap,i-1,j+1); if(yes) { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断左下方是否可以走*/ {
FindWay(oldmap,i+1,j-1); if(yes) { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断左方是否可以走*/ {
FindWay(oldmap,i,j-1); if(yes) { way=j; return; } }
WayCopy(oldmap,map);
if(oldmap==0&&!yes)/*判断左上方是否可以走*/ {
FindWay(oldmap,i-1,j-1); if(yes) { way=j; return; } }
return; }
void MapRand(int (*map))/*开始的随机迷宫图*/ {
int i,j;
cleardevice();/*清屏*/
randomize(); /*随机数发生器*/ for(i=0;i《N;i++) {
for(j=0;j《N;j++) { if(i==0||i==N-1||j==0||j==N-1)/*最外面一圈为墙壁*/ map=random(2);/*其它的随机生成0或1*/ } } }
void PrMap(int (*map))/*输出迷宫图*/ {
int i,j;
for(i=0;i《N;i++) for(j=0;j《N;j++) if(map==0) { setfillstyle(SOLID_FILL,WHITE);/*白色为可走的路*/ bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6); } else { setfillstyle(SOLID_FILL,BLUE);/*蓝色为墙壁*/ bar(100+j*15-6,50+i*15-6,100+j*15+6,50+i*15+6);
} }
void Find(void)/*找到通路*/ {
int i;
setfillstyle(SOLID_FILL,RED);/*红色输出走的具体路线*/ wayn--;
for(i=wayn;i》=0;i--) {
bar(100+way*15+6); sleep(1);/*控制显示时间*/ }
bar(100+(N-2)*15-6,50+(N-2)*15-6,100+ (N-2)*15+6,50+(N-2)*15+6); /*在目标点标红色*/ setcolor(GREEN);
settextstyle(0,0,2);/*设置字体大小*/ outtextxy(130,400,"Find a way!"); }
void NotFind(void)/*没找到通路*/ {
setcolor(GREEN);
settextstyle(0,0,2);/*设置字体大小*/ outtextxy(130,400,"Not find a way!"); }
void Result(void)/*结果处理*/ {
if(yes)/*如果找到*/ Find();
else/*没找到路*/ NotFind(); getch(); }
void Close(void)/*图形关闭*/ {
closegraph(); }
用C语言迷宫求解
给一个比较简短的程序:
#include "stdlib.h"
#include《stdio.h》
#include《string.h》
#define M 10
#define N 10
void ShowMaze(char m)
{ int i,j;
char fx={ "←","↑","→","↓" };
for ( i=0;i《M+2;i++,printf("\n") )
for ( j=0;j《N+2;j++ )
if ( m==’#’ ) printf(" ");
else if ( m);
else if ( m==’0’ ) printf(" ");
else if ( m==’1’ ) printf("■");
else if ( m==’@’ ) printf("○");
else if ( m==’*’ ) printf("◎");
}
void main()
{ int dir={ {-1,0},{0,-1},{1,0},{0,1} };
char m=
{ "############",
"#1111111111#",
"#@001000101#",
"#1101000101#",
"#1000011001#",
"#1011100001#",
"#100010000*#",
"#1010001001#",
"#1011101101#",
"#1100000001#",
"#1111111111#",
"############"
};
int i,j,k,xx,yy;
int x,y,d,f;
ShowMaze(m);
for ( i=0;i《M+2;i++ ) for ( j=0;j《N+2;j++ ) if ( m==’@’ ) { x=j; y=i; break; }
for ( i=0;i《4;i++ ) { xx=x+dir==’0’ ) { d=i; break; } }
f=0; x=xx; y=yy; d--; if ( d《0 ) d=3;
while ( 1 )
{ xx=x+dir;
if ( m=d+’a’; f=1; break; }
else if ( m==’@’ ) break;
else if ( m=’0’; x=xx; y=yy; }
else if ( m=d+’a’; d--; if ( d《0 ) d=3; x=xx; y=yy; }
else if ( m==’1’ ) { d++; d%=4; }
}
printf("----------------------\n"); ShowMaze(m);
}
C语言 老鼠走迷宫
可以给你点提示:迷宫 可用个二维数组表示。求解方法是:从入口出发,顺某个方向走,若能过去,继续;否则,沿着原路返回,换方向继续走,直到所有可能的通路都被找到为止。为保证在任何位置上都能沿原路退回,需要一个先进后出的栈结构保存从入口到当前位置的路径。这里,给个算法的思想,不实现图形界面了。假设迷宫数据存放在一txt中:
迷宫数据
8 8 //迷宫的大小,行数与列数
1 1 8 8 //1 1 表入口位置 8 8 表出口位置
0 0 1 0 0 0 1 0 //以下表迷宫,1表示墙、0表示通路,为避免走的过程中越界,最好在四周加上以堵墙。
0 0 1 0 0 0 1 0
0 0 0 0 1 1 0 0
0 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0
0 1 1 1 0 1 1 0
1 1 0 0 0 0 0 0
#include 《stdio.h》
#include 《stdlib.h》
#define MAXSIZE 50
#define ERROR -1
#define OK 0
#define FALSE 0
#define TRUE 1
typedef enum{RIGHT,DOWN,LEFT,UP} Direction;
typedef enum{YES,NO} MarkTag;
typedef struct position{ //迷宫中位置的坐标
int x;
int y;
}Position;
typedef struct{ //当前位置在路径中的序号
int order; //当前位置在迷宫中的坐标
Position seat; //从当前位置走到下一位置的方向含首
Direction di; //栈元素的类型
}SElemType;
typedef struct{
SElemType *elem;
int top;
}Stack;
char maze; //用二维数组表示迷宫
int InitStack(Stack *S){ //创建一个空栈
S-》elem=(SElemType *)malloc(MAXSIZE*MAXSIZE*sizeof(SElemType));
if(!S-》elem)
return ERROR;
S-》top=0;
return OK;
}
int Push(Stack *S,SElemType e){ //元素e入蚂老派栈
if(S-》top》=MAXSIZE*MAXSIZE)
return ERROR;
S-》elem=e;
return OK;
}
int Pop(Stack *S,SElemType e){ //栈顶元素出栈,由e带回栈顶元素
if(S-》top《=0)
return ERROR;
*e=S-》elem;
return OK;
}
int Empty(Stack S){ //若栈为空,返回TRUE,否则返回FALSE
if(S.top==0)
return TRUE;
return FALSE;
}
int createMaze(char *filename,Position *startpos,Position *endpos){ //从文件filename读入数据创建迷宫,由参数带闷贺回入口位置和出口位置
FILE *fp;
int i,j,rows,cols,temp;
Position start,end;
fp=fopen(filename,"r");
if(!fp){
printf("open file %s error!\n",filename);
return ERROR;
}
if(!feof(fp)){
fscanf(fp,"%d %d",&rows,&cols); //读入迷宫的行数和列数
fscanf(fp,"%d %d",&start.x,&start.y); //读入迷宫的入口位置
fscanf(fp,"%d %d",&end.x,&end.y); //读入迷宫的出口位置
}
for(i=1;i《=rows;i++) //读入迷宫数据
for(j=1;j《=cols;j++){
fscanf(fp,"%d",&temp);
maze=48+temp;
}
fclose(fp);
//在迷宫四周加墙
for(i=0,j=0;i《=rows+1;i++) maze=’1’;
for(i=0,j=cols+1;i《=rows+1;i++) maze=’1’;
for(i=0,j=0;j《=cols+1;j++) maze=’1’;
for(i=rows+1,j=0;j《=cols+1;j++) maze=’1’;
*startpos=start;
*endpos=end;
return OK;
}
int canPass(Position curpos){
if(maze==’0’)
return TRUE;
return FALSE;
}
void markPos(Position curpos,MarkTag tag){ //为已走过的位置标记
switch(tag){
case YES: maze=’.’; break; //路径标记
case NO: maze=’#’; break; //死胡同标记
}
}
Position nextPos(Position curpos,Direction dir){ //根据当前的位置坐标和下一步要探索的方向dir求下一步要走的位置坐标
Position nextpos;
switch(dir){
case RIGHT: nextpos.x=curpos.x; nextpos.y=curpos.y+1; break;
case DOWN: nextpos.x=curpos.x+1; nextpos.y=curpos.y; break;
case LEFT: nextpos.x=curpos.x; nextpos.y=curpos.y-1; break;
case UP: nextpos.x=curpos.x-1; nextpos.y=curpos.y; break;
}
return nextpos;
}
Direction nextDir(Direction dir){
switch(dir){ //按照RIGHT DOWN LEFT UP的次序进行路径探索
case RIGHT: return DOWN;
case DOWN: return LEFT;
case LEFT: return UP;
}
}
/*若迷宫中存在从入口start到出口end的通道,则求得一条存放在栈S中,并返回TRUE,若不存在则返回FALSE*/
int Solve(Stack *S,Position start,Position end){
Position curpos;
SElemType e;
int curstep=1;
if(InitStack(S)==ERROR)
return FALSE;
curpos=start;
do{
if(canPass(curpos)){ //当前位置可以通过
markPos(curpos,YES); //留下足迹
e.order=curstep;
e.seat=curpos;
e.di=RIGHT;
Push(S,e);
if(curpos.x==end.x && curpos.y=end.y)
return TRUE; //找到从入口到出口的通道
curpos=nextPos(curpos,RIGHT);
curstep++;
}
else{
if(!Empty(*S)){ //当前位置不能通过
if(Pos(S,&e)==ERROR)
return FALSE;
while(e.di==UP && !Empty(*S)){ //4个方向都找不到通路,则回溯
curpos=e.seat;
markPos(curpos,NO);
if(Pop(S,&e)==ERROR)
return FALSE;
}
if(e.di!=UP){ //4个方向还没有探索完
e.di=nextDir(e.di);
Push(S,e); //换下一个方向探索
curpos=nextPos(e.seat,e.di);
}
}
}while(!Empty(*S));
return FALSE;
}
void main(void){
Position startPos,endPos;
Stack path;
SElemType e;
char *fname="in.txt";
if(createMaze(fname,&startPos,&endPos)==ERROR) return;
Solve(&path,startPos,endPos);
while(!Empty(path)){ //输出出口到入口的路径
Pop(&path,&e);
printf("(%d,%d)\n",e.seat.x,e.seat.y);
}
}
C语言 走迷宫游戏程序
c描述 数据结构,清华大学版的,现成的。
#include《stdio.h》
#include《conio.h》
#include《math.h》
#include《stdlib.h》
#include《graphics.h》
#define x1 (a-120)/20
#define y1 (b-40)/20
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ENTER 13
#define ESC 27
int d={0};
int a=120,b=40;
void init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc20");
}
void drawlist()
{
int i,j;
setbkcolor(BLACK);
setcolor(RED);
for(i=0,j=0;i《=20,j《=20;i++,j++)
{
line(i*20+120,40,i*20+120,440);
line(120,40+20*j,520,40+20*j);
}
}
void drawfirst()
{
gotoxy(120,40);
setcolor(YELLOW);
circle(120+10,40+10,6);
setfillstyle(1,BROWN);
floodfill(120+10,40+10,YELLOW);
}
void clearold(int m,int n)
{
setfillstyle(1,BLACK);
floodfill(m+10,n+10,YELLOW);
setcolor(BLACK);
circle(m+10,n+10,6);
}
void drawnew(int m,int n)
{
setcolor(YELLOW);
circle(m+10,n+10,6);
setfillstyle(1,BROWN);
floodfill(m+10,n+10,YELLOW);
}
void filllist()
{
int i,j,t,m;
randomize();
for(i=0;i《=18;i++)
for(j=1;j《=19;j++)
{ t=random(2)
if(t==1)
{
setfillstyle(1,1);
floodfill(121+20*i,41+20*j,RED);
d=1;}
else d=0;
}
d=0;
setfillstyle(1,BLACK);
floodfill(121,41,RED);
floodfill(121+19*20,41+19*20,RED);
}
void getway()
{
int flag=1;
while(flag==1)
{
gotoxy(a,b);
for(;b《=440&&a《=520&&a》=120&&b》=40;)
{
switch(getch())
{
case UP : {
if(b==40);
else if(d==0)
{clearold(a,b);gotoxy(a,b=b-20);drawnew(a,b);}
else;
break;}
case DOWN:{
if(b==440);
else
if(d==0){clearold(a,b);gotoxy(a,b=b+20);drawnew(a,b);}else;
break; }
case RIGHT : {
if(b==520);
else
if(d==0){clearold(a,b);gotoxy(a=a+20,b);drawnew(a,b);}else;
break; }
case LEFT : {
if(b==120);
else
if(d==0){clearold(a,b);gotoxy(a=a-20,b);drawnew(a,b);}else;
break;}
case ESC : exit();break;
default : break;
if(a==500&&b==420)break;
}/*switch finish*/
}/*for finish*/
}/*while finish*/
}
void main()
{
init();
drawlist();
filllist();
drawfirst();
getway();
getch();
closegraph();
}
关于C语言编写一个迷宫
#include《stdio.h》
#include《stdlib.h》
#define M 15
#define N 15
struct mark //定义迷宫内点的坐标类型
{
int x;
int y;
};
struct Element //"恋"栈元素,嘿嘿。。
{
int x,y; //x行,y列
int d; //d下一步的方向
};
typedef struct LStack //链栈
{
Element elem;
struct LStack *next;
}*PLStack;
/*************栈函数****************/
int InitStack(PLStack &S)//构造空栈
{
S=NULL;
return 1;
}
int StackEmpty(PLStack S)//判断栈是否为空
{
if(S==NULL)
return 1;
else
return 0;
}
int Push(PLStack &S, Element e)//压入新数据元素
{
PLStack p;
p=(PLStack)malloc(sizeof(LStack));
p-》elem=e;
p-》next=S;
S=p;
return 1;
}
int Pop(PLStack &S,Element &e) //栈顶元素出栈
{
PLStack p;
if(!StackEmpty(S))
{
e=S-》elem;
p=S;
S=S-》next;
free(p);
return 1;
}
else
return 0;
}
/***************求迷宫路径函数***********************/
void MazePath(struct mark start,struct mark end,int maze)
{
int i,j,d;int a,b;
Element elem,e;
PLStack S1, S2;
InitStack(S1);
InitStack(S2);
maze=2; //入口点作上标记
elem.x=start.x;
elem.y=start.y;
elem.d=-1; //开始为-1
Push(S1,elem);
while(!StackEmpty(S1)) //栈不为空 有路径可走
{
Pop(S1,elem);
i=elem.x;
j=elem.y;
d=elem.d+1; //下一个方向
while(d《4) //试探东南西北各个方向
{
a=i+diradd;
b=j+diradd;
if(a==end.x && b==end.y && maze==0) //如果到了出口
{
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem);
elem.x=a;
elem.y=b;
elem.d=886; //方向输出为-1 判断是否到了出口
Push(S1,elem);
printf("\n0=东 1=南 2=西 3=北 886为则走出迷宫\n\n通路为:(行坐标,列坐标,方向)\n");
while(S1) //逆置序列 并输出迷宫路径序列
{
Pop(S1,e);
Push(S2,e);
}
while(S2)
{
Pop(S2,e);
printf("--》(%d,%d,%d)",e.x,e.y,e.d);
}
return; //跳出两层循环,本来用break,但发现出错,exit又会结束程序,选用return还是不错滴
}
if(maze==0) //找到可以前进的非出口的点
{
maze=2; //标记走过此点
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem); //当前位置入栈
i=a; //下一点转化为当前点
j=b;
d=-1;
}
d++;
}
}
printf("没有找到可以走出此迷宫的路径\n");
}
/*************建立迷宫*******************/
void initmaze(int maze)
{
int i,j;
int m,n; //迷宫行,列
printf("请输入迷宫的行数 m=");
scanf("%d",&m);
printf("请输入迷宫的列数 n=");
scanf("%d",&n);
printf("\n请输入迷宫的各行各列:\n用空格隔开,0代表路,1代表墙\n",m,n);
for(i=1;i《=m;i++)
for(j=1;j《=n;j++)
scanf("%d",&maze);
printf("你建立的迷宫为(最外圈为强)...\n");
for(i=0;i《=m+1;i++) //加一圈围墙
{
maze=1;
maze=1;
}
for(j=0;j《=n+1;j++)
{
maze=1;
maze=1;
}
for(i=0;i《=m+1;i++) //输出迷宫
{
for(j=0;j《=n+1;j++)
printf("%d ",maze);
printf("\n");
}
}
void main()
{
int sto;
struct mark start,end; //start,end入口和出口的坐标
int add
initmaze(sto);//建立迷宫
printf("输入入口的横坐标,纵坐标\n");
scanf("%d,%d",&start.x,&start.y);
printf("输入出口的横坐标,纵坐标\n");
scanf("%d,%d",&end.x,&end.y);
MazePath(start,end,sto,add); //find path
system("PAUSE");
}
c语言的走迷宫游戏的程序(VC6用的)
*此程序是我想象一个人实际走迷宫时的真实走法,并无什么确定的算法*/ #include "graphics.h"
#include "stdio.h"
#define N 10
#define M N*N-4+1 /*堆栈最大值,用来保存路口信息*/
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
#define UP_M man.x-1》=0&&a /*人当前位置的上一个位置*/
#define DOWN_M man.x+1#define LEFT_M man.y-1》=0&&a
#define RIGHT_M man.y+1#define ENTER 3 /*岔路的入口*/
#define HAVE 2 /*某条路已经走过*/ struct cross{int up,down,left,right,x,y;} across,stack={0};
int top=1;
int PX=70,PY=40;
struct {int x,y,s;}man;/*迷宫定义,1表示路,可自行更改迷宫的路径,可使全为1,看效果等*/
int a={0,0,0,1,1,0,1,1,1,0,《br》 1,1,0,1,0,1,1,0,1,0,《br》 0,1,1,1,1,0,1,1,1,1,《br》 1,0,1,0,1,1,1,0,1,0,《br》 1,1,1,1,0,0,1,1,1,1,《br》 1,0,1,0,1,1,1,0,1,0,《br》 0,0,1,1,1,0,1,1,1,0,《br》 1,1,1,0,1,0,1,0,1,0,《br》 0,0,1,0,1,1,1,0,1,1,《br》 0,1,1,0,0,1,0,0,0,0};
void init_man() /*初始化人的状态,要求迷宫入口须在左侧*/
{int i;setcolor(WHITE);《br》 for(i=0;i if(a) man.s=UP;《br》 else {outtextxy(500,300,"no way!");getch();exit(0);}
return ;}
if(i==N) {outtextxy(500,300,"no enter!");getch();exit(0);}
}
void show_map() /*显示迷宫*/
{int i,j;《/p》《p》 for(i=0;i for(j=0;j if(a) {setfillstyle(1,WHITE);《br》 bar(PX+j*20,PY+i*20,PX+j*20+20,PY+i*20+20);}
else {setfillstyle(1,LIGHTBLUE);《br》 bar(PX+j*20,PY+i*20,PX+j*20+20,PY+i*20+20);}
setcolor(BLACK);
for(i=0;i {line(PX+i*20,PY,PX+i*20,PY+20*N);《br》 line(PX,PY+i*20,PX+20*N,PY+i*20);}
} #include《stdio.h》
#include《conio.h》
#include《math.h》
#include《stdlib.h》
#include《graphics.h》
#define x1 (a-120)/20
#define y1 (b-40)/20
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ENTER 13
#define ESC 27
int d={0};
int a=120,b=40;
void init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc20");
}
void drawlist()
{
int i,j;
setbkcolor(BLACK);
setcolor(RED);
for(i=0,j=0;i《=20,j《=20;i++,j++)
{
line(i*20+120,40,i*20+120,440);
line(120,40+20*j,520,40+20*j);
}
}
void drawfirst()
{
gotoxy(120,40);
setcolor(YELLOW);
circle(120+10,40+10,6);
setfillstyle(1,BROWN);
floodfill(120+10,40+10,YELLOW);
}
void clearold(int m,int n)
{
setfillstyle(1,BLACK);
floodfill(m+10,n+10,YELLOW);
setcolor(BLACK);
circle(m+10,n+10,6);
}
void drawnew(int m,int n)
{
setcolor(YELLOW);
circle(m+10,n+10,6);
setfillstyle(1,BROWN);
floodfill(m+10,n+10,YELLOW);
}
void filllist()
{
int i,j,t,m;
randomize();
for(i=0;i《=18;i++)
for(j=1;j《=19;j++)
{ t=random(2)
if(t==1)
{
setfillstyle(1,1);
floodfill(121+20*i,41+20*j,RED);
d=1;}
else d=0;
}
d=0;
setfillstyle(1,BLACK);
floodfill(121,41,RED);
floodfill(121+19*20,41+19*20,RED);
}
void getway()
{
int flag=1;
while(flag==1)
{
gotoxy(a,b);
for(;b《=440&&a《=520&&a》=120&&b》=40;)
{
switch(getch())
{
case UP : {
if(b==40);
else if(d==0)
{clearold(a,b);gotoxy(a,b=b-20);drawnew(a,b);}
else;
break;}
case DOWN:{
if(b==440);
else
if(d==0){clearold(a,b);gotoxy(a,b=b+20);drawnew(a,b);}else;
break; }
case RIGHT : {
if(b==520);
else
if(d==0){clearold(a,b);gotoxy(a=a+20,b);drawnew(a,b);}else;
break; }
case LEFT : {
if(b==120);
else
if(d==0){clearold(a,b);gotoxy(a=a-20,b);drawnew(a,b);}else;
break;}
case ESC : exit();break;
default : break;
if(a==500&&b==420)break;
}/*switch finish*/
}/*for finish*/
}/*while finish*/
}
void main()
{
init();
drawlist();
filllist();
drawfirst();
getway();
getch();
closegraph();
}
求助,用C语言并用回溯算法编写的有关蛇吃苹果走迷宫的程序,题目如下:
/*
* snake
*/
#include 《stdio.h》
#include 《stdlib.h》
#include 《string.h》
#define DEBUG 0
#define printpos() \
printf("File: %s\tLine: %d\n", __FILE__, __LINE__); fflush(stdout);
#define CALLOC(ARRAY, NUM, TYPE)\
ARRAY = (TYPE*) calloc(NUM, sizeof(TYPE));\
if (ARRAY == NULL) {\
printf("File: %s, Line: %d: ", __FILE__, __LINE__); \
printf("Allocating memory failed.\n");\
exit(0);\
}
#define REALLOC(ARRAY, NUM, TYPE)\
ARRAY = (TYPE*) realloc(ARRAY, (NUM)*sizeof(TYPE));\
if (ARRAY == NULL) {\
printf("File: %s, Line: %d: ", __FILE__, __LINE__); \
printf("Allocating memory failed.\n");\
exit(0);\
}
const int START = -1;
const int HOME = -2;
#if DEBUG
int m=4, n=4;
int a = {{7, 0, 4, 18}, {4, 0, 1, 1}, {15, 7, 11, -1}, {0, 12, -2, 0}};
#else
int m=0, n=0;
int **a=NULL;
#endif
struct pos {
int x;
int y;
};
typedef struct pos pos;
struct node {
pos p;
int mv;
int n;
};
typedef struct node node;
const pos mv = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
/*
* get m, n, a and check them
*/
int setup()
{
int nstart=0, nhome=0;
int i, j;
#if DEBUG
#else
//get the dimension of the matrix and allocate memory
printf("Please input the number of rows of the matrix: ");
scanf("%d", &m);
if (m《=0) {
printf("Number of rows must be greater than 0.\n");
exit(0);
}
a = (int**) calloc(m, sizeof(int*));
if (a == NULL) {
printf("Allocate memory failed.\n");
exit(1);
}
printf("Please input the number of columns of the matrix: ");
scanf("%d", &n);
if (n《=0) {
printf("Number of columns must be greater than 0.\n");
exit(0);
}
for (i=0; i《m; i++) {
a = (int*) calloc(n, sizeof(int));
if (a == NULL) {
printf("Allocate memory failed.\n");
exit(1);
}
}
//get the matrix
printf("Please input the matrix, entities seperated by blank:\n");
for (i=0; i《m; i++) {
for (j=0; j《n; j++) {
scanf("%d", &a);
}
}
#endif
//check the matrix
for (i=0; i《m; i++) {
for (j=0; j《n; j++) {
if (a == START) {
nstart++;
if (nstart 》 1) {
printf("More than 1 starting point.\n");
exit(0);
}
} else if (a == HOME) {
nhome++;
if (nhome 》 1) {
printf("More than 1 home point.\n");
exit(0);
}
} else if (a 《 0) {
printf("a);
exit(0);
}
}
}
if (nstart == 0) {
printf("No starting point.\n");
exit(0);
}
if (nhome == 0) {
printf("No home point.\n");
exit(0);
}
//output the matrix
printf("The matrix (%d X %d):\n", m, n);
for (i=0; i《m; i++) {
for (j=0; j《n; j++) {
printf("%d\t", a);
}
printf("\n");
}
return 0;
}
int solve(node** optpath)
{
pos dest;//destinating point
node* curpath = NULL;//current path
node** sol = NULL;
int nsol = 0;
int nsteps;//number of steps
int i, j;
int curmv = -1;
int sucmv = 0;//sucessfully moved
int sum;
int maxsum=0;
//setup starting point
for (i=0; i《m; i++) {
for (j=0; j《n; j++) {
if (a == START) {
dest.x = i;
dest.y = j;
break;
}
}
}
nsteps = 0;
CALLOC(curpath, nsteps+1, node);
curpath.p.x = dest.x;
curpath.p.y = dest.y;
curpath.mv = -1;
a = 0;
curmv = 0;
while (1) {
for (sucmv=0, curmv=curpath.mv+1; curmv《4; curmv++) {
dest.x = curpath.x;
dest.y = curpath.y;
if (dest.x 《 0 || dest.x 》= m || dest.y 《 0 || dest.y 》= n) {
curpath.mv = curmv;
continue;
}
if (a == 0) {
curpath.mv = curmv;
continue;
}
nsteps++;
REALLOC(curpath, nsteps+1, node);
curpath.p.x = dest.x;
curpath.p.y = dest.y;
curpath.mv = curmv;
curpath.mv = -1;
curpath;
a = 0;
sucmv = 1;
break;
}
if (sucmv) {
if (curpath.n == HOME) {
nsol++;
REALLOC(sol, nsol, node*);
CALLOC(sol, nsteps+1, node);
memcpy(sol, curpath, (nsteps+1)*sizeof(node));
//back
a.n;
nsteps--;
if (nsteps == -1 && curpath.mv == 3)break;
REALLOC(curpath, nsteps+1, node);
} else {
continue;
}
} else {
a.n;
nsteps--;
if (nsteps == -1 && curpath.mv == 3)break;
REALLOC(curpath, nsteps+1, node);
}
}
//printf("number of solutions: %d\n", nsol);
for (maxsum=0, i=0; i《nsol; i++) {
//printf("Solution %d \n", i);
//printf("\tPath: ");
sum = -1*HOME;
for (j=0; ; j++) {
//printf("(%d, %d)\t", sol.p.y);
sum += sol.n;
if (sol.mv == -1) break;
}
//printf("\n\tSum of apples: %d\n", sum);
if (sum》maxsum) {
maxsum = sum;
*optpath = sol;
}
}
return 0;
}
int output(node* path)
{
int i=0, sum=0;
printf("Path: ");
sum = -1*HOME;
for (i=0; ; i++) {
printf("(%d, %d)\t", path.p.y);
sum += path.n;
if (path.mv == -1) break;
}
printf("\nSum of apples: %d\n", sum);
return 0;
}
int main()
{
node* path=NULL;
setup();
solve(&path);
output(path);
return 0;
}
编译、链接、运行程序,输入与输出如下:
:!gcc -Wall tmp.c -o tmp
:! ./tmp
Please input the number of rows of the matrix: 5
Please input the number of columns of the matrix: 5
Please input the matrix, entities seperated by blank:
1 7 9 7 0
-2 8 10 8 7
0 10 8 2 -1
4 3 0 7 0 9
1 2 5 1 0 7
The matrix (5 X 5):
1 7 9 7 0
-2 8 10 8 7
0 10 8 2 -1
4 3 0 7 0
9 1 2 5 1
Path: (2, 4) (1, 4) (1, 3) (0, 3) (0, 2) (1, 2) (2, 2) (2, 3) (3, 3) (4, 3) (4, 2) (4, 1) (4, 0) (3, 0) (3, 1) (2, 1) (1, 1) (0, 1) (0, 0) (1, 0)
Sum of apples: 108
:!gcc -Wall tmp.c -o tmp
:! ./tmp
Please input the number of rows of the matrix: 4
Please input the number of columns of the matrix: 4
Please input the matrix, entities seperated by blank:
7 0 4 18
4 0 1 1
15 7 11 -1
0 12 -2 0
The matrix (4 X 4):
70418
4011
15711-1
012-20
Path: (2, 3)(1, 3)(0, 3)(0, 2)(1, 2)(2, 2)(2, 1)(3, 1)(3, 2)
Sum of apples: 54

更多文章:
directive vue(如何在Vue中建立全局引用或者全局命令)
2026年5月6日 19:15
javaeclipse包的新建(用eclipse如何创建java工程)
2026年1月11日 08:30
c语言switch语句case后面接什么(c语言switch语句中case后面必须要接整型常量和字符型常量吗)
2025年10月26日 08:15
怎么把普通文件导入变成web项目(java项目怎么转成web项目)
2025年9月18日 16:00
在循环体内使用break语句(在循环结构中使用break语句,退出的是一层循环,还是多重循环呢可以给出一个简单且巧妙的例子吗)
2026年5月1日 00:00
position在css的属性(CSS中position属性详解)
2025年9月26日 23:45
amazed是什么牌子(amazing和amazed有什么区别)
2026年4月3日 15:00
周期函数值域的求法(已知函数的最小正周期为求;当时,求函数的值域.)
2025年11月30日 22:15
毕设做网站必须要用到ssm框架嘛(毕业设计做ssm还是app更难)
2026年3月31日 03:00
new directory(如何将威纶通tk6070ip触摸屏程序上传到电脑里)
2026年8月7日 01:45

















