Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

RandomWalk/영동: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0003 pages from live compare)
Line 18: Line 18:
  cout<<"숫자를 입력하시오: "<<endl;
  cout<<"숫자를 입력하시오: "<<endl;
  cin>>input;
  cin>>input;
  int **square=new int *[input];
  int **square=new int *[input];
  for(i=0;i<input;i++)
  for(i=0;i<input;i++)
  square[i]=new int [input];
  square[i]=new int [input];
  for(i=0;i<input;i++){
  for(i=0;i<input;i++){
  for(j=0;j<input;j++)
  for(j=0;j<input;j++)
  square[i][j]=0;}
  square[i][j]=0;}
   
   
  //시작점 결정
  //시작점 결정
Line 30: Line 30:
  srand((unsigned)time(NULL));
  srand((unsigned)time(NULL));
  b=rand()%input;
  b=rand()%input;
  square[a][b]=1;
  square[a][b]=1;
   
   
  //8방향 랜덤 이동에 대한 코드
  //8방향 랜덤 이동에 대한 코드
Line 39: Line 39:
  case 1:  //북서
  case 1:  //북서
  if(a-1!=-1 && b-1!=-1) {
  if(a-1!=-1 && b-1!=-1) {
  if(square[a-1][b-1] == 0)
  if(square[a-1][b-1] == 0)
  not_go++;
  not_go++;
  a--;
  a--;
  b--;
  b--;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 51: Line 51:
  case 2:  //북
  case 2:  //북
  if(a-1!=-1){
  if(a-1!=-1){
  if(square[a-1][b] == 0)
  if(square[a-1][b] == 0)
  not_go++;
  not_go++;
  a--;
  a--;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 63: Line 63:
  case 3:  //북동
  case 3:  //북동
  if(a-1!=-1 && b+1!=input){
  if(a-1!=-1 && b+1!=input){
  if(square[a-1][b+1] == 0)
  if(square[a-1][b+1] == 0)
  not_go++;
  not_go++;
  a--;
  a--;
  b++;
  b++;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 76: Line 76:
  case 4:  //서
  case 4:  //서
  if(b-1!=-1){
  if(b-1!=-1){
  if(square[a][b-1] == 0)
  if(square[a][b-1] == 0)
  not_go++;
  not_go++;
  b--;
  b--;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 88: Line 88:
  case 5:  //동
  case 5:  //동
  if(b+1!=input){
  if(b+1!=input){
  if(square[a][b+1] == 0)
  if(square[a][b+1] == 0)
  not_go++;
  not_go++;
  b++;
  b++;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 100: Line 100:
  case 6:  //남서
  case 6:  //남서
  if(b-1!=-1 && a+1!=input){
  if(b-1!=-1 && a+1!=input){
  if(square[a+1][b-1] == 0)
  if(square[a+1][b-1] == 0)
  not_go++;
  not_go++;
  a++;
  a++;
  b--;
  b--;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 113: Line 113:
  case 7:  //남
  case 7:  //남
  if(a+1!=input){
  if(a+1!=input){
  if(square[a+1][b] == 0)
  if(square[a+1][b] == 0)
  not_go++;
  not_go++;
  a++;
  a++;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 125: Line 125:
  case 8:  //남동  
  case 8:  //남동  
  if(a+1!=input && b+1!=input){
  if(a+1!=input && b+1!=input){
  if(square[a+1][b+1] == 0)
  if(square[a+1][b+1] == 0)
  not_go++;
  not_go++;
  a++;
  a++;
  b++;
  b++;
  square[a][b]++;
  square[a][b]++;
  }
  }
  else
  else
Line 142: Line 142:
  for(i=0;i<input;i++){
  for(i=0;i<input;i++){
  for(j=0;j<input;j++)
  for(j=0;j<input;j++)
  cout<<square[i][j]<<"\t";
  cout<<square[i][j]<<"\t";
  cout<<"\n";
  cout<<"\n";
  }
  }
Line 152: Line 152:
  //동적할당한 것 지움
  //동적할당한 것 지움
  for(i=0;i<input;i++)
  for(i=0;i<input;i++)
  delete[] square[i];
  delete[] square[i];
  delete [] square;
  delete [] square;
   
   
  return 0;
  return 0;
Line 167: Line 167:
  const int MAX_Y=5;  
  const int MAX_Y=5;  
  const int DIRECTION=8;  
  const int DIRECTION=8;  
  const int MOVE_X[DIRECTION]={0, 1, 1, 1, 0, -1, -1, -1};  
  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};  
  const int MOVE_Y[DIRECTION]={-1, -1, 0, 1, 1, 1, 0, -1};  
    
    
  struct Bug  
  struct Bug  
Line 177: Line 177:
  };  
  };  
    
    
  void showBoard(int a_board[][MAX_X], int length_x, int length_y);  
  void showBoard(int a_board[][MAX_X], int length_x, int length_y);  
  void move(Bug & a_bug);  
  void move(Bug & a_bug);  
  void askLocationOfBug(Bug & a_bug);  
  void askLocationOfBug(Bug & a_bug);  
  void increaseEndCount(int & a_count, Bug & a_bug, int a_board[][MAX_X]);  
  void increaseEndCount(int & a_count, Bug & a_bug, int a_board[][MAX_X]);  
  void makeFootprint(Bug & a_bug, int a_board[][MAX_X]);  
  void makeFootprint(Bug & a_bug, int a_board[][MAX_X]);  
  bool isEnd(int a_count);  
  bool isEnd(int a_count);  
  bool isInBoard(Bug & a_bug0);  
  bool isInBoard(Bug & a_bug0);  
Line 187: Line 187:
  void main()  
  void main()  
  {  
  {  
         int board[MAX_Y][MAX_X]={{0,}}; // 판  
         int board[MAX_Y][MAX_X]={{0,}}; // 판  
         int count=0; // 종료 조건  
         int count=0; // 종료 조건  
         Bug bug; // 바퀴벌레의 위치  
         Bug bug; // 바퀴벌레의 위치  
Line 204: Line 204:
  }  
  }  
    
    
  void showBoard(int a_board[][MAX_X], int length_x, int length_y)  
  void showBoard(int a_board[][MAX_X], int length_x, int length_y)  
  {  
  {  
         for(int i=0;i<length_y;i++)  
         for(int i=0;i<length_y;i++)  
         {  
         {  
                 for(int j=0;j<length_x;j++)  
                 for(int j=0;j<length_x;j++)  
                         cout<<a_board[i][j]<<"\t";  
                         cout<<a_board[i][j]<<"\t";  
                 cout<<endl;  
                 cout<<endl;  
         }  
         }  
Line 220: Line 220:
         if(isInBoard(a_bug))  
         if(isInBoard(a_bug))  
         {  
         {  
                 a_bug.x=a_bug.x+MOVE_X[a_bug.way];  
                 a_bug.x=a_bug.x+MOVE_X[a_bug.way];  
                 a_bug.y=a_bug.y+MOVE_Y[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])  
  void increaseEndCount(int & a_count, Bug & a_bug, int a_board[][MAX_X])  
  {  
  {  
         if(a_board[a_bug.y][a_bug.x]==0)  
         if(a_board[a_bug.y][a_bug.x]==0)  
                 a_count++;  
                 a_count++;  
  }  
  }  
  bool isInBoard(Bug & a_bug0)  
  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)  
         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;  
                 return true;  
         else  
         else  
Line 250: Line 250:
                 return false;  
                 return false;  
  }  
  }  
  void makeFootprint(Bug & a_bug, int a_board[][MAX_X])  
  void makeFootprint(Bug & a_bug, int a_board[][MAX_X])  
  {  
  {  
         a_board[a_bug.y][a_bug.x]++;  
         a_board[a_bug.y][a_bug.x]++;  
  }
  }
== 최근에 객체지향으로 짠 것(2003/08/20) ==
== 최근에 객체지향으로 짠 것(2003/08/20) ==
Line 261: Line 261:
  {
  {
  private:
  private:
  int board[MAX_X][MAX_Y];
  int board[MAX_X][MAX_Y];
  int count;
  int count;
  public:
  public:
Line 272: Line 272:
=== Bug.h ===
=== Bug.h ===
  const int DIRECTION=8;
  const int DIRECTION=8;
  const int MOVE_X[DIRECTION]={0, 1, 1, 1, 0, -1, -1, -1};   
  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};  
  const int MOVE_Y[DIRECTION]={-1, -1, 0, 1, 1, 1, 0, -1};  
  class Bug
  class Bug
  {
  {
Line 299: Line 299:
  for(int j=0;j<5;j++)
  for(int j=0;j<5;j++)
  {
  {
  board[i][j]=0;
  board[i][j]=0;
  }
  }
  }
  }
Line 310: Line 310:
  for(int j=0;j<5;j++)
  for(int j=0;j<5;j++)
  {
  {
  cout<<board[i][j]<<"\t";
  cout<<board[i][j]<<"\t";
  }
  }
  cout<<endl;
  cout<<endl;
Line 317: Line 317:
  void Board::makeFootprint(int a_x, int a_y)
  void Board::makeFootprint(int a_x, int a_y)
  {
  {
  board[a_y][a_x]++;
  board[a_y][a_x]++;
  }
  }
  void Board::increaseEndCount(int a_x, int a_y)
  void Board::increaseEndCount(int a_x, int a_y)
  {
  {
  if(board[a_y][a_x]==0)   
  if(board[a_y][a_x]==0)   
  count++;   
  count++;   
  }
  }
Line 345: Line 345:
  bool Bug::isInBoard()
  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)
  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;
  return true;
  else
  else
Line 355: Line 355:
  if(isInBoard())   
  if(isInBoard())   
  {   
  {   
  x=x+MOVE_X[way];   
  x=x+MOVE_X[way];   
  y=y+MOVE_Y[way];   
  y=y+MOVE_Y[way];   
  }  
  }  
  }
  }
Line 390: Line 390:
----
----
[[RandomWalk]]
[[RandomWalk]]

Revision as of 00:29, 27 March 2026

1학년 데블스캠프 때 짠 것

//랜덤 워크
#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;
}

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

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

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

#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]=Template: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]++; 
}

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

Board.h

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;}
};

Bug.h

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);
};

Board.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++;  
}

Bug.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;  
}

RandomWalk.cpp (Main함수)

#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


RandomWalk