栈---数组---迷宫求解

#include <stdio.h>

//定义全局的变量index:栈数组的下标,i:x坐标。j:y坐标。k:自定义变量,遍历输出栈路径时使用
static 	int index=0,i=1,j=1,k;

//栈结构体,用于存储路径
struct STACK
{
	char x;//x坐标
	char y;//y
	char step;//1:右。2:下。3:左。4:上。
	struct STACK * next;//指向下一个结点
}stack1[20],*H;//定义指向结构体的数组,以及H指针,用于指向栈顶

//检查当前路径是否存在与栈中,返回1表示已经存在,返回0表示不存在
char isExit(int index1,int i1,int j1)
{
	int n;
	for(n=0;n<index;n++)
	{

		if(stack1[n].x==i1&&stack1[n].y==j1)
		{
			return 1;
		}
	}
	return 0;

} 

//打印栈函数元素
void print(int index2)
{
	if(index2>=0)
	{
		printf("x=%d,y=%d\n",stack1[index2].x,stack1[index2].y);
	}
}

//主函数入口
void main()
{
	//定义的迷宫
	char sp[10][10]={{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1},{1,0,1,1,1,0,0,0,0,1},{1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}};

	//初始化第一个元素
	stack1[index].x=i;
	stack1[index].y=j;
	stack1[index].step=1;
	stack1[index].next=NULL;

	//设置头指针
	H=&stack1[index];
	
	//不为出口则一直循环
	while(H->x!=8||H->y!=8)
	{
		//当step大于4时表示超出,重设为1
		if(H->step>4)
			H->step=1;
		//向右移动
		while(stack1[index].step==1&&sp[i][j+1]==0)
		{
			index++;
			i=i;
			j=j+1;

			stack1[index].step=stack1[index-1].step;
			stack1[index].x=i;
			stack1[index].y=j;
			stack1[index].next=H;
			H=&stack1[index];

			//如果下一步已经存在于栈中
			if(isExit(index,i,j)==1)
			{
				//并且它的其他三面都是1(墙壁)
				//if(sp[i+1][j]==1&&sp[i][j-1]==1&&sp[i-1][j]==1)
				//{
					//删除栈顶数据,step++,index--
					H=H->next;
					stack1[index].next=NULL;
					index--;
					i=i;
					j=j-1;
					if(sp[i-1][j]==1&&sp[i+1][j]==1)
					{
						H=H->next;
						stack1[index].next=NULL;
						index--;
						i=i;
						j=j+1;
						H->step--;
					}
					H->step++;
				//}
			}
		
		}
		if(sp[i][j+1]==1)
			H->step++;
		//向下移动
		while(stack1[index].step==2&&sp[i+1][j]==0)
		{
			index++;
			i=i+1;
			j=j;
			
			stack1[index].step=stack1[index-1].step;
			stack1[index].x=i;
			stack1[index].y=j;
			stack1[index].next=H;
			H=&stack1[index];

			//如果下一步已经存在于栈中
			if(isExit(index,i,j)==1)
			{
				//并且它的其他三面都是1(墙壁)
				//if(sp[i+1][j]==1&&sp[i][j-1]==1&&sp[i-1][j]==1)
				//{
					//删除栈顶数据,step++,index--
					H=H->next;
					stack1[index].next=NULL;
					index--;
					i=i-1;
					j=j;
					H->step++;
				//}
			}
		}
		if(sp[i+1][j]==1)
			H->step++;
		//项左移动
		while(stack1[index].step==3&&sp[i][j-1]==0)
		{
			index++;
			i=i;
			j=j-1;
			
			stack1[index].step=stack1[index-1].step;
			stack1[index].x=i;
			stack1[index].y=j;
			stack1[index].next=H;
			H=&stack1[index];

			//如果下一步已经存在于栈中
			if(isExit(index,i,j)==1)
			{
				//并且它的其他三面都是1(墙壁)
			//	if(sp[i+1][j]==1&&sp[i][j-1]==1&&sp[i-1][j]==1)
				//{
					//删除栈顶数据,step++,index--
					H=H->next;
					stack1[index].next=NULL;
					index--;
					i=i;
					j=j+1;
					H->step++;
			//	}
			}
		}
		if(sp[i][j-1]==1)
			H->step++;
		//向上移动
		while(stack1[index].step>=4&&sp[i-1][j]==0&&sp[i][j+1]==1&&sp[i][j-1]==1)
		{
			if(sp[i+1][j]==1||sp[i+1][j]==0)
			{
				index++;
				i=i-1;
				j=j;
			
				stack1[index].step=stack1[index-1].step;
				stack1[index].next=H;
				H=&stack1[index];

				//如果下一步已经存在于栈中
				if(isExit(index,i,j)==1)
				{
					//并且它的其他三面都是1(墙壁)
					//if(sp[i+1][j]==1&&sp[i][j-1]==1&&sp[i-1][j]==1)
					//{
						//删除栈顶数据,step++,index--
						H=H->next;
						stack1[index].next=NULL;
						index--;
						i=i+1;
						j=j;
						H=H->next;
						stack1[index].next=NULL;
						index--;
						i=i-1;
						j=j;
						H->step++;
					//}
				}
			}
		}
		H->step++;
	}//end  while

	//输出迷宫,起点为(1,1)终点为(8,8)
	for(i=0;i<10;i++)
	{
		for(j=0;j<10;j++)
		{
			printf("%d\t",sp[i][j]);
		}
		printf("\n");
	}

	//遍历输出路径
	for(k=0;k<index+1;k++)
	{
		print(k);
	}
}//end  main



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> #include <graphics.h> #include <time.h> #include <conio.h> #define MAXSIZE 20 typedef struct { int x; int y; }PosType; typedef struct { int di1; int di2; int di3; int di4; }Direct; typedef int MazeElem; typedef struct { MazeElem color[MAXSIZE+2][MAXSIZE+2]; PosType start; PosType end; }MazeType; MazeType mazes; int unit,rows,columns,ratio; PosType offset; PosType NextPos( PosType curpos,int di) { switch(di) {case 1: curpos.y++;break; /*右*/ case 2: curpos.x++;break; /*下*/ case 3: curpos.y--;break; /*左*/ case 4: curpos.x--;break;/*上*/ } return curpos; } void Near(PosType start,PosType end) { int i=0; PosType curpos; while(++i<=4) { curpos=NextPos(start,i); if(curpos.x==end.x && curpos.y==end.y) { printf(" success!\n"); getch(); closegraph(); exit(0); } } } void direction(Direct *direct,PosType start,PosType end) { if(start.y<=end.y) { if(start.x<=end.x) { if(end.y-start.y<=end.x-start.x) { direct->di1=2; /*左*/ direct->di2=3; /*下*/ direct->di3=4; /*上*/ direct->di4=1; /*右*/ } else { direct->di1=1; direct->di2=2; direct->di3=3; direct->di4=4; } } else { if(end.y-start.y<=start.x-end.x) { direct->di1=4; /*上 */ direct->di2=1; /*右*/ direct->di3=3; /*左 */ direct->di4=2; /*下*/ } else { direct->di1=1; direct->di2=4; direct->di3=3; direct->di4=2; } } } else { if(start.x<=end.x) { if(start.y-end.y<=end.x-start.x) { direct->di1=2; direct->di2=3; direct->di3=1; direct->di4=4; } else { direct->di1=3; direct->di2=2; direct->di3=4; direct->di4=1; } } else { if(start.y-end.y<=start.x-end.x) { direct->di1=4; direct->di2=3; direct->di3=1; direct->di4=2; } else { direct->di1=3; direct->di2=4; direct->di3=2; direct->di4=1; } } } } void Init(void)/*图形初始化*/ { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc"); cleardevice(); } void CreateMaze() { int i,j; char m ; int gdriver, gmode; detectgraph(&gdriver, &gmode); registerbgidriver(EGAVGA_driver); clrscr(); setbkcolor(2); settextstyle(0,0,2); outtextxy(30,60,"《**This program is designed by heqiuyun**》" ); settextstyle(0,0,1); outtextxy(60,100,"Operate handbook:"); outtextxy(70,140,"1.Black square denotations unpassable;" ); outtextxy(70,180,"2.White square denotations passable;" ); outtextxy(70,220,"3.Ratio mens white squares&&black squares" ); outtextxy(70,260,"4.Input rows,colums and ratio "); scanf("{%d,%d,%d}",&rows,&columns,&ratio); if(rows>MAXSIZE || rows<1 || columns>MAXSIZE ||columns<1) { getchar(); clrscr(); outtextxy(30,60,"attention! "); outtextxy(60,110,"1.1<=rows<=MAXSIZE"); outtextxy(60,160,"2.1<=columns<=MAXSIZE"); outtextxy(60,210,"3.MAXSIZE is defined 20"); outtextxy(60,240," Please enter the \"enter\" key" ); } getchar(); rows=20; columns=20; ratio=2; for(i=1;i<=MAXSIZE;i++) for(j=1;j<=MAXSIZE;j++) mazes.color[i][j]=WHITE; for(i=1;i<=rows;i++) for(j=1;j<=columns;j++) if(random(3)%(ratio)) mazes.color[i][j]=BLACK; } void PointColor(PosType curpos) { setcolor(GREEN); setfillstyle(SOLID_FILL,mazes.color[curpos.y][curpos.x]); bar((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,(curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); rectangle((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,( curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); } void DrawMaze() { int i,j,k,m,n; PosType curpos; clrscr(); m=getmaxx(); n=getmaxy(); k=(m>n?n:m); unit=k/(MAXSIZE*2); m=m/unit; n=n/unit; offset.x=m/4; offset.y=n/4; for(i=1;i<=rows;i++) for(j=1;j<=columns;j++) { curpos.x=j; curpos.y=i; PointColor(curpos); } printf("\n red square denotations maze start;yellow square denotations maze end.\n define maze start and end using (rowth,columnth).\n for example (2,3),(7,8) : "); scanf("(%d,%d),(%d,%d)",&mazes.start.y,&mazes.start.x,&mazes.end.y,&mazes.end.x); if(!mazes.start.y||!mazes.start.y||!mazes.end.y||!mazes.end.x) { mazes.start.y=1; mazes.start.x=1; mazes.end.y=20; mazes.end.x=20 ;} else if(mazes.start.x<1||mazes.start.x>columns||mazes.start.y<1||mazes.start.y>rows||mazes.end.x<1||mazes.end.x>columns||mazes.end.y<1||mazes.end.y>rows) { printf(" attention! 1<=x<=rows,1<=y<=columns, you define rows=%d,columns=%d",rows,columns); getch(); exit(0); } mazes.color[mazes.start.y][mazes.start.x]=RED; PointColor(mazes.start); sleep(1); mazes.color[mazes.end.y][mazes.end.x]=YELLOW; PointColor(mazes.end); sleep(1); i=0; while(++i<=4) { curpos=NextPos(mazes.end,i); if(mazes.color[curpos.y][curpos.x]==WHITE) break; } if(i==5) { printf(" maze end unpassable!\n"); getch(); exit(0); } } void MazePath(PosType start,PosType end) { Direct direct; if(start.x==end.x && start.y==end.y) { printf(" success!\n"); getch(); closegraph(); exit(0); } if(mazes.color[start.y][start.x]==RED ||mazes.color[start.y][start.x]==WHITE) { if(mazes.color[start.y][start.x]==WHITE) { mazes.color[start.y][start.x]=BLUE; PointColor(start); sleep(1); } Near(start,end); direction(&direct,start,end); MazePath(NextPos(start,direct.di1),end); MazePath(NextPos(start,direct.di2),end); MazePath(NextPos(start,direct.di3),end); MazePath(NextPos(start,direct.di4),end); if(mazes.color[start.y][start.x]!=RED) { mazes.color[start.y][start.x]=DARKGRAY; PointColor(start); sleep(1); } } } main() { Init(); CreateMaze(); DrawMaze(); MazePath(mazes.start,mazes.end); printf(" failed!\n"); getch(); closegraph(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值