布线问题

  1 #include <iostream>
  2 #include <list>
  3 #include <stdlib.h>
  4 #define  n 6   //size of maze
  5 using namespace std;
  6 
  7 int grid[n+2][n+2];  //we set the border for the maze
  8  struct position  //this struct is used to record the path
  9 {
 10     int row;
 11     int col;
 12     position(int a, int b) //constructor function
 13     {
 14         row = a;
 15         col = b;
 16     }
 17 
 18     position()
 19     {
 20     }
 21     bool isEqualWith(position &another)  //determine whether two struction is equal
 22     {
 23         if(row==another.row&&col==another.col)
 24             return true;
 25         else
 26             return false;
 27     }
 28 };
 29 
 30 bool findPath(position start, position finish, int &PathLen, position *&path)  //pathlen is the len of the path, path is the array record path;
 31 {
 32     if(start.isEqualWith(finish))  //if the atart point is equal to finish point, we end the function
 33     {
 34         PathLen=0;
 35         cout << "start point is the same as end point";
 36         return true;
 37     }
 38 
 39     for(int i=0; i<=n+1; i++)  //set wall for maze
 40     {
 41         grid[0][i] = grid[n+1][i] = -2;
 42     }
 43     for(int i=0; i<=n+1; i++)
 44     {
 45         grid[i][0] = grid[i][n+1] = -2;
 46     }
 47 
 48     position offset[4];
 49     offset[0].col = 1;
 50     offset[0].row = 0;  //the direction is right
 51     offset[1].col = 0;
 52     offset[1].row=1;  //the direction is down
 53     offset[2].col = -1;
 54     offset[2].row = 0;  //left
 55     offset[3].col = 0;
 56     offset[3].row = -1;  //up
 57 
 58     int numofNbr = 4;  //the number of directins
 59     position here, nbr;  // here is the extension box, nbr is the extended square
 60     here.row = start.row; //we explore the target from the start point
 61     here.col = start.col;
 62     grid[start.row][start.col] = 0;  //the distance of start point is zero
 63     list <position> Q;  //queue
 64     while(true)
 65     {
 66         for(int i=0; i<numofNbr; i++)
 67         {
 68             nbr.row = here.row + offset[i].row;
 69             nbr.col = here.col + offset[i].col;
 70             if(grid[nbr.row][nbr.col]==-1)  //if the node can be extended, we push if into Q, and set the distance of it to one;
 71             {
 72              grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;  
 73               Q.push_back(nbr);
 74             }
 75             if(nbr.row==finish.row&&nbr.col==finish.col)  //when we find the target, we get out of tha circulation, i use the goto sentence, because the break sentence can only jump out one level circulation, the offical explanation for break statement is "A break causes the innermost enclosing loop or switch to be exited immediately."
 76                goto next;
 77         }
 78         if(Q.empty())
 79             return false;
 80         here = Q.front();
 81         Q.pop_front();
 82     }
 83 
 84     next: PathLen = grid[finish.row][finish.col];
 85     path = (position*)malloc(sizeof(position)*PathLen+1);  //the length of path is pathlen+1, because the position information we need to record is pethlen+1
 86     here = finish;
 87     for(int j=PathLen; j>=0; j--)
 88     {
 89         path[j]=here;
 90         for(int i=0; i<numofNbr; i++)
 91         {
 92             nbr.row = here.row + offset[i].row;
 93             nbr.col = here.col + offset[i].col;
 94             if(grid[nbr.row][nbr.col]==j-1)  //if the neighboring point value is equal to the value of now minus 0ne, the neighboring point is last point we need to find
 95                 break;
 96         }
 97         here = nbr;
 98     }
 99 
100     return true;
101 }
102 int main()
103 {
104     for(int i=0; i<n+2; i++)
105         for(int j=0; j<n+2; j++)
106     {
107         grid[i][j]=-1;
108     }
109 
110     grid[4][5]=-2;
111     int a;
112     position* path = NULL;
113     findPath(position(4,4), position(4,6), a, path);
114             for(int i=0; i<a+1; i++)
115     {
116         cout << path[i].row << " " << path[i].col << endl;
117     }
118     return 0;
119 }

 

转载于:https://www.cnblogs.com/1915884031A-qqcom/p/7695272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值