U E D R , A S I H C RSS

Random Walk/현민

~cpp 
// 랜덤 워크
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	int i,j,num;
	cout << "랜덤 워크를 실행하겠습니다. 숫자를 입력하세요 : ";
	cin >> num;
	
	int ** data = new int *[num];
	for (i=0; i<num; i++)
		data[i] = new int [num];


	int dir_x[8]={0,1,1,1,0,-1,-1,-1};
	int dir_y[8]={-1,-1,0,1,1,1,0,-1};

	int line,col,count=0;	// count는 총 움직인 횟수
	srand(time(0));
	line = rand() % num ;	
	col = rand() % num ;	

	for (i=0; i<num; i++)
	{
		for (j=0; j<num; j++)
		{
			data[i][j] = 0;
		}
	}

	
	
	int check=1;
	
	int direction = rand() % 8;
	int next_x = col + dir_x[direction];
	int next_y = line + dir_y[direction];
	
	while(check>0) 
	{
		direction = rand() % 8; // 랜덤으로 점이 움직이는 방향 
		next_x = col + dir_x[direction];
	    next_y = line + dir_y[direction];
	
		check=0;
			
		//벽이 아닐떄 
		if (next_y >= 0 && next_y < num && next_x >= 0 && next_x < num)
		{
			line = line + dir_y[direction];
			col = col + dir_x[direction];
			data[line][col]++;
			count++;
		}

		for (i=0; i<num; i++)
		{
			for (j=0; j<num; j++)
			{
				if(data[i][j] == 0)
					check++;
			}
		}

		

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

	cout << "총 움직인 횟수는 " << count << "번 입니다.\n";

	for(i=0;i<num;i++) 
                delete [] data [i]; 
        delete [] data; 
 
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:51
Processing time 0.0069 sec