Block.h
//
// Block.h
// test
//
// Created by Fang Jian on 14-2-28.
// Copyright (c) 2014年 FangJian. All rights reserved.
//
#ifndef __test__Block__
#define __test__Block__
#include <iostream>
class Block{
public:
Block():_index(0){};
Block(int index):_index(index){};
int getColorIndex(){return _index;};
void setColorIndex(int index){this->_index=index;};
private:
int _index;
};
#endif /* defined(__test__Block__) */
BlockManager.h
//
// BlockManager.h
// test
//
// Created by Fang Jian on 14-2-28.
// Copyright (c) 2014年 FangJian. All rights reserved.
//
#ifndef __test__BlockManager__
#define __test__BlockManager__
#include <iostream>
#include "Block.h"
class BlockManager{
public:
BlockManager(int rows,int cols):_rows(rows),_cols(cols){
blocks=new Block[rows*cols];
};
Block& getBlock(int row,int col){return blocks[row*_cols+col];};
Block& getBlock(int pos){return blocks[pos];};
int getCapacity(){return _rows*_cols;}
int getRows(){return _rows;};
int getCols(){return _cols;};
void flood(int from_row,int from_col,int colorIndex);
void print();
virtual ~BlockManager(){
delete[] blocks;
}
private:
int _rows;
int _cols;
Block* blocks;
void printBoolArray(bool array[]);
};
#endif /* defined(__test__BlockManager__) */
BlockManager.cpp
//
// BlockManager.cpp
// test
//
// Created by Fang Jian on 14-2-28.
// Copyright (c) 2014年 FangJian. All rights reserved.
//
#include "BlockManager.h"
#include <iostream>
using namespace std;
#define WHERE(row,col) ((row)*_cols+(col))
void BlockManager::printBoolArray(bool *array){
for(int i=0;i<_rows;i++){
for(int j=0;j<_cols;j++){
int colorIndex= array[i*_cols+j]?1:0;
cout<<colorIndex<<" ";
}
cout<<endl;
}
cout<<endl;
}
void BlockManager::print(){
for(int i=0;i<_rows;i++){
for(int j=0;j<_cols;j++){
int colorIndex=getBlock(i,j).getColorIndex();
cout<<colorIndex<<" ";
}
cout<<endl;
}
cout<<endl;
}
void BlockManager::flood(int from_row,int from_col,int colorIndex){
//第一点颜色
int firstColorIndex=getBlock(from_row,from_col).getColorIndex();
//数组总大小
int capacity=_rows*_cols;
//要染色的集合
bool* floodSet=new bool[capacity];
//已经检查点得集合
bool* checkedSet=new bool[capacity];
//备选检查点数组
int* candidatePoints=new int[capacity];
//初始化数组
for(int i=0;i<capacity;i++){
floodSet[i]=false;//需染色为1,不需染色为0
candidatePoints[i]=-1;//记录要检查的点在数组中得位置
checkedSet[i]=false; //尚未检查
}
//初始化第一个染色集合
floodSet[WHERE(from_row,from_col)]=true;
//初始化第一个检查点
int candidateIndex=0;
int maxIndex=0;
int currentPointWhere=WHERE(from_row,from_col);
candidatePoints[candidateIndex]=currentPointWhere;
while(maxIndex<capacity && (currentPointWhere=candidatePoints[candidateIndex])>=0){//当有备查点时
floodSet[currentPointWhere]=true;//当前点加入染色集合中
checkedSet[currentPointWhere]=true; //当前点已经检查
//检查当前点的上下左右
//上
int upWhere=currentPointWhere-_cols;
if((upWhere>=0) && !checkedSet[upWhere]
&&blocks[upWhere].getColorIndex()==firstColorIndex){ //在界内且未加入
candidatePoints[++maxIndex]=upWhere;
checkedSet[upWhere]=true;
}
//下
int downWhere=currentPointWhere+_cols;
if((downWhere<capacity) && !checkedSet[downWhere]
&&blocks[downWhere].getColorIndex()==firstColorIndex){ //在界内且未加入
candidatePoints[++maxIndex]=downWhere;
checkedSet[downWhere]=true;
}
//左
int leftWhere=currentPointWhere-1;
if((leftWhere>=0)&&!checkedSet[leftWhere] &&(leftWhere/_cols==currentPointWhere/_cols)
&&blocks[leftWhere].getColorIndex()==firstColorIndex){//在界内且未加入且在同一行
candidatePoints[++maxIndex]=leftWhere;
checkedSet[leftWhere]=true;
}
//右
int rightWhere=currentPointWhere+1;
if((rightWhere<capacity) && !checkedSet[rightWhere]&&(rightWhere/_cols==currentPointWhere/_cols)
&&blocks[rightWhere].getColorIndex()==firstColorIndex){ //在界内且未加入且在同一行
candidatePoints[++maxIndex]=rightWhere;
checkedSet[rightWhere]=true;
}
//检查下一个染色点
candidateIndex++;
//打出来看看
/*
cout<<"@"<<currentPointWhere<<"row:"<<currentPointWhere/_cols<<"col:"<<currentPointWhere%_cols<<endl;
printBoolArray(floodSet);
printBoolArray(checkedSet);
*/
}
//染色
int floodIndex=0;
int where=0;
while((where=candidatePoints[floodIndex++])>=0){
blocks[where]=colorIndex;
}
//内存清理
delete []floodSet;
delete[] candidatePoints;
delete[] checkedSet;
}
main.cpp
//
// main.cpp
// test
//
// Created by Fang Jian on 14-2-28.
// Copyright (c) 2014年 FangJian. All rights reserved.
//
#include <iostream>
#include <time.h>
#include "BlockManager.h"
using namespace std;
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
BlockManager bm(5,7);
/*
bm.print();
bm.getBlock(1, 3).setColorIndex(1);
bm.getBlock(1, 4).setColorIndex(1);
bm.getBlock(1, 5).setColorIndex(1);
bm.getBlock(2, 5).setColorIndex(1);
bm.getBlock(3, 5).setColorIndex(1);
bm.getBlock(4, 5).setColorIndex(1);
bm.print();
bm.flood(0, 0, 2);
bm.print();
*/
srand( (unsigned)time( NULL ) );
int capacity=bm.getCapacity();
for(int i=0;i<capacity/2;i++){
int pos=rand()%capacity;
bm.getBlock(pos).setColorIndex(1);
}
bm.print();
int row=rand()%bm.getRows();
int col=rand()%bm.getCols();
cout<<"flood from row:"<<row<<" col:"<<col<<endl;
bm.flood(row, col, 5);
bm.print();
return 0;
}
运行结果:
Hello, World!
1 1 1 0 1 0 0
0 1 0 1 1 0 0
0 0 0 0 0 0 0
0 1 0 0 1 1 0
0 1 0 1 0 1 0
flood from row:2 col:1
1 1 1 0 1 5 5
5 1 5 1 1 5 5
5 5 5 5 5 5 5
5 1 5 5 1 1 5
5 1 5 1 0 1 5