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










