U E D R , A S I H C RSS

Random Walk/김아영

~cpp 
#include <iostream>
#include <ctime>
using namespace std;

const int max = 12;

int main()
{
	int board[max][max]={0,};
	int x, y, arsize, end=1;
	int direction;
	int total=0;
	
	cout << "판의 크기를 입력하세요(12이하) : " ;
	cin >> arsize ;

	cout << "시작점의 x좌표를 입력하세요 : " ;
	cin >> x ;
	cout << "시작점의 y좌표를 입력하세요 : " ;
	cin >> y ;

	srand((unsigned)time(NULL));
	
	board[x][y]=1;
	
	do
	{
	direction=rand()%8;
	
		switch(direction)
		{
		case 0 : if(x!=0 && y!=0)
				 {		x--;
						y--;
				 }
				 break;

		case 1 : if(y!=0)
				 {
						y--;
				 }
				 break;

		case 2 : if(x!=arsize-1 && y!=0)
				 {
						x++;
						y--;
				 }
				 break;

		case 3 : if(x!=arsize-1)
				 {
						x++;
				 }
				 break;

		case 4 : if(x!=arsize-1 && y!=arsize-1)
				 {
						x++;
						y++;
				 }
				 break;

		case 5 : if(y!=arsize-1)
				 {
						y++;
				 }
				 break;

		case 6 : if(x!=0 && y!=arsize-1)
				 {
						x--;
						y++;
				 }
				 break;

		case 7 : if(x!=0)
				 {	
						x--;
				 }
				 break;
		}

	if(board[x][y]==0)
		end++;
	board[x][y]++;
	
	total++;
	
	}while(end<arsize*arsize);

	for(int i=0;i<arsize;i++)
	{
		for(int j=0;j<arsize;j++)
			cout <<	board[j][i] << "\t";
		cout << endl;
	}
	
	cout << endl << "총 이동 횟수는 " << total << "입니다." << endl ;

	return 0 ;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:50
Processing time 0.0268 sec