U E D R , A S I H C RSS

Random Walk/영동

1. 1학년 데블스캠프 때 짠 것

~cpp 
//랜덤 워크
#include<iostream.h>
#include<stdlib.h>
#include<time.h>

int main()
{
	int i, j;
	int a, b;
	int way;//이동 방향
	int count=1;//이동 횟수
	int not_go=1;//아직 가지 않은 곳을 셀 때 쓰는 수 
	int input;
	//숫자 입력받고 동적할당
	cout<<"Random Walk"<<endl;
	cout<<"숫자를 입력하시오: "<<endl;
	cin>>input;
	int **square=new int *[input];
	for(i=0;i<input;i++)
		square[i]=new int [input];
	for(i=0;i<input;i++){
		for(j=0;j<input;j++)
			square[i][j]=0;}

	//시작점 결정
	srand((unsigned)time(NULL));
	a=rand()%input;
	srand((unsigned)time(NULL));
	b=rand()%input;
	square[a][b]=1;

	//8방향 랜덤 이동에 대한 코드
	do{
		way=rand()%8+1;
		switch(way)
		{
			case 1:  //북서
				if(a-1!=-1 && b-1!=-1) {
					if(square[a-1][b-1] == 0)
						not_go++;
					a--;
					b--;
					square[a][b]++;
				}
				else
					continue;
				count++;
				break;
			case 2:  //북
				if(a-1!=-1){
					if(square[a-1][b] == 0)
						not_go++;	
					a--;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
			case 3:  //북동
				if(a-1!=-1 && b+1!=input){
					if(square[a-1][b+1] == 0)
						not_go++;
					a--;
					b++;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
			case 4:  //서
				if(b-1!=-1){
					if(square[a][b-1] == 0)
						not_go++;
					b--;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
			case 5:  //동
				if(b+1!=input){
					if(square[a][b+1] == 0)
						not_go++;	
					b++;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
			case 6:  //남서
				if(b-1!=-1 && a+1!=input){
					if(square[a+1][b-1] == 0)
						not_go++;
					a++;
					b--;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
			case 7:  //남
				if(a+1!=input){
					if(square[a+1][b] == 0)
						not_go++;
					a++;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
			case 8:  //남동 
				if(a+1!=input && b+1!=input){
					if(square[a+1][b+1] == 0)
						not_go++;
					a++;
					b++;
					square[a][b]++;
				}
				else
					continue;

				count++;
				break;
	}
	}while(not_go < input * input);

	//각 장소의 이동 횟수 출력.
	for(i=0;i<input;i++){
		for(j=0;j<input;j++)
			cout<<square[i][j]<<"\t";
		cout<<"\n";
	}

	
	//이동 횟수 출력	
	cout<<count<<"회 이동\n";

	//동적할당한 것 지움
	for(i=0;i<input;i++)
		delete[] square[i];
	delete [] square;

	return 0;
}

2. 1학년 2학기 때 자바로 짠 것

JavaStudy2002/영동-2주차 <-지금보니 상당히 허접하네요.

3. 최근에 짠 것(2003/08/07)

~cpp 
#include<iostream> 
#include<ctime> 
using namespace std; 
 
const int MAX_X=5; 
const int MAX_Y=5; 
const int DIRECTION=8; 
const int MOVE_X[DIRECTION]={0, 1, 1, 1, 0, -1, -1, -1}; 
const int MOVE_Y[DIRECTION]={-1, -1, 0, 1, 1, 1, 0, -1}; 
 
struct Bug 
{ 
        int x; 
        int y; 
        int way; 
}; 
 
void showBoard(int a_board[][MAX_X], int length_x, int length_y); 
void move(Bug & a_bug); 
void askLocationOfBug(Bug & a_bug); 
void increaseEndCount(int & a_count, Bug & a_bug, int a_board[][MAX_X]); 
void makeFootprint(Bug & a_bug, int a_board[][MAX_X]); 
bool isEnd(int a_count); 
bool isInBoard(Bug & a_bug0); 
 
void main() 
{ 
        int board[MAX_Y][MAX_X]={{0,}}; // 판 
        int count=0; // 종료 조건 
        Bug bug; // 바퀴벌레의 위치 
 
        askLocationOfBug(bug); 
        makeFootprint(bug, board); 
        //시작점에서 발자국을 찍고 시작. 
 
        do{              
                move(bug); 
                increaseEndCount(count, bug, board); 
                makeFootprint(bug, board); 
        }while(isEnd(count)); 
 
        showBoard(board, MAX_X, MAX_Y);  
} 
 
void showBoard(int a_board[][MAX_X], int length_x, int length_y) 
{ 
        for(int i=0;i<length_y;i++) 
        { 
                for(int j=0;j<length_x;j++) 
                        cout<<a_board[i][j]<<"\t"; 
                cout<<endl; 
        } 
        cout<<endl; 
} 
void move(Bug & a_bug) 
{ 
        a_bug.way=rand()%DIRECTION; 
 
        if(isInBoard(a_bug)) 
        { 
                a_bug.x=a_bug.x+MOVE_X[a_bug.way]; 
                a_bug.y=a_bug.y+MOVE_Y[a_bug.way]; 
        } 
} 
void increaseEndCount(int & a_count, Bug & a_bug, int a_board[][MAX_X]) 
{ 
        if(a_board[a_bug.y][a_bug.x]==0) 
                a_count++; 
} 
bool isInBoard(Bug & a_bug0) 
{ 
        if(a_bug0.x+MOVE_X[a_bug0.way]>-1 && a_bug0.x+MOVE_X[a_bug0.way]<5 && a_bug0.y+MOVE_Y[a_bug0.way]>-1 && a_bug0.y+MOVE_Y[a_bug0.way]<5) 
                return true; 
        else 
                return false; 
} 
void askLocationOfBug(Bug & a_bug) 
{ 
        cout<<"바퀴벌레의 x좌표와 y좌표를 입력하시오. <x y>"; 
        cin>>a_bug.x; 
        cin>>a_bug.y; 
		srand((unsigned)time(NULL)); 
} 
bool isEnd(int a_count) 
{ 
        if(a_count<(MAX_X*MAX_Y-1)) 
                return true; 
        else 
                return false; 
} 
void makeFootprint(Bug & a_bug, int a_board[][MAX_X]) 
{ 
        a_board[a_bug.y][a_bug.x]++; 
}

4. 최근에 객체지향으로 짠 것(2003/08/20)

4.1. Board.h

~cpp 
const int MAX_X=5;
const int MAX_Y=5;
class Board
{
private:
	int board[MAX_X][MAX_Y];
	int count;
public:
	Board();
	void showBoard();
	void makeFootprint(int a_x, int a_y);
	void increaseEndCount(int a_x, int a_y);
	int returnCount(){return count;}
};

4.2. Bug.h

~cpp 
const int DIRECTION=8;
const int MOVE_X[DIRECTION]={0, 1, 1, 1, 0, -1, -1, -1};  
const int MOVE_Y[DIRECTION]={-1, -1, 0, 1, 1, 1, 0, -1}; 
class Bug
{
private:
	int x;
	int y;
	int way;
public:
	Bug(){}
	Bug(int a_x, int a_y);
	int returnX(){return x;}
	int returnY(){return y;}
	void move();
	void askLocationOfBug();
	bool isInBoard();
	bool isEnd(int a_count);
};

4.3. Board.cpp

~cpp 
#include<iostream.h>
#include"Board.h"
Board::Board()
{
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			board[i][j]=0;
		}
	}
	count=0;
}
void Board::showBoard()
{
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			cout<<board[i][j]<<"\t";
		}
		cout<<endl;
	}
}
void Board::makeFootprint(int a_x, int a_y)
{
	board[a_y][a_x]++;
}
void Board::increaseEndCount(int a_x, int a_y)
{
	if(board[a_y][a_x]==0)  
		count++;  
}

4.4. Bug.cpp

~cpp 
#include<iostream>
#include<ctime>
#include"Bug.h"
using namespace std;
const int MAX_X=5;
const int MAX_Y=5; 
Bug::Bug(int a_x, int a_y)
{
	x=a_x;
	y=a_y;
}
void Bug::askLocationOfBug()
{
	cout<<"바퀴벌레의 초기 위치를 입력하세요. <x, y>"<<endl;
	cin>>x>>y;
	cout<<"바퀴벌레의 초기 위치는 "<<x<<", "<<y<<"입니다.\n";
	srand((unsigned)time(NULL));
}
bool Bug::isInBoard()
{
	if(x+MOVE_X[way]>-1 && x+MOVE_X[way]<MAX_X && y+MOVE_Y[way]>-1 && y+MOVE_Y[way]<MAX_Y)
		return true;
	else
		return false;
}
void Bug::move()
{
	way=rand()%DIRECTION;
	if(isInBoard())  
	{  
		x=x+MOVE_X[way];  
		y=y+MOVE_Y[way];  
	} 
}
bool Bug::isEnd(int a_count)
{
	if(a_count<(MAX_X*MAX_Y-1))  
		return true;  
	else  
		return false;  
}

4.5. RandomWalk.cpp (Main함수)

~cpp 
#include<iostream>
#include"Board.h"
#include"Bug.h"
using namespace std;
void main()
{
	Board board;
	Bug bug;
	bug.askLocationOfBug();
	board.makeFootprint(bug.returnX(), bug.returnY());

	do{
		bug.move();
		board.increaseEndCount(bug.returnX(), bug.returnY());
		board.makeFootprint(bug.returnX(), bug.returnY());	
	}while(bug.isEnd(board.returnCount()));
	
	board.showBoard();
}

작성자: Yggdrasil

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