~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;
}