== 렌덤워크 == {{{~cpp #include #include // For rand() and srand() #include //For time(0) using namespace std; int count[40][20]; //maximum size of the count array is 40*20 //move dirction int imove[8] = {-1,0,1,1,1,0,-1,-1}; int jmove[8] = {1,1,1,0,-1,-1,-1,0}; //variable int counter = 0; int ibug, jbug; //start point(x,y) int n,m; //array's range is n*m //funtion void input(); void init(); void move(); bool is_end(); void output(); int main() { init(); //inital count array input(); //input the board size move(); //cackroach move the board output(); //print result about count_array and Total_count_Number return 0; } void init() //inital count array { for(int i=0; i>n>>m) { //size range : 240 ) || ( m<=2 || m>40 ) ) { cout<<"the board's size is out of range. try again :"; continue; } break; } cout<<"input start point : "; while(cin>>ibug>>jbug) { //point range : 240 ) || ( jbug<=0 || jbug>40 ) ) { cout<<"start point is out of range. try again:"; continue; } break; } } void move() { count[ibug][jbug]++; //inital the start point srand(time(0)); //for make random number int k; //random variable while( is_end() ) { k = rand()%8; // random value range :0~7 //the board's range if( (ibug+imove[k]) >=0 && (ibug+imove[k]) < n && (jbug+jmove[k])>=0 && (jbug+jmove[k]) < m ) { //벌레의 이동한 좌표 ibug += imove[k]; jbug += jmove[k]; count[ibug][jbug]++;//check the visiting room. counter++; } else continue;//범위밖으로 나가면 다시 랜덤값 결정. } } bool is_end() //각방을 다 방문했는지를 검사 { if(counter >= 50000) //excute limitation return false; for (int i =0; i