首页 / 算法 / 二维数组给定行列获取周围点的算法
二维数组给定行列获取周围点的算法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了二维数组给定行列获取周围点的算法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2190字,纯文字阅读大概需要4分钟。
内容图文
定义方向: 左上=左|上
XxxConstant.h
enum{LEFT = 1, RIGHT = (1 << 1), TOP = (1 << 2), BOTTOM = (1 << 3), LEFT_TOP = (LEFT|TOP), RIGHT_TOP=(RIGHT|TOP), LEFT_BOTTOM=(LEFT|BOTTOM), RIGHT_BOTTOM=(RIGHT|BOTTOM)};
int DIRECTIONS[8] = {LEFT, RIGHT, TOP, BOTTOM, LEFT_TOP, RIGHT_TOP, LEFT_BOTTOM, RIGHT_BOTTOM};
Cell.h代码如下: 表示数组元素所在位置
#pragma once #include <vector> class Cell { private: int row; int column; int direction; bool exists; public: Cell(int direction):direction(direction){}; Cell(int direction, int row, int column, bool exists):direction(direction), row(row), column(column), exists(exists){}; ~Cell(void); inline int getRow(){ return row; } inline int getColumn(){ return column; } inline int getDirection(){ return direction; } inline bool isExists(){ return exists; } std::vector<Cell*> getSurroundCell(int maxRow, int maxColumn); };
Cell.cpp: Cell::getSurroundCell算法实现
#include "Cell.h" #include "MineConstant.h" std::vector<Cell*> Cell::getSurroundCell(int maxRow, int maxColumn){ std::vector<Cell*> ret; int direction = 0; if(row > 0){ //不在第一行 direction |= TOP; } if(row < maxRow - 1){ direction |= BOTTOM; } if(column > 0){ direction |= LEFT; } if(column < maxColumn - 1){ direction |= RIGHT; } int directionCount = sizeof(DIRECTIONS) / sizeof(direction); for(int i = 0; i < directionCount; i++){ int direct = DIRECTIONS[i]; Cell* cell = nullptr; if((direction & direct) == direct){ switch (direct) { case LEFT: cell = new Cell(direct, row, column - 1, true); break; case RIGHT: cell = new Cell(direct, row, column + 1, true); break; case TOP: cell = new Cell(direct, row - 1, column, true); break; case BOTTOM: cell = new Cell(direct, row + 1, column, true); break; case LEFT_TOP: cell = new Cell(direct, row - 1, column - 1, true); break; case LEFT_BOTTOM: cell = new Cell(direct, row + 1, column - 1, true); break; case RIGHT_TOP: cell = new Cell(direct, row - 1, column + 1, true); break; case RIGHT_BOTTOM: cell = new Cell(direct, row + 1, column + 1, true); break; default: break; } } if(cell){ ret.push_back(cell); } } return ret; } Cell::~Cell(void) { }
原文:http://my.oschina.net/u/1778261/blog/490253
内容总结
以上是互联网集市为您收集整理的二维数组给定行列获取周围点的算法全部内容,希望文章能够帮你解决二维数组给定行列获取周围点的算法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。