~cpp 
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	int count, question, random, x = 0 , y = 0;
	cout << "칸 수를 입력하시오 : ";
	cin >> question;
	int ** data = new int *[question];
	for(int i=0 ; i<question ; i++) 
		data[i] = new int [question];
	for ( i = 0 ; i < question ; i++ )
	{
		for ( int j = 0 ; j < question ; j++ )
			data[i][j] = 0;
	}
	srand((time(0)));
	while (1)
	{
		count = 0;
		random = rand() % 4;
		cout << random << endl;
		switch ( random )
		{
		case 0:		//	위
			if ( (y - 1) != -1 )
			{
				data[x][y-1]++;
				y--;
				cout << "위" << endl;
			}
			break;
		case 1:		// 아래
			if ( y + 1 != question )
			{
				data[x][y+1]++;
				y++;
				cout << "아래" << endl;
			}
			break;
		case 2:		// 좌
			if ( x - 1 != -1 )
			{
				data[x-1][y]++;
				x--;
				cout << "좌" << endl;
			}
			break;
		case 3:		// 우
			if ( x + 1 != question )
			{
				data[x+1][y]++;
				x++;
				cout << "우" << endl;
			}
			break;
		}

		for ( i = 0 ; i < question ; i++ )
		{
			for ( int j = 0 ; j < question ; j++ )
			{
				if (data[i][j] == 0)
					count++;
			}
		}
		if ( count == 0 )
			break;
	}

	for ( i = 0 ; i < question ; i++ )
	{
		for ( int j = 0 ; j < question ; j++ )
			cout << data[j][i] << "\t";
		cout << endl;
	}
	for(i=0;i<question;i++) 
		delete [] data [i]; 
	delete [] data;
	return 0;
}

Retrieved from http://wiki.zeropage.org/wiki.php/RandomWalk/유상욱
last modified 2021-02-07 05:27:51