U E D R , A S I H C RSS

미로찾기/정수민

Describe 미로찾기/정수민 here.



~cpp 
#include <stdio.h>

const int miro[13][17]={
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
	{1,0,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1},
	{1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1},
	{1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1},
	{1,1,1,0,1,1,1,1,0,1,1,0,1,1,0,0,1},
	{1,1,1,0,1,0,0,1,0,1,1,1,1,1,1,1,1},
	{1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1},
	{1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1},
	{1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1},
	{1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1},
	{1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1},
	{1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,0,1},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

int main () {

    int x, y, n=0, j=0;
    x=1;
    y=1;

    while (1){
          if (n==7){
                   if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else{}
          }
          else if (n==8){
                   if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else{}
          }
          else if (n==9){
                   if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else{}
          }
          else if (n==6){
                   if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else{}
          }
          else if (n==3){
                   if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else{}
          }
          else if (n==2){
                   if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else{}
          }
          else if (n==1){
                   if (miro[x][y-1]==0){ x=x;y=y-1;n=4;}
                   else if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else{}
          }
          else if (n==4){
                   if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=3;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=2;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=1;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=4;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=7;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=8;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=9;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=6;}
                   else{}
          }
          else {
                   if (miro[x-1][y-1]==0){ x=x-1;y=y-1;n=7;}
                   else if (miro[x-1][y]==0){ x=x-1;y=y;n=8;}
                   else if (miro[x-1][y+1]==0){ x=x-1;y=y+1;n=9;}
                   else if (miro[x][y+1]==0){ x=x;y=y+1;n=6;}
                   else if (miro[x+1][y+1]==0){ x=x+1;y=y+1;n=3;}
                   else if (miro[x+1][y]==0){ x=x+1;y=y;n=2;}
                   else if (miro[x+1][y-1]==0){ x=x+1;y=y-1;n=1;}
                   else if (miro[x][y-1]==0){ x=x;y=y-1;n=4;}
                   else{}
                   printf("처음");
          }
		  if ((x==11)&&(y==15)){
               printf("완료"); 
               break;
          }
          printf("(%d, %d) \n",x , y);
		  j++;
    }
	printf("%d 번째에 미로 탈출\n",j);
    system("PAUSE");
    return 0;
}

   

차아암 길다 -_-;;;;



리팩토링!! -_-!!!!


~cpp 
#include <stdio.h> 
 
const int miro[13][17]={ 
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
        {1,0,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1}, 
        {1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1}, 
        {1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1}, 
        {1,1,1,0,1,1,1,1,0,1,1,0,1,1,0,0,1}, 
        {1,1,1,0,1,0,0,1,0,1,1,1,1,1,1,1,1}, 
        {1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1}, 
        {1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1}, 
        {1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1}, 
        {1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1}, 
        {1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1}, 
        {1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,0,1}, 
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} 
}; 

void find1();
void find2();
void find3();
void find4();
void find6();
void find7();
void find8();
void find9();
void finish();

int x=1, y=1, j=0, finish_X=11, finish_Y=15; 

void main () { 
     find7();
} 
/**
 * 숫자의 의미는 숫자키패드에있다. 키보드의 숫자 키페드의 숫자가 바로 방향! 그래서 5는 생랴돼어있다.
 */
void find7(){
	if (miro[x-1][y-1]==0){ 
		x=x-1;
		y=y-1;
		finish();
		find2();  /*왼쪽벽을 따라간다.*/
	} 
	find8(); 
}

void find8(){
	if (miro[x-1][y]==0){ 
		x=x-1;
		y=y;
		finish();
		find1();
	} 
	else find9();
}

void find9(){
	if (miro[x-1][y+1]==0){ 
		x=x-1;
		y=y+1;
		finish();
		find4();
	} 
	else find6();
}

void find6(){
	if (miro[x][y+1]==0){ 
		x=x;
		y=y+1;
		finish();
		find7();
	} 
	else find3();
}

void find3(){
	if (miro[x+1][y+1]==0){ 
		x=x+1;
		y=y+1;
		finish();
		find8();
	} 
	else find2();
}

void find2(){
	if (miro[x+1][y]==0){ 
		x=x+1;
		y=y;
		finish();
		find9();
	} 
	else find1();
}

void find1(){
	if (miro[x+1][y-1]==0){ 
		x=x+1;
		y=y-1;
		finish();
		find6();
	} 
	else find4();
}

void find4(){
	if (miro[x][y-1]==0){ 
		x=x;
		y=y-1;
		finish();
		find3();
	} 
	else find7();
}

void finish(){
	if ((x==finish_X)&&(y==finish_Y)){ 
		printf("탈출!!! %d 번째에 미로 탈출\n",j);  
		system("PAUSE"); 
		exit(1);
	}
    printf("(%d, %d) \n",x , y); 
    j++; 
}
   

그래도 ... 길다 -_-;;;

근데왜 승한이형 아뒤가 로그아웃되지 않는걸까 -_-;;





Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:31
Processing time 0.0189 sec