~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
~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; }
~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(); }
~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
~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; }
~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(); }
~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
~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
~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; }
~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; }
~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(); }
~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
~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
~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; }
~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; }
~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; }
~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
~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
~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; }
~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; }
~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; }
~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
~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
~cpp #include "roach.h" class SuperRoach : public Roach { public: SuperRoach(); ~SuperRoach(); SuperRoach(int nCurRow, int nCurCol, string szCourse); int GetDelta() { return 2; } };
~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; }
~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; }
~cpp #include "superroach.h" SuperRoach::SuperRoach() { } SuperRoach::~SuperRoach() { } SuperRoach::SuperRoach(int nCurRow, int nCurCol, string szCourse) { SetRoach(nCurRow, nCurCol, szCourse); }
~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; }