当前位置: 首页 >> 程序设计 >> 数据结构和算法 >> 广度优先遍历解决布线迷宫问题源代码
 

广度优先遍历解决布线迷宫问题源代码

作者:      来源:ypxing.cublog.cn     发表时间:2007-11-08     浏览次数:      字号:    

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

//0--unopccupied
//-5--barricade
//-4--right to left
//-3--up to down
//-2--left to right
//-1--down to up
#define BA -5
#define RL -4
#define LR -2
#define UD -3
#define DU -1

char maze[16][16]={
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, BA, 0, 0, 0, 0},
  {0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, BA, 0, 0, 0},
  {0, 0, BA, BA, 0, 0, 0, 0, 0, 0, 0, BA, 0, 0, 0, 0},
  {0, 0, 0, BA, 0, 0, 0, 0, BA, BA, BA, 0, 0, 0, 0, 0},
  {0, 0, 0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, BA, 0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, BA, BA, 0, 0, 0, 0, 0, 0, BA, BA, BA, 0, 0},
  {0, 0, 0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0, BA, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, BA, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, BA, 0, 0, 0},
  {0, BA, BA, 0, 0, 0, BA, BA, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, BA, BA, 0, 0, 0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {BA, BA, BA, 0, 0, BA, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, BA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

class Position
{
public:
  Position()
  {
    x = 0;
    y = 0;
  }
  
  Position(int _x, int _y)
  {
    x = _x;
    y = _y;
  }
  
  Position& operator=(const Position& ps)
  {
    x = ps.x;
    y = ps.y;
    return *this;
  }
  bool operator==(const Position& ps) const
  {
    return ((x==ps.x)&&(y==ps.y));
  }

  bool operator!=(const Position& ps) const
  {
    return !operator==(ps);
  }
  
  int x;
  int y;
};

bool FindPath(Position start, Position end)
{
  //in fact, the start and end should be verified

  if (start==end)
  {
    cout <<start.x << ", " << start.y << endl;
    return true;
  }
  Position cur, temp;
  queue<Position> mq;
  cur.x = start.x;
  cur.y = start.y;
  
  while (cur!=end)
  {
    if ((cur.y>0)&&(maze[cur.x][cur.y-1]==0))
    {
      maze[cur.x][cur.y-1]=-1;
      temp.x = cur.x; temp.y=cur.y-1;
      mq.push(temp);
    }
    if ((cur.x<16-1)&&(maze[cur.x+1][cur.y]==0))
    {
      maze[cur.x+1][cur.y]=-2;
      temp.x = cur.x+1; temp.y=cur.y;
      mq.push(temp);
    }
    if ((cur.y<16-1)&&(maze[cur.x][cur.y+1]==0))
    {
      maze[cur.x][cur.y+1]=-3;
      temp.x = cur.x; temp.y=cur.y+1;
      mq.push(temp);
    }
    if ((cur.x>0)&&(maze[cur.x-1][cur.y]==0))
    {
      maze[cur.x-1][cur.y]=-4;
      temp.x = cur.x-1; temp.y=cur.y;
      mq.push(temp);
    }
    if (mq.empty())
      return false;
    cur = mq.front();
    mq.pop();
  }
  
  if (cur!=end)
    return false;
  
  while (cur!=start)
  {
    cout << cur.x << ", " << cur.y << endl;
    if (maze[cur.x][cur.y]==-1)
    {
      cur.y = cur.y + 1;
    }
    else if (maze[cur.x][cur.y]==-2)
    {
      cur.x = cur.x - 1;
    }
    else if (maze[cur.x][cur.y]==-3)
    {
      cur.y = cur.y -1;
    }
    else
    {
      cur.x = cur.x + 1;
    }
  }
 
}

int main(int argc, char* argv[])
{
  Position start(0, 0), end(5, 4);
  FindPath(start, end);
}

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •