国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言使用廣度優先搜索算法解決迷宮問題(隊列)

2020-01-26 13:55:28
字體:
來源:轉載
供稿:網友

本文實例講述了C語言使用廣度優先搜索算法解決迷宮問題。分享給大家供大家參考,具體如下:

變量 head 和 tail 是隊頭和隊尾指針, head 總是指向隊頭, tail 總是指向隊尾的下一個元素。每個點的 predecessor 成員也是一個指針,指向它的前趨在 queue 數組中的位置。如下圖所示:

廣度優先是一種步步為營的策略,每次都從各個方向探索一步,將前線推進一步,圖中的虛線就表示這個前線,隊列中的元素總是由前線的點組成的,可見正是隊列先進先出的性質使這個算法具有了廣度優先的特點。廣度優先搜索還有一個特點是可以找到從起點到終點的最短路徑,而深度優先搜索找到的不一定是最短路徑。

#include <stdio.h>#define MAX_ROW 5#define MAX_COL 5struct point { int row, col, predecessor; } queue[512];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] = {  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 print_maze(void){  int i, j;  for (i = 0; i < MAX_ROW; i++) {    for (j = 0; j < MAX_COL; j++)    printf("%d ", maze[i][j]);    putchar('/n');  }  printf("*********/n");}void visit(int row, int col){  struct point visit_point = { row, col, head-1 };  maze[row][col] = 2;  enqueue(visit_point);}int main(void){  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 /* goal */    && p.col == MAX_COL - 1)    break;    if (p.col+1 < MAX_COL /* right */    && maze[p.row][p.col+1] == 0)    visit(p.row, p.col+1);    if (p.row+1 < MAX_ROW /* down */    && maze[p.row+1][p.col] == 0)    visit(p.row+1, p.col);    if (p.col-1 >= 0 /* left */    && maze[p.row][p.col-1] == 0)    visit(p.row, p.col-1);    if (p.row-1 >= 0 /* up */    && maze[p.row-1][p.col] == 0)    visit(p.row-1, p.col);    print_maze();  }  if (p.row == MAX_ROW - 1 && p.col == MAX_COL - 1)  {    printf("(%d, %d)/n", p.row, p.col);    while (p.predecessor != -1) {      p = queue[p.predecessor];      printf("(%d, %d)/n", p.row, p.col);    }  } else  printf("No path!/n");  return 0;}

運行結果如下:

[root@localhost arithmetic]# ./maze2.out2 1 0 0 02 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0*********2 1 0 0 02 1 0 1 02 0 0 0 00 1 1 1 00 0 0 1 0*********2 1 0 0 02 1 0 1 02 2 0 0 02 1 1 1 00 0 0 1 0*********2 1 0 0 02 1 0 1 02 2 2 0 02 1 1 1 00 0 0 1 0*********2 1 0 0 02 1 0 1 02 2 2 0 02 1 1 1 02 0 0 1 0*********2 1 0 0 02 1 2 1 02 2 2 2 02 1 1 1 02 0 0 1 0*********2 1 0 0 02 1 2 1 02 2 2 2 02 1 1 1 02 2 0 1 0*********2 1 0 0 02 1 2 1 02 2 2 2 22 1 1 1 02 2 0 1 0*********2 1 2 0 02 1 2 1 02 2 2 2 22 1 1 1 02 2 0 1 0*********2 1 2 0 02 1 2 1 02 2 2 2 22 1 1 1 02 2 2 1 0*********2 1 2 0 02 1 2 1 22 2 2 2 22 1 1 1 22 2 2 1 0*********2 1 2 2 02 1 2 1 22 2 2 2 22 1 1 1 22 2 2 1 0*********2 1 2 2 02 1 2 1 22 2 2 2 22 1 1 1 22 2 2 1 0*********2 1 2 2 02 1 2 1 22 2 2 2 22 1 1 1 22 2 2 1 2*********2 1 2 2 22 1 2 1 22 2 2 2 22 1 1 1 22 2 2 1 2*********2 1 2 2 22 1 2 1 22 2 2 2 22 1 1 1 22 2 2 1 2*********(4, 4)(3, 4)(2, 4)(2, 3)(2, 2)(2, 1)(2, 0)(1, 0)(0, 0)

希望本文所述對大家C語言程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 霍山县| 施秉县| 鹿泉市| 雷州市| 南涧| 阳城县| 政和县| 宜都市| 缙云县| 鄢陵县| 孝义市| 宕昌县| 泾川县| 化德县| 宝鸡市| 东安县| 韩城市| 漳平市| 郧西县| 宁阳县| 监利县| 安溪县| 蓝山县| 丹寨县| 贺州市| 光山县| 杨浦区| 奎屯市| 新乡县| 镇安县| 彭阳县| 绵阳市| 神农架林区| 沂源县| 黄冈市| 平南县| 濉溪县| 夏河县| 吉安县| 车致| 尉氏县|