~cpp
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
const int max=5;
int matrix[max][max];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
matrix[i][j]=0;
}
srand(time(0)); // ë°”í€´ë²Œë ˆ ëžœë�¤ 놓기
int x = rand() % 5;
int y = rand() % 5;
int p, q;
matrix[x][y]=1;
int count=0, number=0;//엔딩수, ì�´ë�™ìˆ˜ ì§€ì •
while(count<24)
{
int moving = rand() % 8;
if (moving==0) //�
p=0,q=-1;
else if(moving == 1) //��
p=1,q=-1;
else if(moving == 2)//�
p=1,q=0;
else if(moving == 3) //남�
p=1,q=1;
else if(moving == 4) //남
p=0,q=-1;
else if(moving == 5) //남서
p=-1,q=1;
else if(moving == 6) //서
p=-1,q=0;
else if(moving == 7) //�서
p=-1,q=-1;
x = x + p;
y = y + q;
if((x<=4 && x>=0) && (y<=4 && y>=0)) // 틀 안쪽�지를 검사
{
if(matrix[x][y]==0) //�리 확�
{
matrix[x][y]=matrix[x][y]+1;
count++;
number++;
}
else
{
matrix[x][y]=matrix[x][y]+1;
number++;
}
}
else // 틀 밖�때 바퀴 � �리기
{
x=x-p;
y=y-q;
}
};
for(int ga=0;ga<5;ga++)
{
for(int se=0;se<5;se++)
cout << matrix[ga][se] << "\t";
cout<<endl;
}
cout<<endl;
cout << "ë°”í€´ë²Œë ˆì�˜ ì�´ë�™íšŸìˆ˜ëŠ” " << number << "입니다." << endl;
return 0;
}