À̵¿ ÇÒ ¼ö ÀÖ´Â °÷À» ½ºÅÿ¡ ÀúÀåÇسõ°í »ç¿ëÇÏ¸é ±íÀÌ ¿ì¼± Ž»ö DFS(Depth First Search)°¡ µÈ´Ù.
±³Àç ÇÁ·Î±×·¥Àº ½ºÅÃÀ» »ç¿ëÇÑ DFS ¹æ¹ýÀÌ´Ù.
À̸¦ Å¥¸¦ ÀÌ¿ëÇÑ ³Êºñ ¿ì¼± Ž»ö BFS ·Î ±¸ÇöÇϽÿÀ.
(BFS·Î ±¸ÇöÇϸé ÃÖ´Ü°æ·Î¸¦ ãÀ» ¼ö ÀÖÀ½)
- push_loc() ÇÔ¼ö¸¦ enqueue_loc() ÇÔ¼ö·Î
- pop() ÇÔ¼ö¸¦ dequeue() ÇÔ¼ö·Î ±¸Çö
DFS | BFS |
[Å¥ ±¸Çö ¹æ¹ý Âü°í]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAZE_SIZE 6 #define QUEUE_SIZE 100 typedef struct { short r; short c; } element; typedef struct { element data[QUEUE_SIZE]; int front, rear; } QueueType; // Å¥ ÃʱâÈ ÇÔ¼ö void init_queue(QueueType *q) { q->front = q->rear = 0; } // Å¥ °ø¹é »óÅ °ËÃâ ÇÔ¼ö int is_empty(QueueType *q) { return (q->front == q->rear); } // Å¥ Æ÷È »óÅ °ËÃâ ÇÔ¼ö int is_full(QueueType *q) { return ((q->rear + 1) % QUEUE_SIZE == q->front); } // Å¥¿¡ »ðÀÔÇÏ´Â ÇÔ¼ö void enqueue(QueueType *q, element item) { if (is_full(q)) { fprintf(stderr, "Å¥ Æ÷È ¿¡·¯\n"); return; } q->rear = (q->rear + 1) % QUEUE_SIZE; q->data[q->rear] = item; } // Å¥¿¡¼ »èÁ¦ÇÏ´Â ÇÔ¼ö element dequeue(QueueType *q) { if (is_empty(q)) { fprintf(stderr, "Å¥ °ø¹é ¿¡·¯\n"); exit(1); } q->front = (q->front + 1) % QUEUE_SIZE; return q->data[q->front]; } | cs |