~cpp
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_JOURNEY 1024 // 최대 여정 수
int walk(int m, int n, int starti, int startj, char journey[MAX_JOURNEY], int **board);
// 메인 함수
void main()
{
// 입력 데이터
int m, n;
int starti, startj;
char journey[MAX_JOURNEY];
// 입력
cout << "Input :\n";
cin >> m >> n;
cin >> starti >> startj;
// 여정 입력
char buffer[MAX_JOURNEY];
int offset=0;
for(;;)
{
cin.getline(buffer,MAX_JOURNEY);
if(strcmp(buffer,"999")==0)
break;
strcpy(&journey[offset],buffer);
offset+=strlen(buffer);
}
// 출력 데이터
int count;
int **board=new int*[m];
for(int i=0;i<m;i++)
{
board[i]=new int[n];
for(int j=0;j<n;j++)
board[i][j]=0;
}
// Walk
count=walk(m,n,starti,startj,journey,board);
// 출력
cout << "Output :\n";
cout << count << endl;
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout << board[i][j] << " ";
cout << endl;
}
for(i=0;i<m;i++)
delete[] board[i];
delete[] board;
}
// Walk 하는 함수
int walk(int m, int n, int starti, int startj, char journey[MAX_JOURNEY], int **board)
{
int move[8][2]={ { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 },
{ 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } };
int count=0;
board[starti][startj]=1;
int currenti=starti, currentj=startj;
int journeycount=0;
for(;;)
{
// 종료 조건 검사
bool quit=true;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(board[i][j]==0)
quit=false;
}
if(quit==false)
break;
}
if(quit==true || journey[journeycount]=='\0')
break;
// Walk
currenti+=move[journey[journeycount]-'0'][0];
currentj+=move[journey[journeycount]-'0'][1];
journeycount++;
if(currenti==-1) currenti=m-1;
if(currenti==m) currenti=0;
if(currentj==-1) currentj=n-1;
if(currentj==n) currentj=0;
board[currenti][currentj]++;
count++;
}
return count;
}