U E D R , A S I H C RSS

Random Walk/성재

No older revisions available

No older revisions available



~cpp 
Random Work...
무지하게 어려웠습니다.
코딩 줄수 줄이는게 최고가 아니란거 알았습니다..
방학동안 공부좀 열심히 해야겠네요..
도움만 잔뜩 받았습니다...이잉잉~~~

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

int main()
{
	cout<<"수를 입력하세요.(0-5)\n";
	bool quit = false;
	int num,b,c;
	int count,i,j;
	cin >> num;
	int ** data = new int *[num]; 
	for(i=0;i<num;i++) 
		data[i] = new int [num];
	srand((time(0)));
	b = rand() % num;
	c = rand() % num;	//end
	
	for(i=0;i<num;i++)
		for(j=0;j<num;j++)
			data[i][j] = 0;
	
	data[b][c] = 1;

	while(!quit)
	{
		quit = true;
		for(i=0;i<num;i++) {
			for(j=0;j<num;j++) {
				if(data[i][j] == 0)
					quit = false;
			}
		}

		int q = rand() % 8;	//end
		
		if(q==0) {
			if(b-1>=0 && c-1>=0)
				data[--b][--c] +=1;
		}
		else if(q==1) {
			if(b>=0 && b<num && c-1>=0)
				data[b][--c] +=1;
		}
		else if(q==2) {
			if(b+1<num && c-1>=0)
				data[++b][--c] +=1;
		}

		else if(q==3) {
			if(b+1< num && c>= 0 && c<num)			
				data[++b][c] +=1;
		}
		
		else if(q==4) {
			if(b+1<num && c+1<num)
				data[++b][++c] +=1;
		}
		else if(q==5) {
			if(c+1<num)
				data[b][++c] +=1;
		}
		else if(q==6) {
			if(b-1>=0 && c+1<num)
				data[--b][++c] +=1;
		}
		else if(q==7) {
			if(b>=0)
				data[--b][c] +=1;
		}
		
		count++;
	
	}
	
	for(i=0;i<num;i++) {
		for(j=0;j<num;j++) {
			cout << data[i][j] << "\t";
		}
		cout << endl;
	}

	for(i=0;i<num;i++) 
               delete [] data [i]; 
       delete [] data; 
 
		return 0;
}

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