#include #include #include #include #define NONE 0x000 #define LEFT 0x001 #define RIGHT 0x002 #define UP 0x004 #define DOWN 0x008 #define MAX_VALUE 9 #define MAX_DEPTH 5 // ÃÖ´ë Çã¿ë ±íÀÌ #define FIND_NODE 10 #define MAX_STACK 100 #define MAX_NODE 4 // ÃÖ´ë ÀÚ½Ä ³ëµå ¼ö #define TRUE 1 #define FALSE 0 typedef struct _node { int value[9]; // ÆÛÁñ µ¥ÀÌŸ int depth; // ³ëµåÀÇ ±íÀÌ int direction; // Ä¿¼­ÀÇ ÇöÀç À§Ä¡ int fromDirection; // Ä¿¼­°¡ ¾îµð·ÎºÎÅÍ ¿Ô´Â°¡ struct _node *nextNode[4]; // ÀÚ½Ä ³ëµå ÃÖ´ë 4°³ } PUZZLE; PUZZLE stack[MAX_STACK]; int current_stack; int callCount; int gCursorPosition; int DFS(PUZZLE *pNode[], int *gNode, int pCount, int pDepth); int swapNodeValue(PUZZLE *childNode[], int i, int cursorPosition, int *direction); void printNodeValue(PUZZLE *childNode[], int count); int getChildCount(int direction); int comparePuzzle(PUZZLE *pNode, int *gNode); int findDirection(PUZZLE *node); void showDirection(PUZZLE *node, int direction); void printCureentNode(); void push(PUZZLE *t); PUZZLE pop(void); int isEmptyStack(void); void initStack(void); int main(void) { int iDimension[9]={2,8,3,1,6,4,7,0,5}; int gDimension[9]={1,2,3,8,0,4,7,6,5}; PUZZLE *iPuzzle[MAX_NODE]; int i; int j; // ½ºÅà ÃʱâÈ­ initStack(); for (i=0; idepth=0; iPuzzle[i]->fromDirection=NONE; iPuzzle[i]->nextNode[i]=NULL; /*for (j=0; jvalue[j]=iDimension[j];*/ } memcpy(iPuzzle[0]->value, iDimension, 9*sizeof(int)); //memcpy(gPuzzle.value, gDimension, 9*sizeof(int)); printf("\nSOURCE : "); for (j=0; jvalue[j]); } printf("\nTARGET : "); for (j=0; jfromDirection; printf("³ëµå[%d]ÀÇ EXCLUSIVE ¿¬»ê ÈÄ DIRECTION : %d\n", pNode[pCount], exDirection); // ÇöÀç Ä¿¼­ À§Ä¡¿¡¼­ ¾î´À ¹æÇâÀ¸·Î °¥¼ö ÀÖ´ÂÁö üũÇÔ showDirection(pNode[pCount], exDirection); printf("³ëµå[%d]ÀÇ Ä¿¼­°¡ °¥ ¼ö ¾ø´Â ¹æÇâ : %d\n", pNode[pCount], pNode[pCount]->fromDirection); // ±íÀ̸¦ Áõ°¡½ÃÅ°°í pDepth=pDepth++; // »ý¼ºÇÒ ¼ö ÀÖ´Â ÀÚ½Ä ³ëµåÀÇ ¼ö¸¦ ±¸ÇÔ makeChildNumber=getChildCount(exDirection); printf("³ëµå[%d]ÀÇ »ý¼º Àڽļö : %d\n", pNode[pCount], makeChildNumber); for (i=0; idepth=pDepth; // ºÎ¸ð ³ëµå¸¦ ÀÚ½Ä ³ëµå¿Í ¿¬°á pNode[pCount]->nextNode[i]=childNode[i]; // ÀÚ½Ä ³ëµå°¡ °¡¸®Å°°í ÀÖ´Â °ÍÀº NULL·Î ÁöÁ¤ childNode[i]->nextNode[0]=NULL; childNode[i]->nextNode[1]=NULL; childNode[i]->nextNode[2]=NULL; childNode[i]->nextNode[3]=NULL; // »ý¼ºµÈ ÀÚ½Ä ³ëµå¿¡ ºÎ¸ð ³ëµåÀÇ °ªÀ» ÀúÀå for (j=0; jvalue[j]=pNode[pCount]->value[j]; //printf("[%d] ",childNode[i]->value[j]); } printf("\nºÎ¸ð[%d]->[%d]¹ø° ÀÚ½ÄÀÇ ÁÖ¼Ò:[%d]\n", pNode[pCount], i, childNode[i]); printf("ºÎ¸ð[%d]->[%d]¹ø° ÀÚ½ÄÀÇ ±íÀÌ:%d\n", pNode[pCount], i, childNode[i]->depth); // ³ëµå¾ÈÀÇ °ªÀ» À̵¿ÇÏ¿© º¯°æ swapNodeValue(childNode, i, cursorPosition, &exDirection); // ³Ñ°ÜÁ® ¿Â ÀÚ½Ä ³ëµå¿Í ¸ñÇ¥ ³ëµå¸¦ ºñ±³ÇÔ if (comparePuzzle(childNode[i], gNode) == FIND_NODE) { printf("\n:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n"); printf("::::::::::::::::::::::::::::: ¸ñÇ¥ ³ëµå ãÀ½ ::::::::::::::::::::::::::::\n"); printf(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\n"); exit(0); } } for (i=0; ivalue[i]; if ((i%3) == 0) printf("\n"); printf("[%d] ", childNode[count]->value[i]); } // Ä¿¼­ À§Ä¡¿Í ±× ÀÌ¿ô¿¡ À§Ä¡ÇÑ °ªÀ» ±³Ã¼ temp1 = tempNode.value[cursorPosition]; if ((*direction&LEFT) == LEFT) { temp2 = tempNode.value[cursorPosition-1]; childNode[count]->value[cursorPosition]=temp2; childNode[count]->value[cursorPosition-1]=temp1; childNode[count]->direction=*direction&LEFT; childNode[count]->fromDirection=RIGHT; // ÀÌ¹Ì ½ÇÇàÇÑ ±¸¹®Àº Skip Çϱâ À§ÇÔÀÓ *direction=*direction-LEFT; //printf("\nDirection:::LEFT:::\t count:%d\t cursorPosition:%d", count, cursorPosition); // ½º¿Ò ÈÄ ³ëµå°ª Ãâ·Â printNodeValue(childNode, count); return 0; } else if ((*direction&UP) == UP) { temp2 = tempNode.value[cursorPosition-3]; childNode[count]->value[cursorPosition]=temp2; childNode[count]->value[cursorPosition-3]=temp1; childNode[count]->direction=*direction&UP; childNode[count]->fromDirection=DOWN; *direction=*direction-UP; //printf("\nDirection:::UP:::\t count:%d\t cursorPosition:%d", count, cursorPosition); // ½º¿Ò ÈÄ ³ëµå°ª Ãâ·Â printNodeValue(childNode, count); return 0; } else if ((*direction&RIGHT) == RIGHT) { temp2 = tempNode.value[cursorPosition+1]; childNode[count]->value[cursorPosition]=temp2; childNode[count]->value[cursorPosition+1]=temp1; childNode[count]->direction=*direction&RIGHT; childNode[count]->fromDirection=LEFT; *direction=*direction-RIGHT; //printf("\nDirection:::RIGHT:::\t count:%d\t cursorPosition:%d", count, cursorPosition); // ½º¿Ò ÈÄ ³ëµå°ª Ãâ·Â printNodeValue(childNode, count); return 0; } else if ((*direction&DOWN) == DOWN) { temp2 = tempNode.value[cursorPosition+3]; childNode[count]->value[cursorPosition]=temp2; childNode[count]->value[cursorPosition+3]=temp1; childNode[count]->direction=*direction&DOWN; childNode[count]->fromDirection=UP; *direction=*direction-DOWN; //printf("\nDirection:::DOWN:::\t count:%d\t cursorPosition:%d", count, cursorPosition); // ½º¿Ò ÈÄ ³ëµå°ª Ãâ·Â printNodeValue(childNode, count); return 0; } return 0; } // »ý¼ºÇÒ ¼ö ÀÖ´Â ÀÚ½Ä ³ëµåÀÇ ¼ö¸¦ ±¸Çϱâ À§ÇÑ ÇÔ¼ö int getChildCount(int direction) { int makeChildCount; // »ý¼º ÇÒ ¼ö ÀÖ´Â ÀÚ½Ä ³ëµåÀÇ ¼ö¸¦ ±¸Çϱâ À§ÇØ Ã¼Å© if (direction == 1 || direction == 2 || direction == 4 || direction == 8) makeChildCount=1; else if (direction == 3 || direction == 5 || direction == 6 || direction == 9 || direction == 10 || direction == 12) makeChildCount=2; else if (direction == 7 || direction == 11 || direction == 13 || direction == 14) makeChildCount=3; else if (direction == 15) makeChildCount=4; return makeChildCount; } // ÇöÀç Ä¿¼­À§Ä¡¿¡¼­ °¥¼ö ÀÖ´Â ¹æÇâÀ» ±¸ÇÔ int findDirection(PUZZLE *node) { int direction; int i; for (i=0; ivalue[i] == 0) { gCursorPosition=direction=i; // 0 [BLANK]ÀÌ À§Ä¡ÇÑ °÷À» ã¾Æ Àü¿ª º¯¼ö¿¡ ÀúÀå printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡ [0 1 2 | 3 4 5 | 6 7 8] : %d\n", node, gCursorPosition); break; } } if ((i/3) == 0) { direction=DOWN; } else if ((i/3) == 1) { direction=UP | DOWN; } else if ((i/3) == 2) { direction=UP; } if ((i%3) == 0) { direction=direction | RIGHT; } else if ((i%3) == 1) { direction=direction | LEFT | RIGHT; } else if ((i%3) == 2) { direction=direction | LEFT; } return direction; } // Ãâ¹ß ³ëµå¿Í ¸ñÇ¥ ³ëµå¸¦ ºñ±³ÇÔ int comparePuzzle(PUZZLE *pNode, int *gNode) { int i; for (i=0; ivalue[i] != gNode[i]) return -1; } if (i == 9) return FIND_NODE; return 0; } // Ä¿¼­°¡ ¿òÁ÷ÀÏ ¼ö ÀÖ´Â ¹æÇâÀ» Ç¥½Ã void showDirection(PUZZLE *node, int direction) { switch(direction) { case 1 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT\n", node); break; case 2 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : RIGHT\n", node); break; case 3 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | RIGHT\n", node); break; case 4 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : UP\n", node); break; case 5 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | UP\n", node); break; case 6 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : RIGHT | UP\n", node); break; case 7 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | RIGHT | UP\n", node); break; case 8 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : DOWN\n", node); break; case 9 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | DOWN\n", node); break; case 10 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : RIGHT | DOWN\n", node); break; case 11 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | RIGHT | DOWN\n", node); break; case 12 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : UP | DOWN\n", node); break; case 13 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | UP | DOWN\n", node); break; case 14 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : RIGHT | UP | DOWN\n", node); break; case 15 : printf("³ëµå[%d]ÀÇ Ä¿¼­ À§Ä¡·ÎºÎÅÍ ¿òÁ÷Àϼö ÀÖ´Â ¹æÇâ : LEFT | RIGHT | UP | DOWN\n", node); break; } } void printNodeValue(PUZZLE *childNode[], int count) { int i; printf("\n\n½º¿Ò ÈÄ : "); // Àӽà ³ëµå¿¡ °ªÀ» º¹»ç for (i=0; ivalue[i]); } printf("\n"); } void printCurrentNode() { int i; printf("\n"); for(i=0;i<9;i++){ printf("%d\t", stack[current_stack-1].value[i]); } } void push(PUZZLE *t) { if (current_stack >= MAX_STACK -1) { printf("\nStack Overflow!\n"); exit(1); } else stack[current_stack] = *t; current_stack++; } PUZZLE pop(void) { PUZZLE ptr; if (current_stack < 0) { printf("\nStack Underflow\n"); exit(1); } else { ptr=stack[current_stack]; current_stack--; } return ptr; } void initStack(void) { current_stack=-1; } int isEmptyStack(void) { return (current_stack == -1); }