U E D R , A S I H C RSS

Random Walk/창재

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

int def_empty();

int array[5][5];

void main()
{
	int def_temp;
	int temp_h, temp_y;
	int bool_def=0;
	srand(time(0));

	int start_h, start_y;
	int place_h, place_y;

	start_h = rand()%5;
	start_y = rand()%5;

	
	for (int i = 0; i < 5 ; i++)
	{
		for (int j = 0 ; j < 5 ; j++)
			array[i][j] = 0;
	}

	array[start_h][start_y] ++;

	temp_h = start_h;
	temp_y = start_y;
	int count = 0;

	while (def_temp != 0 )
	{
		place_h = rand()%5;
		while (place_h == temp_h -1 || place_h == temp_h +1) 
			place_h = rand()%5;
		place_y = rand()%5;
		while (place_y == temp_h -1 || place_y == temp_y +1)
			place_y = rand()%5;

		array[place_h][place_y] ++;

		temp_h = place_h;
		temp_y = place_y;
		count ++;
				
		def_temp = def_empty();
	}


	for (int l = 0; l < 5 ; l++)
	{
		for (int j = 0 ; j < 5 ; j++)
			cout << array[l][j] << "\t";
		cout << endl << endl;
	}
	cout << "총 움직인 횟수 = "  << count << endl ;
}

int def_empty() // 꽉 찼나 안찼나 확인하는 함수.
{
	int empty_def = 0;
	for (int j = 0 ; j < 5 ; j++)
	{
		for (int p = 0 ; p < 5 ; p++)
		{
			if (array[j][p] == 0)
				empty_def ++;
		}
	}
	return empty_def;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:52
Processing time 0.0080 sec