U E D R , A S I H C RSS

Random Walk/동기

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

void main()
{
	srand((unsigned)time(NULL));
	int size;
	cout <<"맵의 크기를 결정해주세요>> ";
 	cin>> size;
	int MAX=size-1;

	int spawnX = rand()%size;
	int spawnY = rand()%size;

	int **data = new int*[size];
	for(int i=0;i<size;i++) {
		data[i] = new int[size];
	}
	for(int k=0;k<size;k++)
		{
			for(int j=0;j<size;j++)
				data[k][j]=0;
		}

	data[spawnY][spawnX]++;

	int out = 0;

	while(out==0)
	{
		out = 1;
		int move= rand()%8;

		if (move==0)
		{
			spawnY--;
			spawnX--;
			if (spawnX>=0 && spawnY>=0)
				data[spawnY][spawnX]++;
			else
			{
				spawnY++;
				spawnX++;
			}
		}
		else if (move==1)
		{
			spawnY--;
			if (spawnY>=0)
				data[spawnY][spawnX]++;
			else
				spawnY++;
		}
		else if (move==2)
		{
			spawnY--;
			spawnX++;
			if (spawnY>=0 && spawnX<=MAX)
				data[spawnY][spawnX]++;
			else{
				spawnY++;
				spawnX--;
			}
		}
		else if (move==3){
			spawnX++;
			if (spawnX<=MAX)
				data[spawnY][spawnX]++;
			else{
				spawnX--;
			}
		}
		else if (move==4){
			spawnX++;
			spawnY++;
			if (spawnX<=MAX && spawnY<=MAX)
				data[spawnY][spawnX]++;
			else{
				spawnX--;
				spawnY--;
			}
		}
		else if (move==5)
		{
			spawnY++;
			if (spawnY<=MAX)
				data[spawnY][spawnX]++;
			else
				spawnY--;
		}
		else if (move==6)
		{
			spawnY++;
			spawnX--;
			if (spawnX>=0 && spawnY<=MAX)
				data[spawnY][spawnX]++;
			else{
				spawnY--;
				spawnX++;
			}
		}
		else if (move==7)
		{
			spawnX--;
			if (spawnX>=0)
				data[spawnY][spawnX]++;
			else{
				spawnX++;
			}
		}
		for(k=0;k<size;k++)
		{
			for(int j=0;j<size;j++)
			{
				if(data[k][j]==0){
					out=0;
					break;
				}
			}
		}
	}

	for(k=0;k<size;k++)
	{
		for(int j=0;j<size;j++){
			cout << data[k][j]<<"\t";
		}
		cout<<endl;
	}
	int p=0;
	for(k=0;k<size;k++)
		{
			for(int j=0;j<size;j++)
				p+=data[k][j];
		}
	cout <<endl<<p<<endl;

	for(i;i<size;i++)
		delete [] data;

}

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