U E D R , A S I H C RSS

Random Walk2/현민

No older revisions available

No older revisions available



~cpp 
#include <iostream>
using namespace std;

int main()
{
	int M,N,i,j;
	cout << "행과 열의 크기를 입력해 주십시오 : ";
	cin >> M >> N;
	int **board = new int *[M];
	for (i=0; i<M; i++)
		board[i] = new int [N];
	
	for (i=0; i<M; i++)
		for (j=0; j<N; j++)
			board[i][j] = 0;

	cout << "처음 시작할 위치의 좌표를 입력해 주십시오 : ";
	int start_x, start_y;
	cin >> start_x >> start_y;
	
	cout << "이동할 행로들을 입력하십시오 : ";
	char ch[100];
	cin.get();
	cin.getline(ch,100);
	int num = strlen(ch);
	char *A = new char [num+1];
	for (i=0; i<num+1; i++)
		A[i] = '0';
	
	int *route = new int[num+1];
	for (i=0; i<num+1; i++)
	{
		A[i] = ch[i];
		route[i] = A[i] - 48;
	}
	
	int walker_x[8] = {0,1,1,1,0,-1,-1,-1};
	int walker_y[8] = {-1,-1,0,1,1,1,0,-1};
	
	int next_x = start_x - 1, next_y = start_y - 1; 
	i=0;
	int check = 1;
	int count = 0;
	while (A[i]!='\0' && check > 0)
	{
		check = 0;
		next_x = next_x + walker_x[route[i]];
		next_y = next_y + walker_y[route[i]];
		if (next_x == -1)
			next_x = N-1;
		if (next_x == N)
			next_x = 0;
		if (next_y == -1)
			next_y = M-1;
		if (next_y == M)
			next_y = 0;
		board[next_y][next_x]++; count++;
		i++;
		
		for (int a=0; a<M; a++) 
		{ 
            for (int b=0; b<N; b++) 
            { 
				if(board[a][b] == 0) 
					check++; 
            } 
        }


	}

	cout << "총 움직인 횟수는 " << count << "번 입니다.\n\n";
	for (i=0; i<M; i++)
	{
		for (j=0; j<N; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
		
	}
	
	return 0;

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