~cpp
#include <iostream>
#include <ctime>
using namespace std;
int sq();
int main()
{
int n, l_or_r, line, row, passed, count = 0;
cout << "Random-Walker를 실행하겠습니다. 숫자를 입력하십시오. ";
cin >> n;
cin.get();
int ** square = new int * [n];
for (int i = 0 ; i < n ; i++)
square[i] = new int [n];
srand(time(0));
line = rand() % n;
row = rand() % n;
for (i = 0 ; i < n ; i++)
for (int j = 0 ; j < n ; j++)
square[i][j] = 0;
while(1)
{
l_or_r = rand() % 2;
if (l_or_r == 0)
{
if (line == 0)
line ++;
else if(line == n - 1)
line --;
else
line += sq();
}
else
{
if (row == 0)
row ++;
else if(row == n - 1)
row --;
else
row += sq();
}
square[line][row]++;
count++;
passed = 0;
for (int i = 0 ; i < n ; i++)
for (int j = 0 ; j < n ; j++)
if (square[i][j] != 0)
passed++;
if (passed == n * n)
break;
for (i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
cout << square[i][j] << " ";
cout << endl;
}
cin.get();
}
for (i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
cout << square[i][j] << " ";
cout << endl;
}
cout << count << "회 이동하였습니다.\n";
for (i = 0 ; i < n ; i++)
delete [] square[i];
delete [] square;
return 0;
}
int sq()
{
srand(time(0));
int x = -1, i = rand();
for (int j = 0 ; j < i ; j++)
x *= -1;
return x;
}