Contents
- 1. ์ค๋ฆฌ์ง๋ ver 1.0
- 2. Array๋ ๋ชจ๋ Vector๋ก, char* ์ String์ผ๋ก ๋ฐ๊พผ ๋ฒ์ version 1.5
- 3. ์ด์ง OOP ๋๋ฌด ์๊ฒ ๋๋๋ฉด ๊ด๋ฆฌํ๊ธฐ ํ๋ค๊ฒ ๊ฐ์์ ์ผ๋จ ๋ณด๋, ๋ฐํด๋ฒ๋ ๋๊ฐ์ ํด๋์ค๋ก๋ง ๋๋ด์ version 2.0
- 4. ๋ณ๊ฒฝ1 ์ ๋ง์กฑํ๋ ์ฝ๋
- 5. ๋ณ๊ฒฝ 2 ๋ฅผ ๋ง์กฑํ๋ ์ฝ๋
- 6. ์๊ตฌ3์ ๋ง์กฑํ๋ ์ฝ๋
1.1. ํค๋ ¶
~cpp
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
class RandomWalkBoard
{
private:
enum { MAXCOURSE = 100 };
int _nRow;
int _nCol;
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
char *_szCourse;
int **_arVisitFrequency;
int _arDirectionX[8];
int _arDirectionY[8];
public:
RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX[], int DirectY[]);
~RandomWalkBoard();
void CourseAllocate(char *szCourse);
void BoardAllocate();
void BoardFree();
void CourseFree();
void ShowStatus();
void SetArrayAsZero();
bool CheckCompletelyPatrol();
void IncreaseVisitCount();
void IncreaseTotalVisitCount();
void StartProcedure();
bool CheckEndCourse();
void CheckDirectionAndMove(int direction);
void DirectionAllocate(int x[], int y[]);
};
#endif
1.2. ์์ค ¶
~cpp
#include "RandomWalkBoard.h"
#include <cstring>
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, char *szCourse, int DirectX[], int DirectY[])
{
_nRow = nRow;
_nCol = nCol;
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
CourseAllocate(szCourse);
BoardAllocate();
SetArrayAsZero();
DirectionAllocate(DirectX, DirectY);
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::CourseAllocate(char *szCourse)
{
_szCourse = new char[ strlen(szCourse) + 1 ];
strcpy(_szCourse, szCourse);
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency = new int*[_nRow];
for(int i = 0 ; i < _nRow ; i++)
{
_arVisitFrequency[i] = new int[_nCol];
}
}
void RandomWalkBoard::CourseFree()
{
delete [] _szCourse;
}
void RandomWalkBoard::DirectionAllocate(int x[], int y[])
{
for(int i = 0 ; i < 8 ; i ++)
{
_arDirectionX[i] = x[i];
_arDirectionY[i] = y[i];
}
}
void RandomWalkBoard::BoardFree()
{
if(_arVisitFrequency)
{
for(int i = 0 ; i < _nRow ; i++)
{
if(_arVisitFrequency[i])
delete [] _arVisitFrequency[i];
}
delete [] _arVisitFrequency;
}
}
RandomWalkBoard::~RandomWalkBoard()
{
CourseFree();
BoardFree();
}
void RandomWalkBoard::ShowStatus()
{
cout << _nTotalVisitCount << endl << endl;
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
_arVisitFrequency[i][j] = 0;
}
}
}
bool RandomWalkBoard::CheckCompletelyPatrol()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
{
return false;
}
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount()
{
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::IncreaseTotalVisitCount()
{
_nTotalVisitCount ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
direction = _szCourse[ _nTotalVisitCount ] - 48;
CheckDirectionAndMove(direction);
IncreaseVisitCount();
IncreaseTotalVisitCount();
}
}
bool RandomWalkBoard::CheckEndCourse()
{
if( _nTotalVisitCount == strlen( _szCourse ) )
{
return true;
}
return false;
}
void RandomWalkBoard::CheckDirectionAndMove(int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = _nRow - 1;
else if(_nCurRow == _nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = _nCol - 1;
else if(_nCurCol == _nCol)
_nCurCol = 0;
}
1.3. ๋ฉ์ธ ¶
~cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
#include "RandomWalkBoard.h"
const int MAXCOURSE = 100;
void InputInitData(int &row, int &col, int &currow, int &curcol, char course[]);
fstream in("test.txt");
int main()
{
int row, col, currow, curcol;
char course[MAXCOURSE];
InputInitData(row, col, currow, curcol, course);
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
RandomWalkBoard test(row, col, currow, curcol, course, directX, directY);
test.StartProcedure();
test.ShowStatus();
return 0;
}
void InputInitData(int &row, int &col, int &currow, int &curcol, char course[])
{
in >> row;
in >> col;
in >> currow;
in >> curcol;
in >> course;
in.close();
}
2.1. ํค๋ ¶
~cpp
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
using namespace std;
class RandomWalkBoard
{
private:
enum { MAXCOURSE = 100 };
int _nRow;
int _nCol;
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
vector< vector<int> > _arVisitFrequency;
vector<int> _arDirectionX;
vector<int> _arDirectionY;
public:
RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX[], int DirectY[]);
~RandomWalkBoard();
void BoardAllocate();
void ShowStatus();
void SetArrayAsZero();
bool CheckCompletelyPatrol();
void IncreaseVisitCount();
void IncreaseTotalVisitCount();
void StartProcedure();
bool CheckEndCourse();
void CheckDirectionAndMove(int direction);
void DirectionAllocate(int x[], int y[]);
};
#endif
2.2. ์์ค ¶
~cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int nCurRow, int nCurCol, string& szCourse, int DirectX[], int DirectY[])
{
_nRow = nRow;
_nCol = nCol;
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
BoardAllocate();
DirectionAllocate(DirectX, DirectY);
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
void RandomWalkBoard::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus()
{
cout << _nTotalVisitCount << endl << endl;
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount()
{
_arVisitFrequency[_nCurRow][_nCurCol] ++;
}
void RandomWalkBoard::IncreaseTotalVisitCount()
{
_nTotalVisitCount ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
direction = _szCourse[ _nTotalVisitCount ] - 48;
CheckDirectionAndMove(direction);
IncreaseVisitCount();
IncreaseTotalVisitCount();
}
}
bool RandomWalkBoard::CheckEndCourse()
{
if( _nTotalVisitCount == _szCourse.size() )
return true;
return false;
}
void RandomWalkBoard::CheckDirectionAndMove(int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = _nRow - 1;
else if(_nCurRow == _nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = _nCol - 1;
else if(_nCurCol == _nCol)
_nCurCol = 0;
}
2.3. ๋ฉ์ธ ¶
~cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "RandomWalkBoard.h"
const int MAXCOURSE = 100;
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int row, col, currow, curcol;
string course;
InputInitData(row, col, currow, curcol, course);
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
RandomWalkBoard test(row, col, currow, curcol, course, directX, directY);
test.StartProcedure();
test.ShowStatus();
return 0;
}
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course)
{
in >> row;
in >> col;
in >> currow;
in >> curcol;
in >> course;
in.close();
}
3. ์ด์ง OOP ๋๋ฌด ์๊ฒ ๋๋๋ฉด ๊ด๋ฆฌํ๊ธฐ ํ๋ค๊ฒ ๊ฐ์์ ์ผ๋จ ๋ณด๋, ๋ฐํด๋ฒ๋ ๋๊ฐ์ ํด๋์ค๋ก๋ง ๋๋ด์ version 2.0 ¶
3.1.1. Board.h ¶
~cpp
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
enum { MAXCOURSE = 100 };
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
Roach _Roach;
vector<int> _arDirectionX;
vector<int> _arDirectionY;
public:
RandomWalkBoard(int nRow, int nCol, int DirectX[], int DirectY[], const Roach& roach);
~RandomWalkBoard();
void BoardAllocate();
void ShowStatus() const;
void SetArrayAsZero();
bool CheckCompletelyPatrol() const;
void IncreaseVisitCount();
void StartProcedure();
bool CheckEndCourse() const;
void DirectionAllocate(int x[], int y[]);
};
#endif
3.1.2. Roach.h ¶
~cpp
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
private:
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
public:
Roach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const;
int GetCurRow() const;
int GetCurCol() const;
void IncreaseTotalVisitCount();
void CheckDirectionAndMove(const vector<int>& X, const vector<int>& Y, int nRow, int nCol, int direction);
int GetTotalVisitCount() const;
};
#endif
3.2.1. Board.cpp ¶
~cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, int DirectX[], int DirectY[], const Roach& roach) : _Roach(roach)
{
_nRow = nRow;
_nCol = nCol;
BoardAllocate();
DirectionAllocate(DirectX, DirectY);
_arVisitFrequency[_Roach.GetCurRow()][_Roach.GetCurCol()] ++;
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
void RandomWalkBoard::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus() const
{
cout << _Roach.GetTotalVisitCount() << endl << endl;
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount()
{
_arVisitFrequency[ _Roach.GetCurRow() ][ _Roach.GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
direction = _Roach.GetCourse()[ _Roach.GetTotalVisitCount() ] - 48;
_Roach.CheckDirectionAndMove(_arDirectionX, _arDirectionY, _nRow, _nCol, direction);
IncreaseVisitCount();
_Roach.IncreaseTotalVisitCount();
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
if( _Roach.GetTotalVisitCount() == _Roach.GetCourse().size() )
return true;
return false;
}
3.2.2. Roach.cpp ¶
~cpp
#include "Roach.h"
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
const string& Roach::GetCourse() const
{
return _szCourse;
}
int Roach::GetCurRow() const
{
return _nCurRow;
}
int Roach::GetCurCol() const
{
return _nCurCol;
}
int Roach::GetTotalVisitCount() const
{
return _nTotalVisitCount;
}
void Roach::IncreaseTotalVisitCount()
{
_nTotalVisitCount ++;
}
void Roach::CheckDirectionAndMove(const vector<int>& X, const vector<int>& Y, int nRow, int nCol, int direction)
{
_nCurRow += Y[direction];
_nCurCol += X[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
3.3. ๋ฉ์ธ ¶
~cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "RandomWalkBoard.h"
#include "Roach.h"
const int MAXCOURSE = 100;
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int row, col, currow, curcol;
string course;
InputInitData(row, col, currow, curcol, course);
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach roach(currow, curcol, course);
RandomWalkBoard test(row, col, directX, directY, roach);
test.StartProcedure();
test.ShowStatus();
return 0;
}
void InputInitData(int &row, int &col, int &currow, int &curcol, string &course)
{
in >> row;
in >> col;
in >> currow;
in >> curcol;
in >> course;
in.close();
}
4. ๋ณ๊ฒฝ1 ์ ๋ง์กฑํ๋ ์ฝ๋ ¶
- ํ .. ํ๋ฉด์ ์ข ๋ง์ด ๋ฏ์ด ๊ณ ์ณค์ต๋๋ค. ์ญ์ ๋์์ธ์ด ๊ตฌ๋ฆฐ๊ฑฐ์์..;;
- ํ๋ฉด์ static ์ฌ์ฉ๋ฒ์ ํ์คํ ์ตํ์ต๋๋ค. ์๊ท ๋ก์ฐ~^^
4.1.1. Board ¶
~cpp
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
enum { MAX_ROACH = 2 };
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
Roach _Roach[2];
void BoardAllocate();
void SetArrayAsZero();
bool CheckEndCourse() const;
bool CheckCompletelyPatrol() const;
public:
RandomWalkBoard(int nRow, int nCol, const Roach roach[]);
~RandomWalkBoard();
void ShowStatus() const;
void IncreaseVisitCount(int nSequence);
void StartProcedure();
};
#endif
4.1.2. Roach ¶
~cpp
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
private:
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
static vector<int> _arDirectionX;
static vector<int> _arDirectionY;
public:
Roach();
Roach(int nCurRow, int nCurCol, string szCourse);
void SetRoach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const { return _szCourse; }
int GetCurRow() const { return _nCurRow; }
int GetCurCol() const { return _nCurCol; }
void IncreaseTotalVisitCount() { _nTotalVisitCount ++; }
int GetTotalVisitCount() const { return _nTotalVisitCount; }
void CheckDirectionAndMove(int nRow, int nCol, int direction);
static void DirectionAllocate(int X[], int Y[]);
};
#endif
4.2.1. Board ¶
~cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, const Roach roach[])
{
_nRow = nRow;
_nCol = nCol;
BoardAllocate();
for(int i = 0 ; i < MAX_ROACH ; i++)
{
_Roach[i] = roach[i];
_arVisitFrequency[_Roach[i].GetCurRow()][_Roach[i].GetCurCol()] ++;
}
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus() const
{
for(int i = 0 ; i < MAX_ROACH ; i ++)
cout << _Roach[i].GetTotalVisitCount() << endl;
cout << endl;
for(i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount(int nSequence)
{
_arVisitFrequency[ _Roach[nSequence].GetCurRow() ][ _Roach[nSequence].GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
for(int i = 0 ; i < MAX_ROACH ; i ++)
{
if( _Roach[i].GetCourse().size() > _Roach[i].GetTotalVisitCount() )
{
direction = _Roach[i].GetCourse()[ _Roach[i].GetTotalVisitCount() ] - 48;
_Roach[i].CheckDirectionAndMove(_nRow, _nCol, direction);
IncreaseVisitCount(i);
_Roach[i].IncreaseTotalVisitCount();
}
}
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
if( _Roach[0].GetTotalVisitCount() == _Roach[0].GetCourse().size() && _Roach[1].GetTotalVisitCount() == _Roach[1].GetCourse().size())
return true;
return false;
}
4.2.2. Roach ¶
~cpp
#include "Roach.h"
vector<int> Roach::_arDirectionX;
vector<int> Roach::_arDirectionY;
void Roach::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
Roach::Roach()
{
}
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
void Roach::SetRoach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
4.3. ๋ฉ์ธ ¶
~cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "RandomWalkBoard.h"
#include "Roach.h"
const int MAXCOURSE = 100;
void InputInitData(int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach::DirectionAllocate(directX, directY);
int row, col, currow, curcol;
string course;
Roach roach[2];
in >> row;
in >> col;
for(int i = 0 ; i < 2 ; i ++)
{
InputInitData(currow, curcol, course);
roach[i].SetRoach(currow, curcol, course);
}
RandomWalkBoard test(row, col, roach);
test.StartProcedure();
test.ShowStatus();
in.close();
return 0;
}
void InputInitData(int &currow, int &curcol, string &course)
{
in >> currow;
in >> curcol;
in >> course;
}
- ์ ์๊พธ ํญ์ด ์นํ๋์ง..--;
5. ๋ณ๊ฒฝ 2 ๋ฅผ ๋ง์กฑํ๋ ์ฝ๋ ¶
- ๋ณ๊ฒฝ 1์์ ๊ฑฐ์ ๋ณ๊ฒฝ๋๊ฒ ์๋๊ตฐ์. ํผ์ ์ข์ํ๋ค๋..--; ๋ณ๊ฒฝ1์ ํ๋ฉด์ ๋ฆฌํฉํ ๋ง ์ฌ๊ธ ๊ณต๋ถํ๊ฑธ ์จ๋ดค์ต๋๋ค. ๋ฉ์๋ ์ฎ๊ธฐ๊ธฐ get ์ด๋ฐ๊ฑฐ ๋ง๋ ๋์ค๋ฉด ๊ทธ๊ฒ๋ ์ฎ๊ธฐ๊ธฐ ์ ๋?
- ๋ณ์ ์ด๋ฆ๋ ์ด์ง ๋ฐ๊พธ๊ณ .. ํ๋ค ๋ณด๋๊น ๋ญ๊ฐ ๊นจ๋ฌ์๊ฒ๋ ๊ฐ๋ค๋ ์๊ฐ์ด.. ^^;
- ์ฝ๊ฒ ์๊ฐํ ๋ผ๊ณ ์ผ๋จ 999๋ ์ง์๋ฒ๋ ธ์ต๋๋ค.
5.1.1. Board.h ¶
~cpp
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
int _nRoachCount;
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
vector< Roach > _Roaches;
void BoardAllocate();
void SetArrayAsZero();
bool CheckEndCourse() const;
bool CheckCompletelyPatrol() const;
bool CheckAllRoachesEndCourse() const;
public:
RandomWalkBoard(int nRow, int nCol, vector<Roach>& roach, int nRoachCount);
~RandomWalkBoard();
void ShowStatus() const;
void IncreaseVisitCount(int nSequence);
void StartProcedure();
};
#endif
5.1.2. Roach.h ¶
~cpp
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
private:
int _nCurRow;
int _nCurCol;
int _nTotalVisitCount;
string _szCourse;
static vector<int> _arDirectionX;
static vector<int> _arDirectionY;
public:
Roach();
Roach(int nCurRow, int nCurCol, string szCourse);
void SetRoach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const { return _szCourse; }
int GetCurRow() const { return _nCurRow; }
int GetCurCol() const { return _nCurCol; }
void IncreaseTotalVisitCount() { _nTotalVisitCount ++; }
int GetTotalVisitCount() const { return _nTotalVisitCount; }
void CheckDirectionAndMove(int nRow, int nCol, int direction);
static void DirectionAllocate(int X[], int Y[]);
};
#endif
5.2.1. Board.cpp ¶
~cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, vector<Roach>& roach, int nRoachCount)
{
_nRow = nRow;
_nCol = nCol;
_nRoachCount = nRoachCount;
_Roaches.resize(_nRoachCount);
BoardAllocate();
for(int i = 0 ; i < _nRoachCount ; i++)
{
_Roaches[i] = roach[i];
_arVisitFrequency[_Roaches[i].GetCurRow()][_Roaches[i].GetCurCol()] ++;
}
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
RandomWalkBoard::~RandomWalkBoard()
{
}
void RandomWalkBoard::ShowStatus() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
cout << _Roaches[i].GetTotalVisitCount() << endl;
cout << endl;
for(i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount(int nSequence)
{
_arVisitFrequency[ _Roaches[nSequence].GetCurRow() ][ _Roaches[nSequence].GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i].GetCourse().size() > _Roaches[i].GetTotalVisitCount() )
{
direction = _Roaches[i].GetCourse()[ _Roaches[i].GetTotalVisitCount() ] - 48;
_Roaches[i].CheckDirectionAndMove(_nRow, _nCol, direction);
IncreaseVisitCount(i);
_Roaches[i].IncreaseTotalVisitCount();
}
}
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
if( CheckAllRoachesEndCourse() )
return true;
return false;
}
bool RandomWalkBoard::CheckAllRoachesEndCourse() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i].GetTotalVisitCount() != _Roaches[i].GetCourse().size() )
return false;
}
return true;
}
5.2.2. Roach.cpp ¶
~cpp
#include "Roach.h"
vector<int> Roach::_arDirectionX;
vector<int> Roach::_arDirectionY;
void Roach::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
Roach::Roach()
{
}
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
void Roach::SetRoach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
5.3. ๋ฉ์ธ ¶
~cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "RandomWalkBoard.h"
#include "Roach.h"
void InputInitData(int &currow, int &curcol, string &course);
fstream in("test.txt");
int main()
{
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach::DirectionAllocate(directX, directY);
int row, col, currow, curcol;
int roachcount = 0;
string course;
vector<Roach> roaches(10);
in >> row;
in >> col;
while(!in.eof())
{
if(roaches.size() <= roachcount)
roaches.resize( roaches.size() + 10 );
InputInitData(currow, curcol, course);
roaches[roachcount].SetRoach(currow, curcol, course);
roachcount ++;
}
RandomWalkBoard test(row, col, roaches, roachcount);
test.StartProcedure();
test.ShowStatus();
in.close();
return 0;
}
void InputInitData(int &currow, int &curcol, string &course)
{
in >> currow;
in >> curcol;
in >> course;
}
6. ์๊ตฌ3์ ๋ง์กฑํ๋ ์ฝ๋ ¶
- ์.. ์ด๋ฒ์๋ ๋ณ๋ก ๊ณ ์น์ง ์์๋ค๋ ๊ฒ์ ์์์.. ^^;
- STL ์ปจํ
์ด๋๋ ๋ณต์ฌ๋ณธ์ ์ฌ์ฉํ๋ค๋ ๊ฑธ ์๊ณ ํฌ์ธํฐ๋ก ๋ฐ๊ฟจ์ต๋๋ค.
- ์ด์งธ ์ ์ ๋๋ฌ์์ ธ๊ฐ๋๊ฒ ๊ฐ๋ค๋..--;
- ... ์ง๊ธ ๋ณด๋๊น SuperRoach ํด๋์ค๋ ์ ๋ง๋ค์๋์ง ์๋ฌธ์ด ๊ฐ๋ค.
6.1.1. Board ¶
~cpp
#ifndef _RANDOM_WALK_H
#define _RANDOM_WALK_H
#include <vector>
#include <string>
#include "Roach.h"
using namespace std;
class RandomWalkBoard
{
private:
int _nRoachCount;
int _nRow;
int _nCol;
vector< vector<int> > _arVisitFrequency;
vector< Roach* > _Roaches;
void BoardAllocate();
void SetArrayAsZero();
bool CheckEndCourse() const;
bool CheckCompletelyPatrol() const;
public:
RandomWalkBoard(int nRow, int nCol, vector< Roach* >& roach, int nRoachCount);
~RandomWalkBoard();
void ShowStatus() const;
void IncreaseVisitCount(int nSequence);
void StartProcedure();
};
#endif
6.1.2. Roach ¶
~cpp
#ifndef _ROACH_H_
#define _ROACH_H_
#include <vector>
#include <string>
using namespace std;
class Roach
{
protected:
int _nCurRow;
int _nCurCol;
static vector<int> _arDirectionX;
static vector<int> _arDirectionY;
int _nTotalVisitCount;
string _szCourse;
public:
Roach();
Roach(int nCurRow, int nCurCol, string szCourse);
void SetRoach(int nCurRow, int nCurCol, string szCourse);
const string& GetCourse() const { return _szCourse; }
int GetCurRow() const { return _nCurRow; }
int GetCurCol() const { return _nCurCol; }
void IncreaseTotalVisitCount() { _nTotalVisitCount ++; }
int GetTotalVisitCount() const { return _nTotalVisitCount; }
virtual void CheckDirectionAndMove(int nRow, int nCol, int direction);
static void DirectionAllocate(int X[], int Y[]);
virtual ~Roach();
virtual int GetDelta() { return 1; }
};
#endif
6.1.3. SuperRoach ¶
~cpp
#include "roach.h"
class SuperRoach : public Roach
{
public:
SuperRoach();
~SuperRoach();
SuperRoach(int nCurRow, int nCurCol, string szCourse);
int GetDelta() { return 2; }
};
6.2.1. Board ¶
~cpp
#include "RandomWalkBoard.h"
#include <iostream>
using namespace std;
RandomWalkBoard::RandomWalkBoard(int nRow, int nCol, vector< Roach* >& roach, int nRoachCount)
{
_nRow = nRow;
_nCol = nCol;
_nRoachCount = nRoachCount;
_Roaches.resize(_nRoachCount);
BoardAllocate();
for(int i = 0 ; i < _nRoachCount ; i++)
{
_Roaches[i] = roach[i];
_arVisitFrequency[_Roaches[i]->GetCurRow()][_Roaches[i]->GetCurCol()] ++;
}
}
void RandomWalkBoard::BoardAllocate()
{
_arVisitFrequency.resize(_nRow);
for(int i = 0 ; i < _nRow ; i++)
_arVisitFrequency[i].resize(_nCol);
SetArrayAsZero();
}
RandomWalkBoard::~RandomWalkBoard()
{
for(int i = 0 ; i < _nRoachCount ; i++)
{
delete _Roaches[i];
}
}
void RandomWalkBoard::ShowStatus() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
cout << _Roaches[i]->GetTotalVisitCount() << endl;
cout << endl;
for(i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
cout << _arVisitFrequency[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void RandomWalkBoard::SetArrayAsZero()
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
_arVisitFrequency[i][j] = 0;
}
}
bool RandomWalkBoard::CheckCompletelyPatrol() const
{
for(int i = 0 ; i < _nRow ; i ++)
{
for(int j = 0 ; j < _nCol ; j ++)
{
if(_arVisitFrequency[i][j] == 0)
return false;
}
}
return true;
}
void RandomWalkBoard::IncreaseVisitCount(int nSequence)
{
_arVisitFrequency[ _Roaches[nSequence]->GetCurRow() ][ _Roaches[nSequence]->GetCurCol() ] ++;
}
void RandomWalkBoard::StartProcedure()
{
int direction;
while( !CheckEndCourse() && !CheckCompletelyPatrol() )
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i]->GetCourse().size() > _Roaches[i]->GetTotalVisitCount() )
{
direction = _Roaches[i]->GetCourse()[ _Roaches[i]->GetTotalVisitCount() ] - 48;
for( int j = 0 ; j < _Roaches[i]->GetDelta() ; j++)
{
_Roaches[i]->CheckDirectionAndMove(_nRow, _nCol, direction);
IncreaseVisitCount(i);
}
_Roaches[i]->IncreaseTotalVisitCount();
}
}
}
}
bool RandomWalkBoard::CheckEndCourse() const
{
for(int i = 0 ; i < _nRoachCount ; i ++)
{
if( _Roaches[i]->GetTotalVisitCount() != _Roaches[i]->GetCourse().size() )
return false;
}
return true;
}
6.2.2. Roach ¶
~cpp
#include "Roach.h"
vector<int> Roach::_arDirectionX;
vector<int> Roach::_arDirectionY;
void Roach::DirectionAllocate(int X[], int Y[])
{
_arDirectionX.resize(8);
_arDirectionY.resize(8);
_arDirectionX.assign(&X[0], &X[7]);
_arDirectionY.assign(&Y[0], &Y[7]);
}
Roach::Roach()
{
}
Roach::~Roach()
{
}
Roach::Roach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
void Roach::SetRoach(int nCurRow, int nCurCol, string szCourse)
{
_nCurRow = nCurRow;
_nCurCol = nCurCol;
_nTotalVisitCount = 0;
_szCourse = szCourse;
}
void Roach::CheckDirectionAndMove(int nRow, int nCol, int direction)
{
_nCurRow += _arDirectionY[direction];
_nCurCol += _arDirectionX[direction];
if(_nCurRow == -1)
_nCurRow = nRow - 1;
else if(_nCurRow == nRow)
_nCurRow = 0;
if(_nCurCol == -1)
_nCurCol = nCol - 1;
else if(_nCurCol == nCol)
_nCurCol = 0;
}
6.2.3. SuperRoach ¶
~cpp
#include "superroach.h"
SuperRoach::SuperRoach()
{
}
SuperRoach::~SuperRoach()
{
}
SuperRoach::SuperRoach(int nCurRow, int nCurCol, string szCourse)
{
SetRoach(nCurRow, nCurCol, szCourse);
}
6.3. ๋ฉ์ธ ¶
~cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "RandomWalkBoard.h"
#include "SuperRoach.h"
void InputInitData(int &currow, int &curcol, string &course, char &isSuper);
fstream in("test.txt");
int main()
{
int directX[8] = {0,1,1,1,0,-1,-1,-1};
int directY[8] = {-1,-1,0,1,1,1,0,-1};
Roach::DirectionAllocate(directX, directY);
int row, col, currow, curcol;
int roachcount = 0;
char isSuper;
string course;
vector< Roach* > roaches(10);
Roach* temp;
in >> row;
in >> col;
while(!in.eof())
{
if(roaches.size() <= roachcount)
roaches.resize( roaches.size() + 10 );
InputInitData(currow, curcol, course, isSuper);
if(isSuper == 'N')
temp = new Roach(currow, curcol, course);
else if(isSuper == 'S')
temp = new SuperRoach(currow, curcol, course);
roaches[roachcount] = temp;
roachcount ++;
}
RandomWalkBoard test(row, col, roaches, roachcount);
test.StartProcedure();
test.ShowStatus();
in.close();
return 0;
}
void InputInitData(int &currow, int &curcol, string &course, char &isSuper)
{
in >> currow;
in >> curcol;
in >> isSuper;
in >> course;
}










