poj3984广搜c语言

来源:互联网 发布:linux 升级内核好处 编辑:程序博客网 时间:2024/06/11 08:07
#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