료 ¶
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(); // }
... 디를 ... 막막...
부를 ㅡ_ㅡ^
부를 ㅡ_ㅡ^