迷宫(伪代码)

#include<stdio.h>

#define MAX_ROW 5
#define MAX_COL 5

struct point {int row, col,pre} queue [512];
int head,tail=0;

void enqueue(struct point p)
{
tail++;
queue[tail]=p;
}

struct point dequeque(void)
{
head++;
return queue[head-1];
}

int is_empty()
{
return head==tail;
}



/***maze***/
int maze[MAX_ROW][MAX_COL] = {
	0, 1, 0, 0, 0,
	0, 1, 0, 1, 0,
	0, 0, 0, 0, 0,
	0, 1, 1, 1, 0,
	0, 0, 0, 1, 0,
};


void visit(int row,int col)
{
struct point p={row,col,head-1};
maze[row][col]=2;
enqueue(p);
}
void pirnt_maze()
{
int i,j;
for(i=0;i<MAX_ROW;i++){
	for(j=0;j<MAX_COL;j++){
		printf("%d ",maze[i][j]);
	}
	printf("\n");
}
printf("****************");
}
		
		

int main(void)
{
	/*base case*/
	struct point p={0,0,-1};
	enqueue(p);
	maze[p.row][p.col]=2;
	
	/*search*/
	while(!is_empty()){
		p=dequeue();
		if(p.row==MAX_ROW&&p.col==MAX_COL)
			break;
	
		if(p.col<MAX_COL-1&&maze[p.row][p.col+1]==0)
			visit(p.row,p.col+1);
		if(p.col>0&&maze[p.row][p.col-1]==0)
			visit(p.row,p.col-1);
		if(p.row>0&&maze[p.row-1][p.col]==0)
			visit(p.row-1,p.col);
		if(p.row<MAX_ROW-1&&maze[p.row+1][p.col]==0)
			visit(p.row+1,p.col);
		print_maze();
	}
	/*print result*/
	if(p.row==MAX_ROW-1&&p.col==MAX_COL-1){
	while(p.pre!=-1){
		p=queue[p.pre];
		printf("(%d,%d)\n",p.row,p.col);
	}		
	}else{
		printf("not path");
	}
	return 0;
}





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值