烤红薯配鱿鱼丝 2016-06-08 10:56 采纳率: 0%
浏览 3757

算法分析:电路板排列(分支限界法)

#include <**iostream**>
#include <**fstream**>
#include <**algorithm**>
#include <**functional**>
#include <**queue**>
using namespace std;
class BoardNode{
public:
int *x;//x[1:n]记录电路板排列
int n;
int t;//x[1:t]是当前节点所相应的部分排列
int maxL;//x[1:s]的最大长度
BoardNode(){};
BoardNode(BoardNode const &source){
n = source.n;
t = source.t;
maxL = source.maxL;
x = new int[n + 1];
for (int i = 0; i <= n; ++i)
x[i] = source.x[i];
}
bool Comput(int i,int m,int **B);//计算本块所形成的排列maxL
bool operator < (const BoardNode &A)const{
if (this->t > A.t)
return true;
else if(this->t == A.t){
return this->maxL < A.maxL ? true : false;
}
else
return false;
}
};
bool BoardNode::Comput(int i,int m,int **B){
++this->t;
swap(x[t],x[i]);
int *low = new int[m + 1];
int *high = new int[m + 1];
for (int j = 0; j <= m; ++j){
low[j] = m;
high[j] = 0;
}
for (j = 1; j <= t; ++j){
for (int k = 1; k <= m; ++k){
if (B[x[j]][k]){
if (j < low[k])
low[k] = j;
if (j > high[k])
high[k] = j;
}
}
}
maxL = 0;
for (j = 1; j <=t ;++j){
if (maxL < high[j] - low[j])
maxL = high[j] - low[j];
}
delete low;
delete high;
return true;
}
//////////////////////////////////
class minBorad{
private:
int **B;//描述矩阵
int n;//n块电路板
int m;//m个连接块

int *bestx;//bestx[1:n]最优排列
int bestMaxL;//最优最大长度
BoardNode E;
priority_queue Heap;
void BBArrangement();
void cmpBest(BoardNode &newBoardNode);
public:
minBorad(){};
minBorad(int **B, int n, int m){
this->B = B;
this->n = n;
this->m = m;
bestx = new int[n + 1];
bestMaxL = INT_MAX;
E.x = new int[n + 1];
E.maxL = 0;
E.n = n;
E.t = 0;
for (int i = 0; i <= n; ++i)
{
E.x[i] = i;
}
BBArrangement();
}
int getBestMaxL(){return this->bestMaxL;}
int *getBestx(){return this->bestx;}
};
void minBorad::cmpBest(BoardNode &newBoardNode){
if (newBoardNode.maxL <= this->bestMaxL)
{
this->bestMaxL = newBoardNode.maxL;
cout<<"当前最优:"< for (int i = 0; i {
this->bestx[i] = newBoardNode.x[i];
cout<<bestx[i]<<" ";
}
cout<<endl;
}
}
void minBorad::BBArrangement(){
Heap.push(E);
while(!Heap.empty()){
BoardNode nowNode(Heap.top());
Heap.pop();
// cout<<"弹出节点 "<<nowNode.t<<endl;
// for (int z = 0; z <= n; ++z)
// {
// cout<<nowNode.x[z]<<" ";
// }
// cout<<endl;
for (int i = nowNode.t + 1; i <= n; ++i)
{
BoardNode newBoardNode(nowNode);
newBoardNode.Comput(i,m,B);
if (newBoardNode.maxL < bestMaxL)//如果当前最大长度小于最优长度则搜索子树
{
if (newBoardNode.t == n)
{
cmpBest(newBoardNode);
}
else{
Heap.push(newBoardNode);
// cout<<"加入节点 "<<newBoardNode.t<<endl;
// for (int z = 0; z <= n; ++z)
// {
// cout<<newBoardNode.x[z]<<" ";
// }
// cout<<endl;
}
}
}

}

};
int main(){
ifstream in("input.txt");
ofstream out("output.txt");
//#define in cin
//#define out cout
int cases;
in>>cases;
for(int Case = 1; Case <= cases; ++Case){
int n,m;
in>>n>>m;
int **B = new int *[n + 1];
int i,j;
for (i = 1; i <= n; ++i)
{
B[i] = new int [m];
}
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m; ++j)
{
in>>B[i][j];
}
}
minBorad cmpMinBorad(B,n,m);
out<<"Case #"<<Case<<": "<<cmpMinBorad.getBestMaxL()<<endl;
for (j = 1; j <= n; ++j)
{
out<<(cmpMinBorad.getBestx())[j]<<" ";
}
out<<endl;
}
return 0;
}
input问文本输入
8 5
11111
01010
01110
10110
10100
11010
00001
01001
output输出的结果不对为什么啊???还有算法复杂度是多少啊?怎么算的

  • 写回答

2条回答 默认 最新

  • devmiao 2016-06-08 15:10
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog