练习12

 
  1. /***********************************************************************************
  2. 12. 下图是一个集装箱仓库,阴影部分表示有集装箱存放不能通过,无阴影处为临时通
  3.  道。当有人要从入口处到达出口处时,必须寻找可通过路线,请你找出可完成这个过程
  4.  的最方便(即用最短路线)到达出口处的路径。
  5.           ┎┰┰┰入口┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┰┒
  6.           ┠╂╂╂──╂╂╂╂┸┸╂┸┸╂┸┸╂┸┸╂╂╂╂┸┸╂╂╂┨
  7.           ┠╂╂╂──╂┸┸╂──╂┰┰╂┰┰╂──╂╂╂╂──╂╂╂┨
  8.           ┠╂╂╂──╂┰┰╂┰┰╂╂╂╂╂╂╂──╂┸┸╂──╂╂╂┨
  9.           ┠╂╂╂──╂╂╂╂╂╂╂╂╂╂╂╂╂┰┰╂┰┰╂┰┰╂╂╂┨
  10.           ┠╂╂╂──╂┸┸╂┸┸╂┸┸╂┸┸╂┸┸╂┸┸╂┸┸╂╂╂┨
  11.           ┠╂╂╂──╂┰┰╂┰┰╂┰┰╂──╂┰┰╂──╂┰┰╂╂╂┨
  12.           ┠╂╂╂──╂╂╂╂╂╂╂╂╂╂──╂╂╂╂──╂╂╂╂╂╂┨
  13.           ┠╂╂╂──╂╂╂╂┸┸╂┸┸╂──╂╂╂╂──╂┸┸╂╂╂┨
  14.           ┠╂╂╂──╂╂╂╂┰┰╂┰┰╂┰┰╂╂╂╂┰┰╂──╂╂╂┨
  15.           ┖┸┸┸──┸┸┸┸┸┸┸┸┸┸┸┸┸┸┸┸┸┸┸出口┸┸┸┚
  16.            输入:入口:(1,2) 出口:(10,9)
  17.            输出:路径
  18.            方法: 深度优先,广度优先
  19. ************************************************************************************/
  20. //#include <stdio.h>
  21. #include <queue>
  22. using namespace std;
  23. #define ENDDIRT 5
  24. struct Node
  25. {
  26.     int row;
  27.     int col;
  28. };
  29. int dr[4] = {-1,0,1,0};
  30. int dc[4] = {0,1,0,-1};
  31. int maze[10][10] = {
  32.     1,0,1,1,1,1,1,1,1,1,
  33.     1,0,1,0,0,0,0,1,0,1,
  34.     1,0,0,0,1,1,0,1,0,1,
  35.     1,0,1,1,1,1,0,0,0,1,
  36.     1,0,1,1,1,1,1,1,1,1,
  37.     1,0,0,0,0,0,0,0,0,1,
  38.     1,0,1,1,1,0,1,0,1,1,
  39.     1,0,1,1,1,0,1,0,1,1,
  40.     1,0,1,0,0,0,1,0,0,1,
  41.     1,0,1,1,1,1,1,1,0,1}; 
  42. static int dirt[12][12];
  43. void PrintPath(Node start)
  44. {
  45.     int direct;
  46.     Node cur = start;
  47.     printf("(%d,%d)",cur.row,cur.col);
  48.     while((direct=dirt[cur.row][cur.col]) != ENDDIRT)
  49.     {
  50.         cur.row = cur.row - dr[direct];
  51.         cur.col = cur.col - dc[direct];
  52.         printf(" -> (%d,%d)",cur.row,cur.col);
  53.     }
  54.     printf("/n");
  55. }
  56. void main()
  57. {
  58.     int row,col;
  59.     int map[12][12];
  60.     Node cur;
  61.     queue<Node> q;
  62.     Node start = {1,2};
  63.     Node end = {10,9};
  64.     int flag = 0;
  65.     for(row=0; row<12; row++)
  66.     {
  67.         for(col=0; col<12; col++)
  68.             if(row==0 || col==0 || row==11 || col==11)
  69.                 map[row][col] = 1;
  70.             else map[row][col] = maze[row-1][col-1];
  71.     }
  72.     q.push(end);
  73.     dirt[end.row][end.col] = ENDDIRT;
  74.     
  75.     while(!q.empty())
  76.     {
  77.         cur = q.front();
  78.         //走过的路要屏蔽掉
  79.         map[cur.row][cur.col] = 1;
  80.         q.pop();
  81.         if(cur.row == start.row && cur.col == start.col)
  82.         {
  83.             flag = 1;
  84.             break;
  85.         }
  86.         else
  87.         {
  88.             int i;
  89.             Node temp;
  90.             for(i=0; i<4; i++)
  91.             {
  92.                 temp.row = cur.row + dr[i];
  93.                 temp.col = cur.col + dc[i];
  94.                 if(map[temp.row][temp.col] == 0)
  95.                 {
  96.                     q.push(temp);
  97.                     dirt[temp.row][temp.col] = i;
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     if(flag == 1)
  103.     {
  104.         //输出路径
  105.         printf("Have path!/n");
  106.         PrintPath(start);
  107.     }else
  108.     {
  109.         printf("No path!/n");
  110.     }
  111.     
  112. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值