分支限界法 求电路布线的最短路径






#include
#include
#include
using namespace std;


//import the grid data;

ifstream fin("map.txt");//这个map是7行7列,请以这个为例子来理解这个程序


typedef struct Position
{
	int row;
	int col;


} Posi;



//find the shortest path for the grid

bool FindPath(Posi start,Posi finish,int & PathLen,int **&grid,Posi *& path,int n,int m)
{
	//if the start position is the finish position
	if((start.row == finish.row) && (start.col == finish.col))
	{
		PathLen = 0;
		return true;
	}

	Position offset[4];

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

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

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

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

	Posi here,nbr;

	here.row = start.row;
	here.col = start.col;

	int NumOfNbrs = 4;//ajacent position;


	grid[start.row][start.col] = 2;//init the start position's length  with value 2,

	queue Q;

	do
	{

		for(int firdex = 0;firdex < NumOfNbrs;firdex++)
		{
			nbr.row = here.row + offset[firdex].row;
			nbr.col = here.col + offset[firdex].col;

			if(grid[nbr.row][nbr.col] == 0)//this position haven't been visted
			{
				grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;
			

				if((nbr.row == finish.row) && (nbr.col == finish.col))//find the shortest path
				{
					break;
				}

				Q.push(nbr);
			}

		}


		if((nbr.row == finish.row) && (nbr.col ==finish.col))
		{
			break;//wiring was completed
		}


		if(Q.empty())//if queue is empty
		{
			return false;//no result
		}



		here = Q.front();
		Q.pop();




	}while(true);



	//traceback the shortest path

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

	here = finish;

	for(int firdex = PathLen-1;firdex >=0;firdex--)
	{
		path[firdex] = here;

		for(int secdex = 0;secdex < NumOfNbrs;secdex++)
		{
			nbr.row = here.row + offset[secdex].row;
			nbr.col = here.col + offset[secdex].col;

			if(grid[nbr.row][nbr.col] == firdex+2)//It is the nbr's grid that why the grid[nbr.row][nbr.col] can give index of "firdex+2"
			{
				break;
			}
		}

		here =nbr;//move to the previous node
	}


	return true;

}


//allocate memory for the grid
void InitGrid(int **&grid,int n,int m)
{
	grid = new int*[n+2];
	for(int firdex = 0;firdex < n+2;firdex++)
		grid[firdex] = new int[m+2];

	//set the bound

	for(int index = 0;index < m+2;index++)//top an bottom
	{
		grid[0][index] = grid[n+1][index]  =1;
	}

	for(int index = 0;index < n+2;index++)
	{
		grid[index][0] = grid[index][m+1] = 1;
	}

	for(int firdex = 1;firdex < n+1;firdex++)
	{
		for(int secdex = 1;secdex < m+1;secdex++)
			fin>>grid[firdex][secdex];
	}


}


//destroy the resource for the grid
void Destroy(int ** &grid,int n,int m)
{
	if(grid != NULL)
	{
		for(int firdex  = 0;firdex < n+2;firdex++)
		{
			delete [] grid[firdex];
			grid[firdex]  = NULL;
		}

		delete grid;
		grid = NULL;
	}

}



int main(void)
{

	int m = 0,n = 0;
	Posi start,finish;
	int PathLength = 0;

	Posi * path = NULL;

	int ** grid  = NULL;

	cout<<"Please input the m and n of the grid:"<>n>>m;

	cout<<"Please input the start position:"<";
	}

	cout<



分支限界法 求电路布线的最短路径_第1张图片 

 

 

 

 

你可能感兴趣的:(自己的小文章)