# 队列—布线问题

.转来的别人的，修改了其中一个小bug.

#include <iostream>
#include <queue>
using namespace std;
int m = 8;
int n = 8;
int grid[10][10];
int indexcount = 0;
struct Position
{
int row;
int col;
};
void showPath()
{
for (int i = 0; i<10; i++)
{
for (int j = 0; j<10; j++)
cout << grid[i][j] << " ";
cout << endl;
}
cout << "------------------" << endl;
}
bool FindPath(Position start, Position finish, int &PathLen, Position  *&path)
{
if ((start.row == finish.row) && (start.col == finish.col))
{
PathLen = 0;
cout << "start=finish" << endl;
return true;
}
for (int i = 1; i<9; i++)
{
for (int j = 1; j<9; j++)
grid[i][j] = -1;

}
grid[1][3] = -2;
grid[2][3] = -2;
grid[2][4] = -2;
grid[3][5] = -2;
grid[4][4] = -2;
grid[4][5] = -2;
grid[5][5] = -2;
for (int i = 0; i <= m + 1; i++)
grid[0][i] = grid[n + 1][i] = -2;
for (int i = 0; i <= n + 1; i++)
grid[i][0] = grid[i][m + 1] = -2;
cout << "完整图" << endl;
showPath();
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] = 0;

cout << "布线前图" << endl;
showPath();

queue<Position> Q;
do
{
for (int 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] == -1)
{
grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;
if ((nbr.row == finish.row) && (nbr.col == finish.col)) break;
Q.push(nbr);
}

}

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

if (Q.empty()) return false;
here = Q.front();
Q.pop();
indexcount++;
} while (true);

PathLen = grid[finish.row][finish.col];
path = new Position[PathLen];
here = finish;
for (int j = PathLen - 1; j >= 0; j--)
{
path[j] = here;
for (int 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)
{
break;
}
}
here = nbr;
}
return PathLen;
}
int main()
{
Position start;
start.row = 3;
start.col = 2;
cout << "布线起点" << endl;
cout << start.row << " " << start.col << endl;
Position finish;
finish.row = 4;
finish.col = 6;

cout << "布线结束点" << endl;
cout << finish.row << " " << finish.col << endl;
int PathLen = 0;
Position *path;

FindPath(start, finish, PathLen, path);
cout << "布线后路径图" << endl;
showPath();
cout << "路径" << endl;
for (int i = 0; i<PathLen; i++)
{
cout << path[i].row << " " << path[i].col << endl;
}
cout << "布线问题完毕!" << endl;
system("pause");
return 0;
}

#### 布线问题 队列式分支算法

2017-07-17 14:47:22

#### c++ 数据结构 队列应用之电路布线

2016-12-06 20:26:01

#### 算法复习-----------利用列队解决电路布线问题

2015-05-06 09:38:17

#### 电路布线问题

2016-12-29 12:24:50

#### 算法设计--电路布线问题（分支限界法求解）

2013-06-24 16:36:58

#### 队列布线问题C++代码

2009年04月04日 4KB 下载

#### 队列的应用—电路布线最优解问题

2013-02-05 20:39:29

#### 算法java实现--动态规划--电路布线问题

2014-04-25 10:01:53

#### 电路布线问题的几种动态规划算法

2017-04-01 09:10:03

#### 算法java实现--分支限界法--电路板排线问题

2014-05-26 09:51:54