C++ 布线问题,分支限界法

来源:互联网 发布:东北经济振兴 知乎 编辑:程序博客网 时间:2024/06/16 09:29
  1. #include <iostream>  
  2. #include <queue>  
  3. using namespace std;  
  4. int m=8;  
  5. int n=8;  
  6. int grid[10][10];  
  7. int indexcount=0;  
  8. struct Position  
  9. {  
  10.     int row;  
  11.     int col;  
  12. };  
  13. void showPath()  
  14. {  
  15.     for(int i=0; i<10; i++)  
  16.     {  
  17.   
  18.         for(int j=0; j<10; j++)  
  19.             cout<<grid[i][j]<<" ";  
  20.         cout<<endl;  
  21.     }  
  22.     cout<<"------------------"<<endl;  
  23. }  
  24. bool FindPath(Position start,Position finish,int &PathLen,Position  *&path)  
  25. {  
  26.     //计算从起点位置start到目标位置finish的最短布线路径,找到最短布线路//径则返回true,否则返回false  
  27.   
  28.     if((start.row==finish.row) && (start.col==finish.col))  
  29.     {  
  30.         PathLen=0;  
  31.         cout<<"start=finish"<<endl;  
  32.         return true;  
  33.     } //start=finish  
  34. //设置方格阵列“围墙”  
  35. //初始化图,-1为未访问  
  36.     for(int i=1; i<9; i++)  
  37.     {  
  38.         for(int j=1; j<9; j++)  
  39.             grid[i][j]=-1;  
  40.   
  41.     }  
  42.     //添加阻挡点  
  43.      grid[2][3]=-2;  
  44.     for(int i=0; i<= m+1; i++)  
  45.         grid[0][i]=grid[n+1][i]=-2; //顶部和底部  
  46.     for(int i=0; i<= n+1; i++)  
  47.         grid[i][0]=grid[i][m+1]=-2; //左翼和右翼  
  48. //初始化相对位移  
  49.     cout<<"完整图"<<endl;  
  50.     showPath();  
  51.     Position offset[4];  
  52.     offset[0].row=0;  
  53.     offset[0].col=1;//右  
  54.     offset[1].row=1;  
  55.     offset[1].col=0;//下  
  56.     offset[2].row=0;  
  57.     offset[2].col=-1;//左  
  58.     offset[3].row=-1;  
  59.     offset[3].col=0;//上  
  60.     int NumOfNbrs=4;//相邻方格数  
  61.     Position here,nbr;  
  62.     here.row=start.row;  
  63.     here.col=start.col;  
  64.     grid[start.row][start.col]=0;  
  65. //标记可达方格位置  
  66. cout<<"布线前图"<<endl;  
  67.     showPath();  
  68.   
  69.     queue<Position> Q;  
  70.     do  //标记相邻可达方格  
  71.     {  
  72.         for(int I=0; I<NumOfNbrs; I++)  
  73.         {  
  74.             nbr.row=here.row + offset[I].row;  
  75.             nbr.col=here.col+offset[I].col;  
  76.             if(grid[nbr.row][nbr.col]==-1)  
  77.             {  
  78. //该方格未被标记  
  79.                 //cout<<grid[nbr.row][nbr.col]<<endl;//显示路标值  
  80.                 grid[nbr.row][nbr.col]=grid[here.row][here.col]+1;  
  81.                  //cout<<nbr.col<<"   "<<nbr.row<<endl;//显示坐标  
  82.             }  
  83.             if((nbr.row==finish.row) &&(nbr.col==finish.col)) break//完成布线  
  84.             Q.push(nbr);  
  85.   
  86.   
  87.         }  
  88. //是否到达目标位置finish?  
  89.         if((nbr.row==finish.row)&&(nbr.col==finish.col)) break;//完成布线  
  90. //活结点队列是否非空?  
  91.         if(Q.empty()) return false;//无解  
  92.         here = Q.front();  
  93.         //cout<<here.col<<" "<<here.row<<endl;  
  94.         Q.pop();//取下一个扩展结点  
  95.   
  96.   
  97.         indexcount++;  
  98.        // cout<<"下一节点"<<indexcount<<endl;  
  99.     }  
  100.     while(true);  
  101. //构造最短布线路径  
  102.     PathLen=grid[finish.row][finish.col];  
  103.     path=new Position[PathLen];  
  104. //从目标位置finish开始向起始位置回溯  
  105.     here=finish;  
  106.     for(int j=PathLen-1; j>=0; j--)  
  107.     {  
  108.         path[j]=here;  
  109. //找前驱位置  
  110.         for(int i=0; i<NumOfNbrs; i++)  
  111.         {  
  112.             nbr.row=here.row+offset[i].row;  
  113.             nbr.col=here.col+offset[i].col;  
  114.             if(grid[nbr.row][nbr.col]==j)  
  115.             {  
  116.                // cout<<j<<endl;  
  117.                 break;  
  118.             }  
  119.         }  
  120.         here=nbr;//向前移动  
  121.     }  
  122.     return PathLen;  
  123. }  
  124. int main()  
  125. {  
  126.     Position start;  
  127.     start.col=1;  
  128.     start.row=1;  
  129.  cout<<"布线起点"<<endl;  
  130.  cout<<start.col<<" "<<start.row<<endl;  
  131.     Position finish;  
  132.     finish.row=3;  
  133.     finish.col=4;  
  134.      cout<<"布线结束点"<<endl;  
  135.       cout<<finish.col<<" "<<finish.row<<endl;  
  136.     int PathLen=0;  
  137.     Position *path;  
  138.   
  139.     FindPath(start,finish,PathLen,path);  
  140.     cout<<"布线后路径图"<<endl;  
  141.     showPath();  
  142.     cout<<"路径"<<endl;  
  143.     for(int i=0; i<PathLen; i++)  
  144.     {  
  145.         cout<<path[i].col<<" "<<path[i].row<<endl;  
  146.     }  
  147.     cout << "布线问题完毕!" << endl;  
  148.     return 0;  
  149. }  

0 0
原创粉丝点击