1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include <iostream>
#include <iomanip>
#include <queue>
#include <vector>
using namespace std;
class Block {
public:
int val=0;
std::vector<Block *> *vect=NULL;
int r=0;
int c=0;
Block():val(0){};
Block(int i):val(i){};
};
vector<Block*> v_arr[50];
int cols = 21;
int rows = 12;
int numblocks=12*21; //252
void display_bin(std::vector<Block *> &iblock){
std::cout << "BIN CONTENTS : ";
for ( std::vector<Block*>::iterator it=iblock.begin() ; it!= iblock.end(); it++ )
std::cout << (*it)->r << "," << (*it)->c << " " ;
std::cout << std::endl;
}
void fill(Block *m, int cols, int r, int c, vector<Block *> &target, vector<Block *> &replacement) {
if ((m+(r*cols+c))->vect != &target) {
return;
}
queue<pair<int,int>> q;
q.push(make_pair(r, c));
while (!q.empty()) {
auto p = q.front();
q.pop();
int row = p.first, w = p.second, e = p.second;
while (w >= 0 && (m+(row*cols+w))->vect == &target) --w;
while (e < cols && (m+(row*cols+e))->vect == &target) ++e;
for (int n = w + 1; n < e; ++n) {
(m+(row*cols+n))->vect = &replacement;
replacement.push_back(m+(row*cols+n));
if (row > 0 && (m+((row-1)*cols+n))->vect == &target)
q.push(make_pair(row-1, n));
if ((row < rows) && ((m+((row+1)*cols+n))->vect == &target))
q.push(make_pair(row+1, n));
}
}
}
int main() {
Block block[numblocks] =
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
{ 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1 ,1, //0
1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, //1
0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, //2
1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, //3
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, //4
1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, //5
0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, //6
0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, //7
0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, //8
0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //9
0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, //10
0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, //11
};
Block * MBlock=NULL;
MBlock=(Block*)malloc(numblocks*sizeof(Block));
int count=0;
for (int j=0; j< numblocks; j++) {
*(MBlock+count)=block[j];
(MBlock+count)->r=j/cols;
(MBlock+count)->c=j%cols;
if ((MBlock+count)->val ==1) {
(MBlock+count)->vect=&v_arr[0];
}
count++;
}
std::cout << "Data to be sorted into bins" <<std::endl;
count=0;
for (int j=0; j< rows; j++) {
for (int k=0; k<cols; k++) {
std::cout << (MBlock+count)->val << ",";
count++;
}
std::cout << std::endl;
}
int vcount = 1;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
if ((MBlock+(r*cols+c))->val == 1) {
fill(MBlock, cols, r, c, v_arr[0] , v_arr[vcount]);
if (v_arr[vcount].size() >0)
vcount++;
}
}
}
//For containers that have more than 4 elements, change the value of val
std::cout << "-------------------------------------------------------------" << std::endl;
std::cout << "There were " << vcount << " containers created" << std::endl;
int disint=1;
for (int m=0; m< vcount ; m++) {
std::cout << "Vcount " << m << " with size " << v_arr[m].size() << " ==> " ;
if (v_arr[m].size() >4){
disint++;
for (unsigned int n=0; n< v_arr[m].size() ; n++) {
std::cout << v_arr[m][n]->r << "," << v_arr[m][n]->c << " ";
v_arr[m][n]->val=disint;
}
} else {
for (unsigned int n=0; n< v_arr[m].size() ; n++) {
std::cout << v_arr[m][n]->r << "," << v_arr[m][n]->c << " ";
v_arr[m][n]->val=0;
}
}
std::cout << std::endl;
}
//DISPLAY the results of blobbing
std::cout << "-------------------------------------------------------------" << std::endl;
std::cout << "Displaying containers with 4 or more elements" << std::endl;
count=0;
for (int j=0; j< rows; j++) {
for (int k=0; k<cols; k++) {
if ((MBlock+count)->val)
std::cout << (MBlock+count)->val << ",";
else
std::cout << "."<< "," ;
count++;
}
std::cout << std::endl;
}
free(MBlock);
}
|