- μΈμκ΅°μ Random Walk - μ μ¬μ¬ν΄--;
- ν€λ
~cpp
#ifndef _BOARD_H_
#define _BOARD_H_
class RandomWalkBoard
{
private:
int _nRow;
int _nCol;
int** _nBlockFrequency;
int _nCurRow;
int _nCurCol;
int _nTotalMovement;
public:
RandomWalkBoard(int& nRow, int& nCol, int& nCurRow, int& nCurCol);
~RandomWalkBoard();
void BoardAllocate();
void BoardFree();
void AddVisitCount();
void ShowBoardStatus() const;
bool CheckCompletelyPatrol() const;
void SetArrayAsZero(int& nCurRow);
void StartMovement();
int GetRandomDirection() const;
bool CheckCorrectCoordinate(int& nDelRow, int& nDelCol) const;
void AddTotalCount();
};
#endif
~cpp
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
#include "RandomWalkBoard.h"
//// μμ±μμ μλ©Έμ ////
RandomWalkBoard::RandomWalkBoard(int& nRow, int& nCol, int& nCurRow, int& nCurCol)
{
_nRow = nRow;
_nCol = nCol;
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalMovement = 0;
srand(time(0));
BoardAllocate();
}
void RandomWalkBoard::BoardAllocate()
{
_nBlockFrequency = new int*[_nRow];
for(int i = 0 ; i < _nRow ; i ++)
{
_nBlockFrequency[i] = new int[_nCol];
SetArrayAsZero(i);
}
}
void RandomWalkBoard::SetArrayAsZero(int& nCurRow)
{
for(int i = 0 ; i < _nCol ; i ++)
{
_nBlockFrequency[nCurRow][i] = 0;
}
}
RandomWalkBoard::~RandomWalkBoard()
{
BoardFree();
}
void RandomWalkBoard::BoardFree()
{
if(_nBlockFrequency)
{
for(int i = 0 ; i < _nRow ; i ++)
{
if(_nBlockFrequency[i])
delete [] _nBlockFrequency[i];
}
delete [] _nBlockFrequency;
}
}
////////////////////////
//// λ³Έ νλ‘κ·Έλ¨ ////
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j++)
{
if(_nBlockFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::ShowBoardStatus() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j++)
{
cout << _nBlockFrequency[i][j] << " ";
}
cout << endl;
}
cout << "νμ¬ ν : " << _nCurRow << endl;
cout << "νμ¬ μ΄ : " << _nCurCol << endl;
cout << "μ΄ μ΄λλ : " << _nTotalMovement << endl;
}
void RandomWalkBoard::AddVisitCount()
{
_nBlockFrequency[_nCurRow][_nCurCol]++;
}
void RandomWalkBoard::StartMovement()
{
int DelRow = 0;
int DelCol = 0;
while(!CheckCompletelyPatrol())
{
DelRow = GetRandomDirection();
DelCol = GetRandomDirection();
if(CheckCorrectCoordinate(DelRow, DelCol))
{
_nCurRow += DelRow;
_nCurCol += DelCol;
AddVisitCount();
AddTotalCount();
}
}
}
int RandomWalkBoard::GetRandomDirection() const
{
return (rand()%3) - 1;
}
bool RandomWalkBoard::CheckCorrectCoordinate(int& nDelRow, int& nDelCol) const
{
if(_nCurRow + nDelRow < 0 || _nCurRow + nDelRow > _nRow - 1)
return false;
if(_nCurCol + nDelCol < 0 || _nCurCol + nDelCol > _nCol - 1)
return false;
return true;
}
void RandomWalkBoard::AddTotalCount()
{
_nTotalMovement++;
}
~cpp
#include <iostream>
using namespace std;
#include "RandomWalkBoard.h"
void AskInitData(int& nRow, int& nCol, int& nCurRow, int& nCurCol);
int main()
{
int nRow, nCol, nCurRow, nCurCol;
AskInitData(nRow, nCol, nCurRow, nCurCol);
RandomWalkBoard Test(nRow, nCol, nCurRow, nCurCol);
Test.StartMovement();
Test.ShowBoardStatus();
return 0;
}
void AskInitData(int& nRow, int& nCol, int& nCurRow, int& nCurCol)
{
cout << "\nνμ μ
λ ₯νμΈμ : ";
cin >> nRow;
cout << "\nμ΄μ μ
λ ₯νμΈμ : ";
cin >> nCol;
cout << "\nνμ¬ λ°ν΄λ²λ μ νμ μ
λ ₯νμΈμ(0 ~ ν-1) : ";
cin >> nCurRow;
cout << "\nνμ¬ λ°ν΄λ²λ μ μ΄μ μ
λ ₯νμΈμ(0 ~ μ΄-1) : ";
cin >> nCurCol;
}
- κ°λ¨νκ±Έ λ무 볡μ‘νκ² μ§ κ±΄ μλκ° νλ μκ°μ΄ λ λ€.. μ κΈΈ -- μΈμ
- κ·Όλ° λ¦¬ν©ν λ§ μ±
보면 λ©μλλ μ΅λν μ§§κ³ κ°κ²°νκ² λμΌλΌ κ·Έλμ--; -- μΈμ
- μμ€κ° μ’ κΈΈμ΄μ§κΈ΄ ν΄λ νμ€ν μμ보긴 νΈνκ² κ°λ€. -- μΈμ
- μ΄κ±Έ μ νλλ©΄, κ·Έλ₯ μ¬μ¬ν΄μ..--; λλ λ°λΈμ€ μΊ ν κ°κ³ μΆλΉ.. μ©.. -- μΈμ
DeleteMe )
λκ°
μ
μ±
ν
λ
μ?