poj3984广搜c语言

#include <stdio.h>//输入5*5迷宫,输出路径
#define MAX_ROW 5  
#define MAX_COL 5  
struct point { int row, col, predecessor; } queue[512],p;  
struct node{int x,y;}arr[50];
int head = 0, tail = 0;  
void enqueue(struct point p)  
{  
    queue[tail++] = p;  
}  
struct point dequeue(void)  
{  
    return queue[head++];  
}  
int is_empty(void)  
{  
    return head == tail;  
}  
int maze[MAX_ROW][MAX_COL];
void visit(int row, int col)  
{  
    struct point visit_point = { row, col, head-1 };  
    maze[row][col] = 2;  
    enqueue(visit_point);  
}  
int main(void)  
{  
	int i,j;
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
			scanf("%d",&maze[i][j]);
		struct point p={0,0,-1};
    maze[p.row][p.col] = 2; 
    enqueue(p);
    while (!is_empty()) {  
        p = dequeue();  
        if (p.row == MAX_ROW - 1 
        && p.col == MAX_COL - 1)  
        break;  
        if (p.col+1 < MAX_COL 
        && maze[p.row][p.col+1] == 0)  
        visit(p.row, p.col+1);  
        if (p.row+1 < MAX_ROW  
        && maze[p.row+1][p.col] == 0)  
        visit(p.row+1, p.col);
        if (p.col-1 >= 0  
        && maze[p.row][p.col-1] == 0)
        visit(p.row, p.col-1);
        if (p.row-1 >= 0
        && maze[p.row-1][p.col] == 0)
        visit(p.row-1, p.col);
    }
	i=1;
	arr[0].x=p.row, arr[0].y=p.col;  
    while (p.predecessor != -1) {  
       p = queue[p.predecessor]; 
	   arr[i].x=p.row, arr[i].y=p.col;
	   i++;  
	}
	j=i;
	for(i=j-1;i>=0;i--)
		printf("(%d,%d)\n",arr[i].x,arr[i].y);
    return 0;  
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值