二维数组给定行列获取周围点的算法

定义方向: 左上=左|上

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)
{
}


转载于:https://my.oschina.net/u/1778261/blog/490253

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值