U E D R , A S I H C RSS

randomwalk/홍선


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


... ... ...
ㅡ_ㅡ^

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:44
Processing time 0.0102 sec