U E D R , A S I H C RSS

Random Walk/재니

작성자의 페이지

02 장재니 Genie

소스

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

만들고나서..

원래는 8방향이엇는데 소스를 어디다 뒀는지 기억이 안나네요..ㅡㅡ;;

----
RandomWalk
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:51
Processing time 0.0273 sec