当前位置: 首页 > news >正文

菜篮网网站开发技术推广服务公司

菜篮网网站开发技术,推广服务公司,史志网站建设,一个主机域名可以做多少个网站《数据结构、算法与应用C语言描述》-队列的应用-电路布线问题 问题描述 在 迷宫老鼠问题中,可以寻找从迷宫入口到迷宫出口的一条最短路径。这种在网格中寻找最短路径的算法有许多应用。例如,在电路布线问题的求解中,一个常用的方法就是在布…

《数据结构、算法与应用C++语言描述》-队列的应用-电路布线问题

问题描述

在 迷宫老鼠问题中,可以寻找从迷宫入口到迷宫出口的一条最短路径。这种在网格中寻找最短路径的算法有许多应用。例如,在电路布线问题的求解中,一个常用的方法就是在布线区域设置网格,该网格把布线区域划分成nxm个方格,就像迷宫一样(如图 9-12a 所示)。一条线路从一个方格 a 的中心点连接到另一个方格 b 的中心点,转弯处可以采用直角,如图 9-12b 所示。已经有线路经过的方格被“封锁”,成为下一条线路的障碍。我们希望用a 和 b之间的最短路径来布线,以减少信号的延迟。
在这里插入图片描述

求解策略

a和b之间的最短路径需要在两个过程中确定。一个是距离标记过程,另一个是路径标记过程。在距离标记过程中,先从位置a开始,把从a可到达的相邻方格都标记为1(表示与a相距为1),然后把从编号为1的方格可到达的相邻方格都标记为2(表示与a相距为2)。这个标记过程继续下去,直至到达b或者没有可到达的相邻方格为止。图9-13a 显示了这种搜索过程的结果,其中 a=(3,2),b=(4,6)。图中的阴影部分是被封锁的方格。

一旦到达 b,b 的编号便是 b 与 a之间的距离(在图 9-13a 中,b上的标号为 9)。距离标记过程结束之后,路径标记过程开始。从方格 b 开始,首先移动到一个其编号比b的编号小1的相邻方格上。在图9-13a中,我们从b移到方格(5,6)。接下来,从方格(5,6)移到比当前编号小 1 的相邻位置上。重复这个过程,直至到达 a 为止。在图 9-13a 的例子中,从(5,6),然后移到(6,6)、(6,5)、(6,4)、(5,4),等等。图 9-13b 给出了所得到的路径,它是(3,2)和(4,6)之间的最短路径。注意,最短路径不是唯一的,(3,2)、(3,3)、(4,3)、(5,3)、(5,4)、(6,4)、(6,5)、(6,6)、(5,6)、(4,6)是另一条最短路径。
在这里插入图片描述

代码

#include <iostream>
#include <queue>
using namespace std;/*用于存储迷宫地址的结构体*/
struct Position
{int row,  //行col;  //列Position() {}Position(int prow, int pcol):row(prow),col(pcol){}operator int() const { return row; }friend ostream& operator<<(ostream& out, const Position x){out << "(" << x.row << "," << x.col << ")";return out;}
};
/*创建二维数组*/
template <class T>
bool make2dArray(T**& x, int numberOfRows, int numberOfColumns)
{try {//行指针x = new T * [numberOfRows];//为每一行分配内存for (int i = 0; i < numberOfRows; i++)x[i] = new int[numberOfColumns];return true;}catch (bad_alloc) { return false; }
}/*遍历二维数组*/
template<class T>
void traverse2dArray(T**& x, int numberOfRows, int numberOfColumns)
{for (int i = 0; i < numberOfRows; i++){for (int j = 0; j < numberOfColumns; j++){cout.width(4);cout << x[i][j] << "  ";}cout << endl;}
}/*电路布线问题全局变量*/
int** Grid;//二维方阵
int GridSize;//方阵大小
queue<Position> path;//记录路径的队列
/*电路布线---和迷宫老鼠问题有点像*/
/*方格元素为1表示为障碍,方格元素为0表示无障碍*/
/*方格标记为2表示是起点,方格标记为大于2的整数m表示该方格距离起点m-2步*/
void inputGridQueue()//输入迷宫
{cout << "Please enter the size of Grid-Matrix:";while (!(cin >> GridSize)){cin.clear();//清空标志位while (cin.get() != '\n')//删除无效的输入continue;cout << "Please enter the size of Grid-Matrix:";}//+2的原因是为了避免在处理内部位置和边界位置时存在差别make2dArray<int>(Grid, GridSize + 2, GridSize + 2);//初始化边界位置的数值for (int i = 0; i <= GridSize + 1; i++){Grid[i][0] = 1;Grid[0][i] = 1;Grid[i][GridSize + 1] = 1;Grid[GridSize + 1][i] = 1;}//初始化迷宫for (int i = 1; i <= GridSize; i++)for (int j = 1; j <= GridSize; j++){int positionij;cout << "Please enter Grid[" << i << "," << j << "]:";while (!(cin >> positionij)){cin.clear();//清空标志位while (cin.get() != '\n')//删除无效的输入continue;cout << "Please enter Grid[" << i << "," << j << "]:";}Grid[i][j] = positionij;}cout << "The Grid = " << endl;traverse2dArray<int>(Grid, GridSize + 2, GridSize + 2);
}
bool findPathQueue(Position start,Position end)//寻找从起点到终点的最短路径,如找到返回true,如没找到则返回false
{if ((start.row == end.row) && (start.col == end.col))return true;//初始化偏移量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;//上Position here = start;Grid[start.row][start.col] = 2;//标记起点为2int numOfNbrs = 4;//一个方格的相邻位置数/*标记各个方格*/queue<Position> q;//将需要标记的方格入栈qPosition nbr;//邻居方格while (true){//给相邻位置做标记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] == 0)//只有方格为0时表示方格无障碍和未标记{Grid[nbr.row][nbr.col] = Grid[here.row][here.col] + 1;if ((nbr.row == end.row) && (nbr.col == end.col))break;//如果标记到终点,则标记完成q.push(nbr);//把邻居结点插入队列,以备后面标记,也就是找邻居的邻居}}if ((nbr.row == end.row) && (nbr.col == end.col))break;//如果标记到终点,则标记完成if (q.empty())return false;//没有找到终点,则不可达,返回falsehere = q.front();//取队首元素作为下一节点,以备标记其邻居节点q.pop();//删除队首节点}/*标记完成,构造路径*/int pathLength = Grid[end.row][end.col] - 2;here = end;//从终点开始回溯for (int i = pathLength - 1; i >= 0; i--){path.push(here);for (int j = 0; j < numOfNbrs; j++)//在周边寻找父节点{nbr.row = here.row + offset[j].row;nbr.col = here.col + offset[j].col;if (Grid[nbr.row][nbr.col] == i + 2)break;}here = nbr;}path.push(start);return true;
}
void outputPathQueue()//输出路径
{int i = 0;cout << "The path = ";while(!path.empty()){cout << path.front() << ", ";path.pop();}cout << endl;
}void routingOfCircuit()//测试函数
{inputGridQueue();//输入迷宫/*输入起始值点和终点*/Position start, end;cout << "Please enter the start node:";while (!(cin >> start.row >> start.col)){cin.clear();//清空标志位while (cin.get() != '\n')//删除无效的输入continue;cout << "Please enter the start node:";}cout << "Please enter the end node:";while (!(cin >> end.row >> end.col)){cin.clear();//清空标志位while (cin.get() != '\n')//删除无效的输入continue;cout << "Please enter the end node:";}findPathQueue(start, end);//寻找最短路径,如找到返回true,如没找到则返回falseoutputPathQueue();//输出路径
}int main()
{cout << "电路布线问题********************" << endl;routingOfCircuit();return 0;
}

运行结果

C:\Users\15495\Documents\Jasmine\Work\coding\cmake-build-debug\coding.exe
鐢佃矾甯冪嚎闂********************
Please enter the size of Grid-Matrix:7
Please enter Grid[1,1]:0
Please enter Grid[1,2]:0
Please enter Grid[1,3]:1
Please enter Grid[1,4]:0
Please enter Grid[1,5]:0
Please enter Grid[1,6]:0
Please enter Grid[1,7]:0
Please enter Grid[2,1]:0
Please enter Grid[2,2]:0
Please enter Grid[2,3]:1
Please enter Grid[2,4]:1
Please enter Grid[2,5]:0
Please enter Grid[2,6]:0
Please enter Grid[2,7]:0
Please enter Grid[3,1]:0
Please enter Grid[3,2]:0
Please enter Grid[3,3]:0
Please enter Grid[3,4]:0
Please enter Grid[3,5]:1
Please enter Grid[3,6]:0
Please enter Grid[3,7]:0
Please enter Grid[4,1]:0
Please enter Grid[4,2]:0
Please enter Grid[4,3]:0
Please enter Grid[4,4]:1
Please enter Grid[4,5]:1
Please enter Grid[4,6]:0
Please enter Grid[4,7]:0
Please enter Grid[5,1]:1
Please enter Grid[5,2]:0
Please enter Grid[5,3]:0
Please enter Grid[5,4]:0
Please enter Grid[5,5]:1
Please enter Grid[5,6]:0
Please enter Grid[5,7]:0
Please enter Grid[6,1]:1
Please enter Grid[6,2]:1
Please enter Grid[6,3]:1
Please enter Grid[6,4]:0
Please enter Grid[6,5]:0
Please enter Grid[6,6]:0
Please enter Grid[6,7]:0
Please enter Grid[7,1]:1
Please enter Grid[7,2]:1
Please enter Grid[7,3]:1
Please enter Grid[7,4]:0
Please enter Grid[7,5]:0
Please enter Grid[7,6]:0
Please enter Grid[7,7]:0
The Grid =1     1     1     1     1     1     1     1     11     0     0     1     0     0     0     0     11     0     0     1     1     0     0     0     11     0     0     0     0     1     0     0     11     0     0     0     1     1     0     0     11     1     0     0     0     1     0     0     11     1     1     1     0     0     0     0     11     1     1     1     0     0     0     0     11     1     1     1     1     1     1     1     1
Please enter the start node:3 2
Please enter the end node:4 6
The path = (4,6), (5,6), (6,6), (6,5), (6,4), (5,4), (5,3), (5,2), (4,2), (3,2),Process finished with exit code 0

离线等价类问题

http://www.hkea.cn/news/50065/

相关文章:

  • 网站开发培训班百度网站推广关键词怎么查
  • 东莞市做网站公司seo怎样
  • ps做网站大小尺寸应用商店优化
  • 网站站群建设方案知名网页设计公司
  • 广州网站建设公司哪家好专业的seo搜索引擎优化培训
  • 外国人做汉字网站seo搜索排名影响因素主要有
  • 外贸五金网站建设网站制作优化排名
  • 义乌网站建设多少钱网络平台营销
  • 怀仁有做网站的公司吗磁力搜索引擎2023
  • 建站行业都扁平化设计合肥网站推广公司哪家好
  • 做企业网站织梦和wordpress哪个好百度指数查询工具app
  • 郑州网站服务公司优化神马排名软件
  • 茶叶网站建设的优势南宁seo外包平台
  • 高古楼网站 做窗子北京seo技术交流
  • 南阳建设网站制作网络最有效的推广方法
  • 纯静态网站seoseo排名优化北京
  • 开封网站建设哪家好指数计算器
  • 网站开发 架构石家庄seo关键词排名
  • 可以免费做商业网站的cms百度seo霸屏软件
  • 哪家网站建设专业快速建站教程
  • 坪山网站建设行业现状优化seo方案
  • 做网站需要架构师吗网站平台有哪些
  • 网站建设丿选择金手指15凡科建站官网
  • 可以做外国网站文章武汉企业seo推广
  • 天津网站建设公司最好太原做网站哪家好
  • 网站代下单怎么做百度指数数据分析平台入口
  • 淘宝做动效代码的网站seo的优化方向
  • 番禺建网站公司网站搜索工具
  • 安徽万振建设集团网站长春网站推广公司
  • 网站怎么制作 推广seo超级外链工具免费