急急急!!!!请教程序错误,关于分支限界最短路径

键盘手 2010-12-15 10:00:35
好不容易找到一个,可惜有错误,各位高手帮帮忙啊!!
#include <iostream>
#include<Queue>
using namespace std;
int grid[8][8];
int m=8,n=8;
int PathLen, *path;
typedef struct
{
int row;
int col;
}Position;
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);
}


运行结果:


Compiling...
xianjie_1.cpp
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(30) : error C2065: 'LinkedQueue' : undeclared identifier
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(30) : error C2275: 'Position' : illegal use of this type as an expression
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(10) : see declaration of 'Position'
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(30) : error C2065: 'Q' : undeclared identifier
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(38) : error C2228: left of '.Add' must have class/struct/union type
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(42) : error C2228: left of '.IsEmpty' must have class/struct/union type
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(44) : error C2059: syntax error : 'delete'
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(47) : error C2228: left of '.PathLen' must have class/struct/union type
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(47) : error C2228: left of '.row' must have class/struct/union type
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(58) : error C2109: subscript requires array or pointer type
e:\liumin\suanfa_exam\xianjie_1\xianjie_1.cpp(58) : error C2109: subscript requires array or pointer type
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2010-12-16
  • 打赏
  • 举报
回复
有那么复杂么,不就是一个priority_queue+状态结点数据结构,做题思路要理清了再做.
龙哥依旧 2010-12-16
  • 打赏
  • 举报
回复
不报错了!
#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);
}
龙哥依旧 2010-12-16
  • 打赏
  • 举报
回复
你这个没有LinkedQueue的定义,给你加上了,编译通过,但是这个程序还有问题,先给你贴上,有空再看看,你先自己看看!
#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);
}
  • 打赏
  • 举报
回复
1,LinkedQueue链队列没有声明?
2,Position 将类型当作了表达式,查看Position声明?
3,Q,未定义的标志符
4,.Add的左边必须是一个类/结构/联合体类型
5,'.IsEmpty' 的左边必须是一个类/结构/联合体类型
6,delete语法错误
7,.PathLen' 的左边必须是一个类/结构/联合体类型
8,.row' 的左边必须是一个类/结构/联合体类型
9,需要数组或者指针类型
10,需要数组或者指针类型

根据这些错误分析,可能是某些头文件没有包含正确


aaajj 2010-12-15
  • 打赏
  • 举报
回复
应该是#include <queue> 吧

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧