Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233508
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: C/C++

2017-02-19 23:09:41

一下程序使用队列,通过广度优先搜索算法。得到迷宫的最优解。目前只得到最短不数,还无法正确获取每一步的坐标
日后完善。

点击(此处)折叠或打开

  1. #include<stdio.h>


  2. typedef struct point {
  3.     int x;
  4.     int y;
  5.     int level;
  6. }Point;

  7. Point Queue[100];
  8. int head;
  9. int tail;
  10. Point data[100];
  11. int count = 0;

  12. Point data1[100];
  13. int count1 = 0;


  14. void Init_Queue()
  15. {
  16.     int i;
  17.     for (i = 0; i < 100; i++) {
  18.         Queue[i].x = 0;
  19.         Queue[i].y = 0;
  20.         Queue[i].level = 0;
  21.     }
  22.     head = 0;
  23.     tail = 0;
  24. }

  25. int IsEmpty(void) {
  26.     if (head == tail)
  27.         return 1;

  28.     return 0;
  29. }

  30. void Add_Queue_Tail(Point p)
  31. {
  32.     
  33.     Queue[tail] = p;
  34.     
  35.     tail++;

  36.     if (tail >= 100)
  37.         tail = 0;

  38.         
  39. }

  40. Point Del_Queue_Head() {
  41.     Point p;

  42.     p = Queue[head];
  43.     Queue[head].x = 0;
  44.     Queue[head].y = 0;
  45.     
  46.     if (count < 100) {
  47.         data[count] = p;
  48.         count++;
  49.     }
  50.     else {
  51.         printf("erro\n");
  52.     }

  53.     head++;
  54.     if (head >= 100)
  55.         head = 0;

  56.     return p;
  57. }

  58. void Disply_Queue() {
  59.     int i;

  60.     if (IsEmpty()) {
  61.         printf("Queue is empty\n");
  62.         return ;
  63.     }

  64.     printf("\n");
  65.     if (head < tail) {
  66.         for (i = head; i < tail; i++) {
  67.             printf("#%d (%d,%d) ", i, Queue[i].x, Queue[i].y);
  68.         }
  69.     }
  70.     else {
  71.         for (i = head; i < 100; i++) {
  72.             printf("#%d (%d,%d) ", i, Queue[i].x, Queue[i].y);
  73.         }
  74.         for (i = 0; i < tail; i++) {
  75.             printf("#%d (%d,%d) ", i, Queue[i].x, Queue[i].y);
  76.         }
  77.     }
  78.     printf("\n");
  79.     
  80. }


  81. int buff[5][5] = {
  82.     0, 1, 0, 0, 0,
  83.     0, 0, 0, 1, 0,
  84.     1, 0, 0, 0, 0,
  85.     0, 1, 0, 1, 0,
  86.     0, 0, 0, 1, 0
  87. };

  88. int visit[5][5];


  89. //四个方向
  90. int dir[4][2] = {
  91.     { 0, 1 },{ 1, 0 },
  92.     { 0, -1 },{ -1, 0 }
  93. };

  94. void Init_Visit(void)
  95. {
  96.     int i, j;
  97.     for (i = 0; i < 5; i++) {
  98.         for (j = 0; j < 5; j++) {
  99.             visit[i][j] = 0;
  100.         }
  101.     }
  102. }

  103. int Test(void)
  104. {
  105.     int i, x, y ,sum = 0,flag = 0;
  106.     int Cur_level = 1;
  107.     Point p, result;
  108.     
  109.     result.x = 4;
  110.     result.y = 4;


  111.     Init_Visit();
  112.     Init_Queue();
  113.     p.x = 0;
  114.     p.y = 0;
  115.     p.level = 1;

  116.     Add_Queue_Tail(p);

  117.     visit[p.x][p.y] = 1;

  118.     while (!IsEmpty()) {
  119.         p = Del_Queue_Head();

  120.         flag = 0;
  121.         for (i = 0; i < 4; i++) {
  122.             x = p.x + dir[i][0];
  123.             y = p.y + dir[i][1];
  124.             Cur_level = p.level;
  125.             if (x < 0 || x>4 || y < 0 || y>4) {
  126.                 if ((i == 3) && (flag == 0)) {
  127.                     if (count1 < 100) {
  128.                         data1[count1] = p;
  129.                         count1++;
  130.                     }
  131.                     else {
  132.                         printf("erro\n");
  133.                     }
  134.                 }
  135.                 continue;
  136.             }
  137.             if (x == 4 && y == 4) {

  138.                 printf("find it level =%d \n", p.level);
  139.                 return 1;
  140.             }

  141.             if ((buff[x][y] == 0) && (visit[x][y] != 1)) {
  142.                 p.x = x;
  143.                 p.y = y;
  144.                 p.level = Cur_level +1;
  145.                 Add_Queue_Tail(p);
  146.                 visit[x][y] = 1;
  147.                 flag = 1;
  148.             }

  149.                 if ((i == 3 )&&(flag == 0)) {
  150.                     if (count1 < 100) {
  151.                         data1[count1] = p;
  152.                         count1++;
  153.                     }
  154.                     else {
  155.                         printf("erro\n");
  156.                     }
  157.                 }
  158.             
  159.         }

  160.     }
  161.     printf("fail\n");
  162.     return 0;
  163. }

  164. void main(void)
  165. {
  166.     int i;
  167.     Test();


  168.     for (i = 0; i < count; i++)
  169.     {
  170.         printf(" (%d,%d) ", data[i].x, data[i].y);
  171.     }
  172.     printf("\n");
  173.     for (i = 0; i < count1; i++)
  174.     {
  175.         printf(" (%d,%d) ", data1[i].x, data1[i].y);
  176.     }
  177.     printf("\n");

  178.     printf("end\n");
  179.     while (1);
  180. }

阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~