U E D R , A S I H C RSS

Random Walk/종찬

No difference found



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

int main()
{	
	int size;
	int end=0;
	cout << "몇 칸 짜리? : ";
	cin >> size;
	int ** data = new int *[size];
	for (int i=0;i<size;i++) {
		data[i] = new int [size];
		for (int j=0;j<size;j++) {
			data[i][j]=0;
		}
	}
	srand((unsigned)time(NULL));
	int x=rand()%size;
	int y=rand()%size;

	while(end == 0) {
		int x_move = rand()%3;
		int y_move = rand()%3; // 이부분을 루프안에 안 넣어서 고생..
		end=1;
		data[x][y]++;
		if (x_move==0) {
			if (x>0)
			{
				x--;
				
			}
		}
		else if (x_move==2) {
			if (x<size-1) {
				x++;
			
			}
		}

		if (y_move==0) {
			if (y>0) {
				y--;
			
			}
		}
		else if (y_move==2) {
			if (y<size-1) {
				y++;
			
			}
		}
		for (int i=0;i<size;i++) {
			for (int j=0;j<size;j++) {
				if (data[i][j] ==0) {
					end=0;					
				}
			}
		}
	}
	int sum=0;
	for (int c=0; c<size;c++) {
		for (int d=0;d<size;d++)
			sum += data[c][d];
	}
	
	
	for (int a=0; a<size;a++) {
		for (int b=0;b<size;b++)
			cout << data[a][b] << "\t";
		cout << endl;		
	}
	cout << "합 : " << sum << endl;
	return 0;
} // by 종찬
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:51
Processing time 0.0330 sec