U E D R , A S I H C RSS

Random Walk/황재선

2004

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

const int rowMax = 40;
const int colMax = 20;

int board[rowMax][colMax];
int row, col, ibug, jbug, count;

void init();
void input();
void move();
bool existZero(int aBoard[rowMax][colMax]);
void printResult();

int main()
{
	init();
	input();
	move();
	printResult();
	return 0;
}

void init()
{
	for (int i = 0; i < rowMax; i++)
		for (int j =0; j < colMax; j++)
			board[i][j] = 0;
	count = 0;
}


void input()
{
	do
	{
		cout << "  ?(3 40)";
		cin >> row;
		cout << "  ?(2 20)";
		cin >> col;
		if (row > 2 && row < 41 && col > 1 && col < 21)
			break;
		cout << ",    .  ." << endl;
	} while(true);

	do
	{
		cout << "   .";
		cin >> ibug;
		cout << "   .";
		cin >> jbug;
		if (ibug >= 0 && ibug < col && jbug >= 0 && jbug < row)
			break;
		cout << "   .  ." << endl;

	} while(true);
	board[ibug][jbug] = 1;

}

void move()
{
	srand(time(0));

	int imove[] = {-1, 0, 1, 1, 1, 0, -1, -1};
	int jmove[] = {1, 1, 1, 0, -1, -1, -1, 0};

	while(true)
	{
		int index = rand() % 8;

		if (ibug + imove[index] < 0 || ibug + imove[index] > row - 1 ||
		jbug + jmove[index] < 0 || jbug + jmove[index] > col - 1 )
			continue;
		else
		{
			board[ibug + imove[index]][jbug + jmove[index]]++;
			ibug = ibug + imove[index];
			jbug = jbug + jmove[index];
			count++;
		}
		if (!existZero(board))
			break;
		}
}


bool existZero(int aBoard[rowMax][colMax])
{
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			if (aBoard[i][j] == 0)
				return true;
	return false;
}


void printResult()
{
	cout << "\n  한 횟 : \n\n";
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
			cout << setw(3) << board[i][j];
		cout << endl;
	}

	cout << "\n  : " << count << endl;
}

2005

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

//    
#define X_MIN 2
#define X_MAX 20
#define Y_MIN 2
#define Y_MAX 40
#define ITER_LIMIT 50000

// floor 행(row) 
int inputY() {
	cout << "n()(3-40): ";
	int n;	
	while (true) {
		cin >> n;
		if (n > Y_MIN && n <= Y_MAX)
			break;
		cout << " . n()(3-40): ";
	}
	return n;
}

// floor (col) 
int inputX() {
	cout << "m()(2-20): ";
	int m;	
	while (true) {
		cin >> m;
		if (m >= X_MIN && m <= X_MAX)
			break;
		cout << " . m()(2-20): ";		
	}
	return m;
}

//  x 
int inputPosX(int m) {
	cout << "X : ";
	int posX;
	while(true) {
		cin >> posX;
		if (posX >= 0 && posX < m)
			break;
		cout << "X : ";
	}
	return posX;
}

//  y 
int inputPosY(int n) {
	cout << "Y : ";
	int posY;
	while(true) {
		cin >> posY;
		if (posY >= 0 && posY < n)
			break;
		cout << "Y : ";
	}
	return posY;
}

//  row, col  floor 
int** makeRoom(int n, int m) {
	int **room = new int*[n];

	for(int i = 0; i < n; i++)
		room[i] = new int[m];

	return room;	
}

// floor  tile 0 화
void initToZero(int **aRoom, int n, int m) {
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			aRoom[i][j] = 0;
		}
	}
}

//   cell  
bool isAllMoved(int **aRoom, int n, int m) {
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if (aRoom[i][j] == 0)
				return false;
		}
	}
	return true;
}

//  cell  
bool isBlocked(int n, int m, int ibug, int jbug) {
	if (ibug < 0 || ibug >= n ||
		jbug < 0 || jbug >= m)
		return true;
	return false;
}

//  random   
int moveRoach(int **aRoom, int n, int m, int ibug, int jbug) {
	int imove[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
	int jmove[8] = {1, 1, 1, 0, -1, -1, -1, 0};
	
	aRoom[ibug][jbug] = 1;
	
	int count = 0;

	while(true) {		
		int dir = rand() % 8;		
		if(!isBlocked(n, m, ibug+imove[dir], jbug+jmove[dir])) {
			ibug += imove[dir];
			jbug += jmove[dir];
			aRoom[ibug][jbug]++;
			count++;
			if (isAllMoved(aRoom, n, m))
				break;
		}
		if (count >= ITER_LIMIT)
			return -1;
	}
	return count;
}

//  cell 한 횟 
void printRoomCount(int **aRoom, int n, int m) {
	cout << "\n(2)The final count array:" << endl;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			cout << setw(3) << aRoom[i][j];
		}
		cout << endl;
	}
	cout << endl;
}

//   
void printNumOfMove(int count) {
	if (count == -1)
		cout << "\n(1)    " << endl;
	else
		cout << "\n(1)The total number of legal moves: " << count << endl;
}

// floor ()
void destroyRoom(int **aRoom, int n) {
	for(int i = 0; i < n; i++)
		delete []aRoom[i];
}

int main() {
	//   for random function
	srand(time(0));

	// floor 
	int n = inputY();
	int m = inputX();

	//   
	int ibug = inputPosX(m);
	int jbug = inputPosY(n);

	// floor   cell 0 .
	int **room = makeRoom(n, m);
	initToZero(room, n, m);

	//  
	int count = moveRoach(room, n, m, ibug, jbug);
	
	//   cell  
	printNumOfMove(count);
	printRoomCount(room, n, m);

	// floor ()
	destroyRoom(room, n);
	
	return 0;
}
- .

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