maze

#include <iostream>
#include "stack.h"
using namespace std;

#define UP  0x00000001
#define DOWN 0x00000010
#define LEFT 0x00000100
#define RIGHT 0x00001000

bool walk_maze(int (*arr)[10], int x, int y, struct stack *stack_corner);
int main()
{
  struct stack stack_corner;
  struct square pre = {0};
  int maze[10][10] = { {1,1,1,1,1,1,1,1,1,1},
       {1,8,0,0,0,0,0,0,0,1},
       {1,1,0,0,0,0,0,0,0,1},
       {1,1,0,0,0,0,0,0,0,1},
       {1,1,1,0,0,0,0,0,0,1},
       {1,1,1,0,0,0,0,0,0,1},
       {1,1,1,0,0,0,0,0,0,1},          //错在   5.1
       {1,1,1,0,0,0,0,0,0,1},
       {1,1,1,0,0,0,9,0,1,1},
       {1,1,1,0,1,1,1,1,1,1}
            };
 for(int i=0; i<10; i++)
 {
  for(int j = 0; j<10; j++)
  {
   cout<<maze[i][j]<<" ";
  }
  cout<<endl;
 }
 cout<<"*************************************"<<endl;
    if(walk_maze(maze, 1,1, &stack_corner))
 {
  cout<<"找到了"<<endl;
  while(!stack_corner.is_empty(&stack_corner))
  {
   pre = stack_corner.pop(&stack_corner);
   cout<<"x:"<<pre.x<<"   y:"<<pre.y<<endl;
   maze[pre.x][pre.y] = 5;

  }
   for(int i=0; i<10; i++)
 {
  for(int j = 0; j<10; j++)
  {
   cout<<maze[i][j]<<" ";
  }
  cout<<endl;
 }
 }
 else
 {
  cout<<"没找到"<<endl;
 }
 return 0;
}


//还在入口成了特殊位置
bool walk_maze(int (*arr)[10], int x, int y, struct stack *stack_corner)
{
  struct stack sstack;
  struct square current = {0};
  struct square pre = {0};
  init_lstack(&sstack);
  init_lstack(stack_corner);
  int corner;
  int last_corner = corner = -1;
  int n= 0;
  while(arr[x][y] != 9)       //对当前方格进行判断.只能当能走出一步时,才入栈.这是本意.判断当前的方格会不会是下一步.如果不是.
  {            //不能越界,还不能穿墙.还不能从入口出去.
    if((current.direction&UP)==0 && (x-1>-1) && arr[x-1][y] != 1)
    {
    current.direction |= UP;
    arr[x][y] = 1;       //置为不可走状态.
    current.x = x;
    current.y = y;
    corner = UP;
    sstack.push(&sstack, &current);
    current.direction = 0;
    x -=1;
    } else if((current.direction&DOWN)==0 && (x+1<10) && arr[x+1][y] != 1)
    {
    current.direction |= DOWN;
    arr[x][y] = 1;       //置为不可走状态.
    current.x = x;
    current.y = y;
    corner = DOWN;
    sstack.push(&sstack, &current);
    current.direction = 0;
    x+=1;
    }
    else if((current.direction&LEFT)==0 && (y-1>-1) && arr[x][y-1] != 1)
    {
    current.direction |= LEFT;
    arr[x][y] = 1;       //置为不可走状态.
    current.x = x;
    current.y = y;
    corner = LEFT;
    sstack.push(&sstack, &current);
    current.direction = 0;
    y -=1;
    }
    else if((current.direction&RIGHT)==0 && (y+1<10) && arr[x][y+1] != 1)
    {
    current.direction |= RIGHT;
    arr[x][y] = 1;       //置为不可走状态.
    current.x = x;
    current.y = y;
    corner = RIGHT;
    sstack.push(&sstack, &current);
    current.direction = 0;
    y+=1;
    }else if(!sstack.is_empty(&sstack))  //如果栈为空的话也要退出循环,不能进行死循环.
    {         
    current = sstack.pop(&sstack);
    if(!stack_corner->is_empty(stack_corner))
             {
     pre = stack_corner->peek(stack_corner);
     if(current.x == pre.x && current.y == pre.y)
     {
      stack_corner->pop(stack_corner);    //要使用上一次移动的方向.
      pre = stack_corner->peek(stack_corner);
      last_corner  = pre.direction;   //当前方格拐弯的方向
     }
    }
    x = current.x;
    y = current.y;
    arr[x][y] = 0;                         //标记为没有走过,同一条路径,线路不能发生交叉
    continue;
    }
    else
    {
     break;
    }
    //这块代码要独立出来.判断拐点,并保存拐点
    if(last_corner!=corner)
    {
     pre = sstack.peek(&sstack);
     pre.direction = corner;   //记录拐点位置,还有向哪个方向拐
     stack_corner->push(stack_corner, &pre);
     if(stack_corner->get_size(stack_corner)>3 && !sstack.is_empty(&sstack))
     {
      current = sstack.pop(&sstack);
      stack_corner->pop(stack_corner);  //到达最大拐点时.
      x = current.x;
      y = current.y;
      arr[x][y] = 0;     
      continue;        //到达最大转弯数.
     }
     last_corner = corner;
    }
  }
  if(sstack.is_empty(&sstack))
  {
   return false;
  }
  return true;

}

你可能感兴趣的:(struct,UP,include)