码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-10 18:18:28      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

定义方向: 左上=左|上

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!