使用这样的结构存储邻接表:

QVector<QVector<Point>> m_adj;

Point存储当前顶点号及X轴,Y轴:

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轴
};

这里棋盘是这样的,如下:

#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, 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, 0, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1
};

这里的算法很简单:

//左上
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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].push_back(point);}
}

程序运行截图如下;

这样的棋盘:

int map1[MAX_ROW][MAX_COLUMN] = {-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, 0, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -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, 0, 1, -1,-1, -1, -1, -1, -1, -1, -1, -1
};

运行截图如下:

完整源码如下:

#include <QDebug>
#include <QVector>#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, 0, 1, -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轴
};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() << "------------------------------------";}}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);}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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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 - 1].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[]) {Graph g(map1);g.print();getchar();return 0;
}

还有一种是用-1包住棋盘,这样就更加方便了,在下一篇笔记中将会说明!

C++笔记-二维棋盘数组转邻接表(使用QTL)相关推荐

  1. C++笔记-二维棋盘数组使用BFS(宽度优先遍历)

    这里只对一个顶点只能上下左右,不能和左上,左下,右上,右下连起来. 思路步骤: 1.二维棋盘数据转链接表: 2.邻接表直接进行BFS 源码如下: #include <QDebug> #in ...

  2. 返回一个二维整数数组中的最大的子数组和

    一.题目: 1.输入一个二维整形数组,数组里有正数有负数. 2.二维数组中连续的一个子矩阵组成一个子数组. 3.求所有子数组的和的最大值. 二.设计思想: .定义一个二维数组,使用二重循环对其进行赋值 ...

  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) 目录

  4. numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row)

    numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row) 目录

  5. numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array)

    numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array) 目录 numpy使用[]语法索引二维numpy ...

  6. numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range)

    numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range) 目录

  7. numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row)

    numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row) 目录

  8. numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column)

    numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column) 目录

  9. numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array)

    numpy使用[]语法索引二维numpy数组中指定数据列的数值内容(accessing the specific column in numpy array) 目录 numpy使用[]语

最新文章

  1. 122112_1452_Word1
  2. 三层架构和MVC一样吗?(区别)
  3. 蓝桥杯Java历年真题与答案_蓝桥杯大赛java历年真题及答案整理(闭关一个月呕心沥血整理出来的)...
  4. ansys用什么cpu_ANSYS图形工作站与集群配置探讨201904-1
  5. 运算符面试题(剑指offer,面试宝典,牛客网)
  6. 无限级分类 php_php无限极分类的方法是什么
  7. 原创 | 2020年Java程序员应该学习的10大技术
  8. leetcode 删除排序数组中的重复项
  9. 【洛谷 P2633】 Count on a tree(主席树,树上差分)
  10. Linux grep
  11. 上位机与1200组态步骤_图解组态王一组态王软件
  12. ArcGIS制图表达Representation-制图表达使用须知
  13. 年仅44岁,又一高校教师英年早逝
  14. swfupload 无法加载_swfupload提示“错误302”的解决方法
  15. Python数据字典处理Excel,并统计总数,画出饼图
  16. Android双系统实现
  17. 搭建moon服务器,实现zerotier飞速穿透
  18. 20 - Slider组件案例 相亲APP
  19. linux 注销用户命令,Linux如何注销其他用户?
  20. 天空之城(献给我喜欢的女孩,杨)

热门文章

  1. 陶哲轩实分析 引理8.2.7 注
  2. JavaFX 新WebService客户端脚本语言
  3. delphi开发日志——注入“思想”,让程序操纵数据
  4. C/C++基础语法复习(二):C++ 面向对象编程,你需要知道的点
  5. 大家都在用这个神器分析数据,而你还只会Excel表头过滤?
  6. 【转载】指导教师的局域网聊天
  7. 飞鸽传书2013年开发计划
  8. 即使该链接出现到C++,用户又有多少的点击呢?由此可见
  9. 企业即时通讯市场增长500%
  10. 微信支付 php详解,PHP实现微信支付实战案例详解