分支限界法

分支限界法之布线问题

 

一、要求:

    1、输入电路板区域n*m以及布线的起始位置和结束位置;

    2、输出布线方案;

    3、可以使用c或者vc实现

    二、问题分析及实验原理:

    在n*m的方格阵列中存在封锁区域(布线时必须绕开的区域),找到起始位置到目标位置的最短路径。从目标位置开始向起始位置回溯,逐步构造最优解。每次向标记距离比当前方格标记距离少1的相邻方格移动,直到到达起始方格为止。

    三、算法程序源代码:

    #include <iostream>

    #include<stdio.h>

    using namespace std;

    typedef struct

    {

    int row ;

    int col ;

    }Position;

    typedef struct

    {

    //struct Position;

    int row[10000] ;

    int col[10000] ;

    int end;

    int begin ;

    }Queue;

    int grid[100][100];

    Position start, finish;

    int PathLen = 0;

    Position * path;

    int n , m , a , b , x ;

    bool FindPath(Position start,Position finish)

    {//计算从起点位置start到目标位置finish的最短布线路径,找到最短布线路//径则返回true,否则返回false

    if((start.row==finish.row) && (start.col==finish.col))

    {

    PathLen=0;

    return true;

    } //start=finish

    //设置方格阵列“围墙”

    int i ;

    for( i=0; i<= m+1; i++)

    grid[0][i]=grid[n+1][i]=1; //顶部和底部

    for( i=0; i<= n+1; i++)

    grid[i][0]=grid[i][m+1]=1; //左翼和右翼

    int j ;

    //初始化相对位移

    Position offset[4];

    offset[0].row=0; offset[0].col=1;//右

    offset[1].row=1; offset[1].col=0;//下

    offset[2].row=0; offset[2].col=-1;//左

    offset[3].row=-1; offset[3].col=0;//上

    int NumOfNbrs=4;//相邻方格数

    Position here,nbr;

    here.row=start.row;

    here.col=start.col;

    grid[start.row][start.col]=2;

    //标记可达方格位置

    //LinkedQueue<Position> Q;

    Queue Q ;

    Q.end = 0 ;

    Q.begin = 0 ;

    do {//标记相邻可达方格

    for( i=0; i<NumOfNbrs; i++)

    {

    nbr.row=here.row + offset[i].row;

    nbr.col=here.col+offset[i].col;

    if(grid[nbr.row][nbr.col]==0)

    {

    //该方格未被标记

    grid[nbr.row][nbr.col]=grid[here.row][here.col]+1;

    if((nbr.row==finish.row) &&(nbr.col==finish.col))

    break; //完成布线

    //Q.Add(nbr);

    Q.col[Q.end] = nbr.col;

    Q.row[Q.end] = nbr.row;

    Q.end++;

    }

    }

    //是否到达目标位置finish?

    if((nbr.row==finish.row)&&(nbr.col==finish.col)) break;//完成布线

    //活结点队列是否非空?

    if(Q.begin==Q.end) return false;//无解

    else

    {

    here.row=Q.row[Q.begin];

    here.col=Q.col[Q.begin];

    Q.begin++;//取下一个扩展结点

    }

    }while(true);

    //构造最短布线路径

    PathLen=grid[finish.row][finish.col]-2;

    path=new Position[PathLen];

    //从目标位置finish开始向起始位置回溯

    here=finish;

    for( j = PathLen-1; j>=0; j--){

    path[j]=here;

    //找前驱位置

    for( i=0; i<NumOfNbrs; i++){

    nbr.row=here.row+offset[i].row;

    nbr.col=here.col+offset[i].col;

    if(grid[nbr.row][nbr.col]==j+2) break;

    }

    here=nbr;//向前移动

    }

    return true;

    }

    int main()

    {

    int j ;

    printf("输入电路板区域n*m和封锁的格数x:\n");

    cin>>n>>m>>x;

    printf("输入封锁的坐标:\n");

    for( a = 0 ; a < n+2 ; a ++ )

    for( b = 0 ; b < m +2 ; b ++ )

    grid[a][b] = 0 ;

    for( x = x ; x >= 1 ; x -- )

    {

    cin >> a >> b ;

    grid[a][b]= 1;

    }

    printf("输入起始位置和结束位置:\n");

    cin>>start.row>>start.col>>finish.row>>finish.col;

    if(FindPath(start,finish))

    {

    printf("输出路径长度及途中坐标:");

    cout<<PathLen<<endl;

    cout<<start.row<<" "<< start.col<<endl;

    for( j = 0 ; j < PathLen ; j++ )

    cout<<path[j].row<<" "<<path[j].col<<endl;

    }

    else cout<<"No Solution!"<<endl;

    delete []path;

    return 0 ;

    }

 

来自:http://c.chinaitlab.com/example/794375.html

#include #include #include #include using namespace std; ifstream infile; ofstream outfile; class Node { friend int func(int*, int, int, int*); public: int ID; double weight;//物品的重量 }; bool comp1(Node a, Node b) //定义比较规则 { return a.weight > b.weight; } class Load; class bbnode; class Current { friend Load; friend struct Comp2; private: int upweight;//重量上界 int weight;//结点相应的重量 int level;//活结点在子集树中所处的层次 bbnode* ptr;//指向活结点在子集树中相应结点的指针 }; struct Comp2 { bool operator () (Current *x, Current *y) { return x->upweightupweight; } }; class Load { friend int func(int*, int, int, int*); public: int Max0(); private: priority_queue<Current*, vector, Comp2>H;//利用优先队列(最大堆)储存 int limit(int i); void AddLiveNode(int up, int cw, bool ch, int level); bbnode *P;//指向扩展结点的指针 int c;//背包的容量 int n;//物品的数目 int *w;//重量数组 int cw;//当前装载量 int *bestx;//最优解方案数组 }; class bbnode { friend Load; friend int func( int*, int, int, int*); bbnode* parent; bool lchild; }; //结点中有双亲指针以及左儿子标志 int Load::limit(int i) //计算结点所相应重量的上界 { int left,a; left= c - cw;//剩余容量 a = cw; //b是重量上界,初始值为已经得到的重量 while (i <= n && w[i] parent = P; b->lchild = ch; Current* N = new Current; N->upweight = up; N->weight = cw; N->level = level; N->ptr = b; H.push(N); } int Load::Max0() { int i = 1; P = 0; cw = 0; int bestw = 0; int up = limit(1); while (i != n + 1) { int wt = cw + w[i]; //检查当前扩展结点的左儿子结点 if (wt bestw) bestw =wt; AddLiveNode(up,wt, true, i + 1); } up = limit(i + 1); //检查当前扩展结点的右儿子结点 if (up >= bestw)//如果右儿子可行 { AddLiveNode(up,cw, false, i + 1); } Current* N = H.top(); //取队头元素 H.pop(); P = N->ptr; cw = N->weight; up = N->upweight; i = N->level; } bestx = new int[n + 1]; for (int j = n; j > 0; --j) { bestx[j] = P->lchild; P = P->parent; } return cw; } int func(int *w, int c, int n, int *bestx) //调用Max0函数对子集树的优先队列式进行分支限界搜索 { int W = 0; //初始化装载的总质量为0 Node* Q = new Node[n]; for (int i = 0; i < n; ++i) { Q[i].ID = i + 1; Q[i].weight = w[i+1]; W += w[i+1]; } if (W <= c)//如果足够装,全部装入 return W; sort(Q, Q + n, comp1); //首先,将各物品按照重量从大到小进行排序; Load K; K.w = new int[n + 1]; for (int j = 0; j < n; j++) K.w[j + 1] = w[Q[j].ID]; K.cw = 0; K.c = c; K.n = n; int bestp = K.Max0(); for (int k = 0; k < n; k++) { bestx[Q[k].ID] = K.bestx[k + 1]; } delete []Q; delete []K.w; delete []K.bestx; return bestp; } int main() { int*w,*Final; int c,n,i,best; infile.open("input.txt",ios::in); if(!infile) { cerr<<"open error"<>c; infile>>n; w=new int[n+1]; for(i=1;i>w[i]; infile.close(); Final = new int[n+1]; best = func( w, c, n, Final); outfile.open("output.txt",ios::out); if(!outfile) { cerr<<"open error"<<endl; exit(1); } outfile << best << endl; for (int i = 1; i <= n; ++i) { outfile<<Final[i]<<" "; } outfile.close(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值