65,210
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include<Queue>
using namespace std;
int grid[8][8];
int m=8,n=8;
int PathLen;
typedef struct
{
int row;
int col;
}Position;
Position *path;
template <class T> class LinkedQueue;
template <class T>
class Node
{
friend class LinkedQueue<T>;
private:
T data;
Node<T> *link;
};
template<class T>
class LinkedQueue {
public:
LinkedQueue() {front = rear = 0;}
~LinkedQueue();
bool IsEmpty() const
{return ((front) ? false : true);}
bool IsFull() const;
T First() const;
T Last() const;
LinkedQueue<T>& Add(const T& x);
LinkedQueue<T>& Delete(T& x);
private:
Node<T> *front;
Node<T> *rear;
};
template<class T>
LinkedQueue<T>::~LinkedQueue()
{
Node<T> *next;
while (front) {
next = front->link;
delete front;
front = next;
}
}
template<class T>
bool LinkedQueue<T>::IsFull() const
{
Node<T> *p;
try {p = new Node<T>;
delete p;
return false;}
catch (NoMem) {return true;}
}
template<class T>
T LinkedQueue<T>::First() const
{
if (IsEmpty()) throw OutOfBounds();
return front->data;
}
template<class T>
T LinkedQueue<T>::Last() const
{
if (IsEmpty()) throw OutOfBounds();
return rear->data;
}
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x)
{//添加
Node<T> *p = new Node<T>;
p->data = x;
p->link = 0;
if (front) rear->link = p;
else front = p;
rear = p;
return *this;
}
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{//删除
if (IsEmpty()) throw OutOfBounds();
x = front->data;
Node<T> *p = front;
front = front->link;
delete p;
return *this;
}
char* OutOfBounds()
{
return "ERROR!";
}
int FindPath (Position start, Position finish, int &PathLen, Position *&path)
{ //计算从起始位置start到目标位置finish的最短布线路径,找到返回1,否则,返回0
int i;
if ((start.row == finish.row) && (start.col == finish.col)) {
PathLen = 0; return 0; } //start = finish
//设置方格阵列”围墙”
for (i = 0; i <= m+1; i++)
grid[0][i] = grid[n+1][i] = 1; //顶部和底部
for (i = 0; i <= n+1; i++)
grid[i][0] = grid[i][m+1] = 1; //左翼和右翼
//初始化相对位移
int NumOfNbrs = 4; //相邻方格数
Position offset[4], here, nbr;
offset[0].row = 0; offset[0].col = 1; //右
offset[1].row = 1; offset[1].col = 0; //下
offset[2].row = 0; offset[2].col = -1; //左
offset[3].row = -1; offset[3].col = 0; //上
here.row=start.row;
here.col=start.col;
LinkedQueue <Position> Q; //标记可达方格位置
do {
for (i = 0; i< NumOfNbrs; i++) { //标记可达相邻方格
nbr.row = here.row + offset[i].row ;
nbr.col = here.col + offset[i].col;
if (grid[nbr.row][nbr.col]==0) { //该方格未标记
grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;
if ((nbr.row == finish.row) && (nbr.col == finish.col)) break;//完成布线
Q.Add(nbr);
}
}
if ((nbr.row == finish.row) && (nbr.col == finish.col)) break;//完成布线
if (Q.IsEmpty()) //活队列是否为空
return 0; //无解
Q.Delete(here); //取下一个扩展结点
}while (1);
//构造最短布线路径
PathLen = grid[finish.row][finish.col]-2;
path = new Position[PathLen];
here = finish;
for (int j=PathLen-1; j>=0; j--)
{ //找前驱位置
path[j] = here;
for (i = 0; i< NumOfNbrs; i++)
{
nbr.row = here.row + offset[i].row ;
nbr.col = here.col + offset[i].col;
if (grid[nbr.row][nbr.col]==j+2) break;
}
here = nbr; //向前移动
}
return 1;
}
void main ()
{
Position start, finish;
start.row = 3; start.col = 2;
finish.row = 4; finish.col = 6;
FindPath (start, finish, PathLen, path);
}
#include <iostream>
#include<Queue>
using namespace std;
int grid[8][8];
int m=8,n=8;
int PathLen;
typedef struct
{
int row;
int col;
}Position;
Position *path;
template <class T> class LinkedQueue;
template <class T>
class Node
{
friend class LinkedQueue<T>;
private:
T data;
Node<T> *link;
};
template<class T>
class LinkedQueue {
public:
LinkedQueue() {front = rear = 0;}
~LinkedQueue();
bool IsEmpty() const
{return ((front) ? false : true);}
bool IsFull() const;
T First() const;
T Last() const;
LinkedQueue<T>& Add(const T& x);
LinkedQueue<T>& Delete(T& x);
private:
Node<T> *front;
Node<T> *rear;
};
template<class T>
LinkedQueue<T>::~LinkedQueue()
{
Node<T> *next;
while (front) {
next = front->link;
delete front;
front = next;
}
}
template<class T>
bool LinkedQueue<T>::IsFull() const
{
Node<T> *p;
try {p = new Node<T>;
delete p;
return false;}
catch (NoMem) {return true;}
}
template<class T>
T LinkedQueue<T>::First() const
{
if (IsEmpty()) throw OutOfBounds();
return front->data;
}
template<class T>
T LinkedQueue<T>::Last() const
{
if (IsEmpty()) throw OutOfBounds();
return rear->data;
}
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x)
{//添加
Node<T> *p = new Node<T>;
p->data = x;
p->link = 0;
if (front) rear->link = p;
else front = p;
rear = p;
return *this;
}
template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{//删除
if (IsEmpty()) throw OutOfBounds();
x = front->data;
Node<T> *p = front;
front = front->link;
delete p;
return *this;
}
char* OutOfBounds()
{
return "ERROR!";
}
int FindPath (Position start, Position finish, int &PathLen, Position *&path)
{ //计算从起始位置start到目标位置finish的最短布线路径,找到返回1,否则,返回0
int i;
if ((start.row == finish.row) && (start.col == finish.col)) {
PathLen = 0; return 0; } //start = finish
//设置方格阵列”围墙”
for (i = 0; i <= m+1; i++)
grid[0][i] = grid[n+1][i] = 1; //顶部和底部
for (i = 0; i <= n+1; i++)
grid[i][0] = grid[i][m+1] = 1; //左翼和右翼
//初始化相对位移
int NumOfNbrs = 4; //相邻方格数
Position offset[4], here, nbr;
offset[0].row = 0; offset[0].col = 1; //右
offset[0].row = 1; offset[0].col = 0; //下
offset[0].row = 0; offset[0].col = -1; //左
offset[0].row = -1; offset[0].row = 0; //上
here.row=start.row;
here.col=start.col;
LinkedQueue <Position> Q; //标记可达方格位置
do {
for (i = 0; i< NumOfNbrs; i++) { //标记可达相邻方格
nbr.row = here.row + offset[i].row ;
nbr.col = here.col + offset[i].col;
if (grid[nbr.row][nbr.col]==0) { //该方格未标记
grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;
if ((nbr.row == finish.row) && (nbr.col == finish.col)) break;//完成布线
Q.Add(nbr);
}
}
if ((nbr.row == finish.row) && (nbr.col == finish.col)) break;//完成布线
if (Q.IsEmpty()) //活队列是否为空
return 0; //无解
Q.Delete(here); //取下一个扩展结点
}while (1);
//构造最短布线路径
PathLen = grid[finish.row][finish.col]-2;
path = new Position[PathLen];
here = finish;
for (int j=PathLen-1; j>=0; j--)
{ //找前驱位置
path[j] = here;
for (i = 0; i< NumOfNbrs; i++)
{
nbr.row = here.row + offset[i].row ;
nbr.col = here.col + offset[i].col;
if (grid[nbr.row][nbr.col]==j+2) break;
}
here = nbr; //向前移动
}
return 1;
}
void main ()
{
Position start, finish;
start.row = 3; start.col = 2;
finish.row = 4; finish.col = 6;
FindPath (start, finish, PathLen, path);
}