布线问题

  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 }

 

posted @ 2017-10-19 21:28  diamondDemand  阅读(183)  评论(0编辑  收藏  举报