您的位置:首页 > 编程语言 > C语言/C++

poj3984广搜c语言

2015-08-17 00:22 381 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 poj