#include <cstdlib>
#include <ctime>
#include <vector>
#include <iostream>
#include <algorithm>
typedef struct tagCoords
{
int nRow;
int nCol;
int nCount;
int nCheck;
} CHECK_COORDS;
const static int N = 10;
const static long n_iterations = 100;
enum { mvForward = 0x01, mvBackward = 0x02 };
enum { mvHorizontal = 0x01, mvVertical = 0x02, mvDiagonal = 0x03 };
static long n_it = 0;
static short mv_path = mvForward;
static std::vector<CHECK_COORDS> vCheckCoords, vMatchCoords;
using namespace std;
void InitMatrix(vector< vector<int> >& Matrix)
{
vector< vector<int> >::iterator
row_iterator = Matrix.begin();
while (row_iterator != Matrix.end())
(row_iterator++)->resize(N, 0);
Matrix[0][0] = !Matrix[0][0];
Matrix[N-1][N-1] = !Matrix[N-1][N-1];
}
void Display(int val) { std::cout << val << " "; }
int MoveOrient() { return std::rand() % mvDiagonal + mvHorizontal; }
void UpdateView(vector< vector<int> >& Matrix)
{
vector< vector<int> >::iterator
row_iterator = Matrix.begin();
while (row_iterator != Matrix.end())
{
std::for_each(row_iterator->begin(),
row_iterator->end(), Display);
std::cout << endl;
row_iterator++;
}
std::cout << endl;
}
void MoveCheck(int row, int col, int check,
vector< vector<int> >& Matrix, vector<CHECK_COORDS>& CheckCoords)
{
if (n_it >= n_iterations) return;
if (row > N-1 && col <= N-1) row--;
if (col > N-1 && row <= N-1) col--;
if (row >= N-1 && col >= N-1 && mv_path == mvForward)
mv_path = mvBackward;
else if (row <= 0 && col <= 0 && mv_path == mvBackward)
mv_path = mvForward;
Matrix[row][col] = !Matrix[row][col];
std::cout << "row = " << row << " " << "col = " <<
col << " " << "iteration = " << n_it+1 << endl << endl;
if (mvHorizontal == MoveOrient() && row < N-1 && mv_path == mvForward) row++;
else if (mvVertical == MoveOrient() && col < N-1 && mv_path == mvForward) col++;
else if (mvDiagonal == MoveOrient() && row < N-1 && col < N-1 && mv_path == mvForward) { row++; col++; }
else if (mvHorizontal == MoveOrient() && row > 0 && mv_path == mvBackward) row--;
else if (mvVertical == MoveOrient() && col > 0 && mv_path == mvBackward) col--;
else if (mvDiagonal == MoveOrient() && row > 0 && col > 0 && mv_path == mvBackward) { row--; col--; }
bool bFound = false;
vector<CHECK_COORDS>::iterator
coords_iterator = CheckCoords.begin();
while (coords_iterator != CheckCoords.end() && !bFound)
{
if (coords_iterator->nRow == row &&
coords_iterator->nCol == col &&
coords_iterator->nCheck == check)
{ coords_iterator->nCount++; bFound = true; }
coords_iterator++;
}
if (bFound == false)
{
CHECK_COORDS Coords;
std::memset((void*)&Coords, 0x00, sizeof(CHECK_COORDS));
Coords.nRow = row; Coords.nCol = col;
Coords.nCheck = check; Coords.nCount = 1;
CheckCoords.push_back(Coords);
}
Matrix[row][col] = !Matrix[row][col]; n_it++;
UpdateView(Matrix); //std::cin.get();
MoveCheck(row, col, check, Matrix, CheckCoords);
}
int main(int argc, char** argv)
{
vector< vector<int> > Matrix(N,0);
std::srand((unsigned)std::time(NULL));
InitMatrix(Matrix);
UpdateView(Matrix);
n_it = 0;
MoveCheck(0, 0, 0, Matrix, vCheckCoords);
n_it = 0;
MoveCheck(N-1, N-1, 1, Matrix, vCheckCoords);
vector<CHECK_COORDS>::iterator
coords_iterator = vCheckCoords.begin();
while (coords_iterator != vCheckCoords.end())
{
long nVCount = 1;
vector<CHECK_COORDS>::iterator
seek_iterator = coords_iterator;
while (seek_iterator != vCheckCoords.end())
{
if (seek_iterator->nRow == coords_iterator->nRow &&
seek_iterator->nCol == coords_iterator->nCol &&
seek_iterator->nCheck == 1)
nVCount = nVCount + 1;
seek_iterator++;
}
CHECK_COORDS CheckCoords;
std::memset((void*)&CheckCoords, 0x00, sizeof(CHECK_COORDS));
CheckCoords.nRow = coords_iterator->nRow;
CheckCoords.nCol = coords_iterator->nCol;
CheckCoords.nCheck = 0; CheckCoords.nCount = nVCount;
vMatchCoords.push_back(CheckCoords);
std::cout << "row = " << coords_iterator->nRow << " " <<
"col = " << coords_iterator->nCol << " " <<
"check = " << coords_iterator->nCheck << " " <<
"count = " << coords_iterator->nCount << endl;
coords_iterator++;
}
std::cout << endl << endl;
long total = 0;
vector<CHECK_COORDS>::const_iterator
match_iterator = vMatchCoords.begin();
while (match_iterator != vMatchCoords.end())
{
std::cout << "row = " << match_iterator->nRow << " " <<
"col = " << match_iterator->nCol << " " <<
"count = " << match_iterator->nCount << endl;
total+=match_iterator->nCount;
match_iterator++;
}
std::cout << "\nTotal matches of both checks movement: " << total << endl << endl;
cout << "\nPress any key to continue..." << endl; std::cin.get();
return 0;
}