ToyProblems 에서 느낀바가 있어 RandomWalk 를 여러가지 방법으로 풀어보려 합니다. [[TableOfContents]] === 소스 1 : 2학년때 자료구조 숙제로 작성 (약간 Iterative한것 같다) === {{{~cpp #include #include #include using namespace std; // move direction and board. int direction_x[]={-1,0,1,1,1,0,-1,-1}; int direction_y[]={1,1,1,0,-1,-1,-1,0}; int board[40][20]; // maximum size of the board is : 40x20 // required variables. int sizeX, sizeY, xPos, yPos, NumOfBlock, loop=0; const int DIRECTION = 8; void input(); void output(); void move(); void init_board(); int main() { init_board(); input(); move(); output(); return 0; } void init_board() { for(int i=0; isizeX-1 || yPos+direction_y[k]<0 || yPos+direction_y[k]>sizeY-1) continue; xPos += direction_x[k]; yPos += direction_y[k]; if(board[xPos][yPos]==0) NumOfBlock--; board[xPos][yPos]++; if(NumOfBlock==0) break; loop++; } } void input() { cout << "input size of the board" << endl; while(cin >> sizeX >> sizeY) { if(sizeX>40 || sizeY>20) { cout << "size is out of range.\ntry again."; continue; } break; } NumOfBlock=sizeX*sizeY; cout << "input start point;" << endl; while(cin >> xPos >> yPos) { if(xPos>40 || yPos>20) { cout << "point is out of range.\ntry again."; continue; } break; } } void output() { int i,j; // print outputs. for(i=0; i #include #include using namespace std; #define DIRECTION 8 // move direction and board. int direction_x[]={-1,0,1,1,1,0,-1,-1}; int direction_y[]={1,1,1,0,-1,-1,-1,0}; int sizeX, sizeY; int NumberOfUnvisitedBlocks = 0, LoopCount= 0 ; vector > board; void printOutResult(); void moveRoach(int x, int y); int main() { cout << "Size of the board : "; cin >> sizeX >> sizeY; NumberOfUnvisitedBlocks = sizeX * sizeY; // initizlize the vector board.resize(sizeX); vector >::iterator iter; for(iter=board.begin(); iter!=board.end(); ++iter) (*iter).resize(sizeY); cout << "Starting Point : "; int xPos, yPos; cin >> xPos >> yPos; moveRoach(xPos, yPos); printOutResult(); return 0; } void moveRoach(int xPos, int yPos) { int k; srand((unsigned)time(NULL)); // initial point board[xPos][yPos]++; NumberOfUnvisitedBlocks--; // move roach while 'loop' is smaller than 50,000 while(LoopCount<50000) { k = rand()%DIRECTION; // prevent the roach moves outside of the board if(xPos+direction_x[k]<0 || xPos+direction_x[k]>sizeX-1 || yPos+direction_y[k]<0 || yPos+direction_y[k]>sizeY-1) continue; xPos += direction_x[k]; yPos += direction_y[k]; if(board[xPos][yPos]==0) NumberOfUnvisitedBlocks--; board[xPos][yPos]++; if(NumberOfUnvisitedBlocks==0) break; LoopCount++; } } void printOutResult() { int i,j; // print outputs. for(i=0; i #include #include using namespace std; #define NUMOFDIRECTIONS 8 typedef vector > Board; Board roachJourney; int dir_x[]={-1,0,1,1,1,0,-1,-1}; int dir_y[]={1,1,1,0,-1,-1,-1,0}; int sizeX, sizeY; void moveRoachRecursively(int dirIdx); void printOutResult(); int main() { cout << "Size of the board : "; cin >> sizeX >> sizeY; roachJourney.resize(sizeX); Board::iterator iter; for(iter=roachJourney.begin(); iter!=roachJourney.end(); ++iter) (*iter).resize(sizeY); moveRoachRecursively(rand()%NUMOFDIRECTIONS); printOutResult(); return 0; } void moveRoachRecursively(int dirIdx) { // starting point is 0,0 // i'm not sure that this is an excessive use of 'static' keyword static int posX = 0, posY = 0; static int numOfUnVstdPlces = sizeX*sizeY; // if the roach moved outside of the board ignore that case. if(! (posX+dir_x[dirIdx]<0 || posX+dir_x[dirIdx]>sizeX-1 || posY+dir_y[dirIdx]<0 || posY+dir_y[dirIdx]>sizeY-1) ) { posX += dir_x[dirIdx]; posY += dir_y[dirIdx]; if( roachJourney[posX][posY] == 0 ) numOfUnVstdPlces--; roachJourney[posX][posY]++; } if( numOfUnVstdPlces != 0) // choose next direction moveRoachRecursively((unsigned)(rand())%NUMOFDIRECTIONS); else return; } void printOutResult() { Board::iterator iterX; for(iterX=roachJourney.begin(); iterX!=roachJourney.end(); ++iterX) { vector::iterator iterY; for(iterY=(*iterX).begin(); iterY!=(*iterX).end(); ++iterY) { cout.width(4); cout << (*iterY); } cout << endl; } } }}} === 소스 4 : OO 적으로 === - 별로 OO 적이지 못한것 같다...(Roach 와 Board 객체가 [양방향참조]를 한다). DesignPatterns 를 참고하면서 보았어야 하는데.. 나중에 [Refactoring] 해야 함.. * {{{~cpp Roach.java }}} {{{~cpp import java.util.Random; public class Roach{ int dir_x[]={-1,0,1,1,1,0,-1,-1}; int dir_y[]={1,1,1,0,-1,-1,-1,0}; Board _board; boolean bOnTrip; Random rand; public Roach() { bOnTrip = true; rand = new Random(); } public void move() { int randNum; while( bOnTrip ) { randNum = rand.nextInt()%dir_x.length; try { _board.walkOn(dir_x[randNum], dir_y[randNum]); } catch (ArrayIndexOutOfBoundsException e) { //System.err.println(e); drink(); } } } public void finishJourney() { bOnTrip = false; } public void drink() { System.out.println("toast~!"); } public void putOnTheBoard(Board board) { _board = board; } } }}} * {{{~cpp Board.java }}} {{{~cpp public class Board { int _board[][]; int sizeX, sizeY; int roaX, roaY; // watch position of the roach // because the roach doesn't know his position int visitedPlace = 1; Roach _roach; public Board(int x, int y) { sizeX = x; sizeY = y; roaX = roaY = 0; _board = new int[sizeX][sizeY]; } public void walkOn(int x, int y) { System.out.println("바퀴위치 : " + (roaX+x) + ", " + (roaY+y)); if ( _board[roaX+x][roaY+y] == 0 ) visitedPlace++; roaX += x; roaY += y; System.out.println("바퀴위치 : " + roaX + ", " + roaY); _board[roaX][roaY]++; if( visitedPlace == sizeX*sizeY ) { _roach.finishJourney(); printOutBoard(); } } public void putRoachOn(Roach roach) { _roach = roach; } public void printOutBoard() { for(int i=0; i