C++笔记-二维棋盘数组使用BFS(宽度优先遍历)
这里只对一个顶点只能上下左右,不能和左上,左下,右上,右下连起来。
思路步骤:
1.二维棋盘数据转链接表;
2.邻接表直接进行BFS
源码如下:
#include <QDebug>
#include <QVector>
#include <QQueue>#define MAX_COLUMN 6 + 2
#define MAX_ROW 6 + 2//用-1包住,保证处理的统一
int map1[MAX_ROW][MAX_COLUMN] = {-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, 1, -1,-1, -1, -1, -1, -1, -1, 0, -1,-1, -1, -1, -1, -1, -1, 0, -1,-1, -1, -1, -1, -1, -1, 0, -1,-1, -1, -1, -1, 1, 1, 0, -1,-1, -1, -1, -1, -1, -1, -1, -1
};struct Point{Point(int vNum, int x, int y) {this->vNum = vNum;this->x = x;this->y = y;}int vNum = -1; //顶点号int x; //x轴int y; //y轴friend QDebug operator << (QDebug os, Point &test){os << "(顶点:" << test.vNum << ", x:" << test.x << ", y:" << test.y << ")";return os;}
};class Graph {public:Graph(int map[MAX_ROW][MAX_COLUMN]) {memcpy(m_map, map, sizeof(m_map));//不为-1的都是顶点int vCount = 0;for (int i = 0; i < MAX_ROW; i++) {for (int j = 0; j < MAX_COLUMN; j++) {if (map[i][j] != -1) {Point point(vCount++, i, j);QVector<Point> ve;ve.append(point);m_adj.append(ve);}}}if (vCount == 0) {qDebug() << "退出" << endl;exit(-1);}this->m_v = vCount;analysisEdge();}//打印邻接表void print() {for (int i = 0; i < this->m_v; ++i) {qDebug() << "顶点:" << this->m_adj[i][0].vNum << " x:" << this->m_adj[i][0].x << " y:" << this->m_adj[i][0].y << " 的邻接表!";for (int j = 0; j < this->m_adj[i].size(); j++) {qDebug() << "顶点:" << this->m_adj[i][j].vNum << " x:" << this->m_adj[i][j].x << " y:" << this->m_adj[i][j].y;}qDebug() << "------------------------------------";}}void bfs(int startV){bool visited[this->m_v];for(int i = 0; i < this->m_v; i++){visited[i] = false;}Point point = getPointByVertex(startV);if(point.vNum == -1){qDebug() << "顶点错误!";exit(0);}QQueue<Point> queue;queue.push_back(point);visited[startV] = true;while(!queue.empty()){Point s = queue.front();queue.pop_front();// start todo somethingqDebug() << s;// end todo somethingfor(int i = 0; i < this->m_adj[s.vNum].size(); i++){int curretV = this->m_adj[s.vNum][i].vNum;if(visited[curretV] == false){visited[curretV] = true;queue.push_back(this->m_adj[s.vNum][i]);}}}}protected://通过坐标获取顶点号Point getPointByPosXAndPosY(int x, int y) {for (int i = 0; i < this->m_v; i++) {if (this->m_adj[i][0].x == x && this->m_adj[i][0].y == y) {return this->m_adj[i][0];}}return Point(-1, -1, -1);}//通过顶点号返回PointPoint getPointByVertex(int v){for(int i = 0; i < this->m_v; i++){if(this->m_adj[i][0].vNum == v){return this->m_adj[i][0];}}return Point(-1, -1, -1);}void analysisEdge() {//分析下边,这个顶点,如果周围一圈都是非-1的数,说明都可达。for (int i = 0; i < this->m_v; i++) {//左上
// if (m_map[this->m_adj[i][0].x - 1][this->m_adj[i][0].y - 1] != -1) {// Point point = getPointByPosXAndPosY(this->m_adj[i][0].x - 1, this->m_adj[i][0].y - 1);
// if (point.vNum != -1) {// //顶点是从1开始算,但下标是从0开始
// m_adj[this->m_adj[i][0].vNum].push_back(point);
// }
// }//正上if (m_map[this->m_adj[i][0].x - 1][this->m_adj[i][0].y] != -1) {Point point = getPointByPosXAndPosY(this->m_adj[i][0].x - 1, this->m_adj[i][0].y);if (point.vNum != -1) {//顶点是从1开始算,但下标是从0开始m_adj[this->m_adj[i][0].vNum].push_back(point);}}//右上
// if (m_map[this->m_adj[i][0].x - 1][this->m_adj[i][0].y + 1] != -1) {// Point point = getPointByPosXAndPosY(this->m_adj[i][0].x - 1, this->m_adj[i][0].y + 1);
// if (point.vNum != -1) {// //顶点是从1开始算,但下标是从0开始
// m_adj[this->m_adj[i][0].vNum].push_back(point);
// }
// }//正右if (m_map[this->m_adj[i][0].x][this->m_adj[i][0].y + 1] != -1) {Point point = getPointByPosXAndPosY(this->m_adj[i][0].x, this->m_adj[i][0].y + 1);if (point.vNum != -1) {//顶点是从1开始算,但下标是从0开始m_adj[this->m_adj[i][0].vNum].push_back(point);}}//右下
// if (m_map[this->m_adj[i][0].x + 1][this->m_adj[i][0].y + 1] != -1) {// Point point = getPointByPosXAndPosY(this->m_adj[i][0].x + 1, this->m_adj[i][0].y + 1);
// if (point.vNum != -1) {// //顶点是从1开始算,但下标是从0开始
// m_adj[this->m_adj[i][0].vNum].push_back(point);
// }
// }//正下if (m_map[this->m_adj[i][0].x + 1][this->m_adj[i][0].y] != -1) {Point point = getPointByPosXAndPosY(this->m_adj[i][0].x + 1, this->m_adj[i][0].y);if (point.vNum != -1) {//顶点是从1开始算,但下标是从0开始m_adj[this->m_adj[i][0].vNum].push_back(point);}}//左下
// if (m_map[this->m_adj[i][0].x + 1][this->m_adj[i][0].y - 1] != -1) {// Point point = getPointByPosXAndPosY(this->m_adj[i][0].x + 1, this->m_adj[i][0].y - 1);
// if (point.vNum != -1) {// //顶点是从1开始算,但下标是从0开始
// m_adj[this->m_adj[i][0].vNum].push_back(point);
// }
// }//正左if (m_map[this->m_adj[i][0].x][this->m_adj[i][0].y - 1] != -1) {Point point = getPointByPosXAndPosY(this->m_adj[i][0].x, this->m_adj[i][0].y - 1);if (point.vNum != -1) {//顶点是从1开始算,但下标是从0开始m_adj[this->m_adj[i][0].vNum].push_back(point);}}}}private:int m_v; //顶点的个数int m_map[MAX_ROW][MAX_COLUMN];QVector<QVector<Point>> m_adj;
};int main(int argc, char *argv[]) {Q_UNUSED(argc)Q_UNUSED(argv)Graph g(map1);g.print();g.bfs(0);getchar();return 0;
}
如下二维棋盘:
int map1[MAX_ROW][MAX_COLUMN] = {-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, 1, -1,-1, -1, -1, -1, -1, -1, 0, -1,-1, -1, -1, -1, -1, -1, 0, -1,-1, -1, -1, -1, -1, -1, 0, -1,-1, -1, -1, -1, 1, 1, 0, -1,-1, -1, -1, -1, -1, -1, -1, -1
};
BFS是这样的(从第1个顶点(下标为0)开始):
C++笔记-二维棋盘数组使用BFS(宽度优先遍历)相关推荐
- C++笔记-二维棋盘数组转邻接表(使用QTL)
使用这样的结构存储邻接表: QVector<QVector<Point>> m_adj; Point存储当前顶点号及X轴,Y轴: struct Point{Point(int ...
- 返回一个二维整数数组中的最大的子数组和
一.题目: 1.输入一个二维整形数组,数组里有正数有负数. 2.二维数组中连续的一个子矩阵组成一个子数组. 3.求所有子数组的和的最大值. 二.设计思想: .定义一个二维数组,使用二重循环对其进行赋值 ...
- numpy使用[]语法索引二维numpy数组中指定行列位置的数值内容(access value at certain row and column in numpy array)
numpy使用[]语法索引二维numpy数组中指定行列位置的数值内容(access value at certain row and column in numpy array) 目录
- numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row)
numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row) 目录
- numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array)
numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array) 目录 numpy使用[]语法索引二维numpy ...
- numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range)
numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range) 目录
- numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row)
numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row) 目录
- numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column)
numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column) 目录
- numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array)
numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array) 目录 numpy使用[]语
最新文章
- 浅谈Disruptor
- 神器与经典--sp_helpIndex
- 怎样查看哪些程序占用了swap空间
- Vue2 的学习经历 初识
- Gamebryo实例学习之二BackgroundLoad
- 非cpu0启动linux,SD卡无法启动Linux的问题及解决
- TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
- 对话框大小与像素关系
- 数据库中的DbUtils
- hihoCoder #1162 : 骨牌覆盖问题·三 (矩阵快速幂,DP)
- php.exe系统错误,PhpStorm中报 “Cannot run program git.exe, 系统找不到指定的文件” 错误的解决方法...
- android studio 继承关系 快捷键,Android Studio快捷键
- 没解决:如何离线更新eclipse支持的Compiler compliance level
- 测试手机是否可以安装MRP软件和MRP游戏
- 延迟队列DelayQueue原理
- thermal系列(4)-Thermal Core框架
- 仿酷狗音乐列表点击item子控件展开功能
- LSV软件不定时无法下载谷歌影像的原因
- Linux用到的大数据相关命令
- 【蓝桥杯选拔赛真题15】Scratch碰苹果游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
热门文章
- PHP和MySQL Web开发从新手到高手,第7天-创建author管理页面
- Linux命令-防火墙命令:iptables
- 也说春运网络购票:12306的码农没有你想的那么弱 [转]
- 打开网页出现“安全沙箱冲突”的提示
- 反编译工具Reflector 4.2 汉化版
- Ubuntu Vi 编辑器 命令(转)
- FineReport 11.0 全新大屏模式,打开3D视界,大屏制作更快
- Hadoop技术在商业智能BI中的应用
- NanShan企业即时通讯开始写博客
- 程序园冬天好冷怎么办?