利用栈和回溯算法求解迷宫:
这是老师带着做的,自己写不知道什么时候能调出来~~
这就是和老师的差距,这样的程序他就10几分钟而已~
- /*深度优先�索求解迷宫*/
- #include<stdio.h>
- #define MAX_ROW 5
- #define MAX_COL 5
- int maze[MAX_ROW][MAX_COL] = {
- { 0, 1, 0, 0, 0 },
- { 0, 1, 0, 1, 0 },
- { 0, 0, 0, 0, 0 },
- { 0, 1, 1, 1, 0 },
- { 0, 0, 0, 1, 0 },
- };
- struct point{
- int row;
- int col;
- }stack[512];
- struct point front[MAX_ROW][MAX_COL]={
- {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
- {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
- {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
- {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
- {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}},
- };
- int top = 0;
- struct point start = {0, 0};
- struct point target = {4, 4};
- struct point full;
- void push(struct point p)
- {
- stack[top++] = p;
- }
- struct point pop(void)
- {
- return stack[--top];
- }
- int is_empty(void)
- {
- return top == 0;
- }
- struct point get_top(void)
- {
- return stack[top-1];
- }
- void print_stack(void)
- {
- int i;
- printf("stack info: /n");
- for(i = top -1; i >= 0; i--)
- {
- printf("(%d,%d) %d/n",stack[i].row, stack[i].col,
- stack[i].row * MAX_COL + stack[i].col);
- }
- }
- void print_maze(void)
- {
- int i,j;
- for(i = 0; i < MAX_ROW; i++)
- {
- for(j = 0; j < MAX_COL; j++)
- printf("%d ",maze[i][j]);
- putchar('/n');
- }
- printf("*****************/n");
- }
- void print_pre()
- {
- int i, j;
- int row, col;
- for(i = 0; i < MAX_ROW; i++)
- {
- for(j = 0; j < MAX_COL; j++)
- {
- row = front[i][j].row;
- col = front[i][j].col;
- printf("%d(%d)/t", row*MAX_COL + col, i*MAX_COL + j);
- }
- printf("/n");
- }
- }
- void print_front(void)
- {
- struct point tmp = target;
- static int ways = 0;
- printf("%d solution is:/n", ++ways);
- while(!(tmp.row == start.row && tmp.col == start.col))
- {
- printf("(%d,%d)/n", tmp.row, tmp.col);
- tmp = front[tmp.row][tmp.col];
- }
- printf("(%d,%d)/n", start.row, start.col);
- }
- void visit(int newrow, int newcol, struct point p)
- {
- struct point tmp;
- maze[newrow][newcol] = 2;
- /*set new expend node 's father node*/
- front[newrow][newcol] = p;
- tmp.row = newrow;
- tmp.col = newcol;
- push(tmp);
- }
- void back_track(struct point p)
- {
- struct point back = get_top();
- back = front[back.row][back.col];
- while(!(back.row == p.row && back.col == p.col))
- {
- maze[p.row][p.col] = 0;
- p = front[p.row][p.col];
- print_stack();
- print_maze();
- getchar();
- }
- }
- /*void back_track_full(struct point p)
- {
- full = front[full.row][full.col];
- back_track(p,full);
- }*/
- int main(viod)
- {
- struct point p, node;
- int flag = 1;
- printf( "hello,maze! /n" );
- print_maze();
- /*init start situation*/
- p = start;
- maze[p.row][p.col] = 2;
- push(p);
- // print_stack();
- while( !is_empty() )
- {
- /*get the saved point from stack*/
- p = pop();
- /*judge id p is target point*/
- if((p.row == target.row) && (p.col == target.col))
- {
- printf("target is found!/n");
- print_front();
- back_track(p);
- continue;
- }
- flag = 0;
- /*expend p to UP. LEFT,DOWN, RIGHT*/
- if( ((p.row - 1) >= 0) && (maze[p.row - 1][p.col] == 0) )/*UP*/
- {
- visit( p.row - 1, p.col, p );
- flag++ ;
- }
- if( ((p.col - 1) >= 0) && (maze[p.row][p.col - 1] == 0) )/*LEFT*/
- {
- visit( p.row, p.col - 1, p );
- flag++;
- }
- if( ((p.row + 1) < MAX_ROW) && (maze[p.row + 1][p.col] == 0) )/*DOWN*/
- {
- visit( p.row + 1, p.col, p );
- flag++;
- }
- if( ((p.col + 1) < MAX_COL) && (maze[p.row][p.col + 1] == 0) )/*RIGHT*/
- {
- visit( p.row, p.col + 1, p );
- flag++;
- }
- if(flag == 0)
- back_track(p);
- print_stack();
- print_pre();
- // getchar();
- }
- return 0;
- }