No older revisions available
No older revisions available
C++ version ¶
- 개발자 : 나휘동
- 스택 사용
- 문제점 : 배열이 커지면 길을 못 찾는는 것으로 보아 어딘가 잘못된 듯 하다.
~cpp #ifndef __STACK__H__ #define __STACK__H__ typedef struct{ short hrz; short vtc; }Offset; typedef struct{ short row; short col; short direction; }Element; const int M = 7; const int P = 7; const int MAX = M*P; Element path[MAX]; int top = -1; bool isFull() { return (top < MAX - 1 ? false : true); } bool isEmpty() { return (top < 0 ? true : false); } Element push(int * top, Element aItem) { if ( *top < MAX - 1 ) path[++*top]= aItem; return path[*top]; } Element pop(int * top) { if ( *top < 0 ) return path[*top]; return path[--*top]; } #endif
main.cpp
~cpp #include <iostream.h> #include "stack.h" Offset move[8]= { {1,0}, {1,1}, {0,1}, {-1,1}, {-1,0}, {-1,-1}, {0,-1}, {1,-1}}; short maze[M+2][P+2] = { { 1, 1,1,1,1,1,1,1, 1 }, { 1, 0,1,0,0,1,1,1, 1 }, { 1, 0,1,0,1,0,1,1, 1 }, { 1, 0,1,0,1,1,0,1, 1 }, { 1, 0,1,0,1,1,1,0, 1 }, { 1, 0,1,0,1,1,1,0, 1 }, { 1, 0,1,0,1,1,1,0, 1 }, { 1, 1,0,1,0,0,1,0, 1 }, { 1, 1,1,1,1,1,1,1, 1 } }; void main() { Element item; item.col = item.row = 1; item.direction = 0; push(&top, item); int row, col; while ( (row != M || col != P) && top >= 0){ row = path[top].row + move[path[top].direction].vtc; col = path[top].col + move[path[top].direction].hrz; item.col = col; item.row = row; path[top].direction++; push(&top, item); if ( maze[row][col] != 0 ) pop(&top); if ( path[top-1].direction > 7){ pop(&top); maze[path[top].row][path[top].col] = -1; pop(&top); } } for ( int i = 0 ; i <= top ; i++ ) cout << "(" << path[i].row << ", " << path[i].col << ")" << endl; }