U E D R , A S I H C RSS

Random Walk2/영동

사실 이제 Random도 아니죠... Scheduled에 가깝겠죠.
~cpp 
//RandomWalk2 
//Random Walk 
#include<iostream.h> 
int main() 
{ 
        //변수선언==================================== 
        char way[20];//이동하는 방향(8방향: 0~7) 
        int input_y, input_x;//좌표의 총개수 
        int start_y, start_x;//시작점의 좌표
		int journey=0;
        int y, x;//좌표의 현재 위치 
        int i, j;//for문 돌리는 데 필요한 변수 
        int count=0;//총 움직인 수 
        int first_go=0; 

		for(i=0;i<20;i++)
			way[i]='\0';
 
        //x, y 총좌표수를 입력받고 배열생성, 초기화=== 
        cout<<"\n  x좌표 수를 입력하시오: "; 
        cin>>input_x; 
        cout<<"\n  y좌표 수를 입력하시오: "; 
        cin>>input_y; 
        int **base=new int *[input_y];  
        for(i=0;i<input_x;i++)  
			base[i]=new int [input_x];  
		for(i=0;i<input_y;i++){  
			for(j=0;j<input_x;j++)  
				base[i][j]=0;}  
 
        //시작점 입력, 대입=========================== 
        cout<<"\n  시작점의 x좌표를 입력하시오: "; 
        cin>>start_x; 
        cout<<"\n  시작점의 y좌표를 입력하시오: "; 
        cin>>start_y; 
        y=start_y; 
        x=start_x; 
        base[y][x]=1;//시작점은 이미 한 번 왔으므로 1 
        first_go++; 
 
		//방향 입력=================================== 
        cout<<"갈 방향 입력: "; 
        cin>>way; 
        do{ 
        //방향 처리=================================== 
			switch(way[journey]){ 
				case '0'://북 
					if(y==0) 
						y=input_y-1;     
					else 
						y--; 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '1'://북동 
					if(y==0) 
						y=input_y-1; 
					else if(x==input_x-1) 
						x=0; 
					else if(y==0 && x==input_x-1){ 
						x=0;  
						y=input_y-1;} 
					else{ 
						y--; 
						x++;} 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '2'://동 
					if(x==input_x-1) 
						x=0; 
					else 
						x++; 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '3'://남동 
					if(y==input_y-1) 
						y=0; 
					else if(x==input_x-1) 
						x=0; 
					else if(y==input_y-1 && x==input_x-1){ 
						y=0; 
						x=0;} 
					else{ 
						y++; 
						x++;} 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '4'://남 
					if(y==input_y-1) 
						y=0; 
					else 
						y++; 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '5'://남서 
					if(y==input_y-1) 
						y=0; 
					else if(x==0) 
						x=input_x-1; 
					else if(y==input_y-1 && x==0){ 
						y=0; 
						x=input_x-1;} 
					else{ 
						y++; 
						x--;} 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '6'://서 
					if(x==0) 
						x=input_x-1; 
					else 
						x--; 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				case '7'://북서 
					if(y==0) 
						y=input_y-1; 
					else if(x==0) 
						x=input_x-1; 
					else if(y==0 && x==0){ 
						y=input_y-1; 
						x=input_x-1;} 
					else{ 
						y--; 
						x--;
					} 
					if(base[y][x]==0) 
						first_go++; 
					base[y][x]++; 
					count++; 
					journey++;
					break; 
				default: 
					break;
					}

    }while(way[journey]!='\0'); 
	cout<<"\n총 이동 횟수는 "<<count<<"회\n"; 
    for(i=0;i<input_y;i++){ 
		for(j=0;j<input_x;j++) 
			cout<<base[i][j]<<" "; 
        cout<<"\n";} 
         
        //동적할당한 것 지움  
    for(i=0;i<input_x;i++)  
		delete[] base[i];  
    delete [] base;  
 
    return 0; 
} 

작성자: Yggdrasil

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