료구 ¶
Randomwalk Problem ¶
~cpp == Roach.h == //20041004 김 const int Direction = 8; // 바벌가 는 8방 const int imove[8] = {-1,0,1,1,1,0,-1,-1}; // 바벌가 는 방 x 감 const int jmove[8] = {1,1,1,0,-1,-1,-1,0}; // 바벌가 는 방 y 감 int Tile[40][20] = {0,}; // 바벌가 돌다는 배 int size_x, size_y; // x, y 갯 int count,t,R_count=0; // 바벌 동, 랜덤 , 바벌가 모든 돌다록 는 class Roach { private: int Pos_x, Pos_y; // 바벌 public: void Initiation(); // 기 void Input(); // 바벌 력 void Walk(); // 바벌가 는 메드 void show(); // 결과값 력 }; void Roach :: Initiation() // 기 { cout << "가로 기를 . ( 2 < x <=40 ) \n"; while(!(cin >> size_x) || size_x<3 || size_x > 40) cerr << "못된 값 력다. 다 력.\n"; cout << "로 기를 . ( 2 < y <=20 ) \n"; while(!(cin >> size_y) || size_y < 3 || size_y >20) cerr << "못된 값 력다. 다 력.\n"; }; void Roach :: Input() // 바벌 력 { int i,j; cout << "x 를 \n"; cin >> i; cout << "y 를 \n"; cin >> j; Pos_x = i; Pos_y = j; }; void Roach :: Walk() { t=rand()%Direction; // 랜덤로 바벌가 방 다 if(Pos_x + imove[t] >= 0 && Pos_x + imove[t] < size_x) // 바벌가 밖로 벗나 록 는 Pos_x += imove[t]; // 바벌 x 를 if(Pos_y + jmove[t] >= 0 && Pos_y + jmove[t] < size_y) Pos_y += jmove[t]; // 바벌 y 를 count++; // 바벌가 를 구기 if(Tile[Pos_x][Pos_y]==0) R_count++; // 바벌가 모든 방문면 게 기 Tile[Pos_x][Pos_y]++; // 바벌가 방문 때다 가 방문 를 구다. }; void Roach :: show() { cout << "바벌 동는 " << count << " 다.\n"; cout << " 는 " << Pos_x << " , " << Pos_y << "다.\n"; cout << "각 방문 는 다과 같다. \n"; for(int m=0; m < size_x; m++){ for(int n=0; n < size_y; n++) cout << Tile[m][n] << "\t"; cout << endl; } } //20041004 김 #include <iostream.h> #include <cstdlib> #include <ctime> #include "Roach.h" void main() { srand((unsigned)time(NULL)); // 랜덤 Roach Hong; // Hong 라는 Roach 래를 Hong.Initiation(); // 배 기 Hong.Input(); // 바벌 while(R_count!=size_x*size_y || count < 50000) // 바벌가 모든 면 나 가 50000 면 게 다 Hong.Walk(); // 바벌 동 메드 Hong.show(); // 결과값 력 }
게... 디를 고 ... 막막...
공부를 다 ㅡ_ㅡ^
공부를 다 ㅡ_ㅡ^