~cpp
#include <iostream>
#include <cstring>
using namespace std;
struct Size
{
int width;
int height;
};
struct Location
{
int x;
int y;
};
class Journey
{
public:
int *JourneyArray;
int Length;
int CurrentPosition;
Journey(char *str)
{
Length=strlen(str);
JourneyArray=new int[Length];
for(int i=0;i<Length;i++)
JourneyArray[i]=str[i]-'0';
CurrentPosition=0;
}
~Journey()
{
delete[] JourneyArray;
}
int GetNextDirection()
{
if(IsFinished())
return -1;
return JourneyArray[CurrentPosition++];
}
bool IsFinished()
{
return (CurrentPosition==Length);
}
};
class Board
{
public:
int **BoardArray;
Size BoardSize;
int TraceCount;
Board(Size size)
{
BoardSize = size;
TraceCount = 0;
BoardArray = new int* [size.height];
for (int i = 0; i < size.height; i++)
{
BoardArray[i] = new int [size.width];
for (int j = 0; j < size.width; j++)
BoardArray[i][j] = 0;
}
}
~Board()
{
for (int i = 0; i < BoardSize.height; i++)
delete[] BoardArray[i];
delete[] BoardArray;
}
void LeaveTrace(Location location)
{
BoardArray[location.y][location.x]++;
TraceCount++;
}
bool IsAllCellsPassed()
{
for (int i = 0; i < BoardSize.height; i++)
{
for (int j = 0; j < BoardSize.width; j++)
{
if (BoardArray[i][j] == 0)
return false;
}
}
return true;
}
};
class Roach
{
public:
Board *MyBoard;
Location CurrentLocation;
void GoOnBoard(Board *board, Location startlocation)
{
MyBoard=board;
CurrentLocation=startlocation;
MyBoard->LeaveTrace(startlocation);
}
void Walk(Journey* journey)
{
int move[8][2]={ { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 } };
int direction;
while(MyBoard->IsAllCellsPassed()==false || journey->IsFinished()==false)
{
direction=journey->GetNextDirection();
CurrentLocation.x+=move[direction][0];
CurrentLocation.y+=move[direction][1];
if(CurrentLocation.x==-1) CurrentLocation.x=MyBoard->BoardSize.height-1;
if(CurrentLocation.x==MyBoard->BoardSize.height) CurrentLocation.x=0;
if(CurrentLocation.y==-1) CurrentLocation.y=MyBoard->BoardSize.width-1;
if(CurrentLocation.y==MyBoard->BoardSize.width) CurrentLocation.y=0;
MyBoard->LeaveTrace(CurrentLocation);
}
}
};
class User
{
public:
Size GetBoardSize()
{
Size size;
cin >> size.width >> size.height;
return size;
}
Location GetRoachStartLocation()
{
Location location;
cin >> location.x >> location.y;
return location;
}
Journey * GetJourney()
{
char str[1024];
cin >> str;
return new Journey(str);
}
void PutBoard(Board *board)
{
cout << "움직인 횟수: " << board->TraceCount-1 <<endl;
for (int h = 0; h < board->BoardSize.height; h++)
{
for (int w = 0; w < board->BoardSize.width; w++)
cout << board->BoardArray[h][w] << " ";
cout << endl;
}
}
};
class Executor
{
public:
void Execute(User *user)
{
Size boardsize=user->GetBoardSize();
Location startlocation=user->GetRoachStartLocation();
Journey *journey=user->GetJourney();
Board board(boardsize);
Roach roach;
roach.GoOnBoard(&board,startlocation);
roach.Walk(journey);
user->PutBoard(&board);
delete journey;
}
};
void main()
{
Executor executor;
User user;
executor.Execute(&user);
}