数据结构实验二(2)C++实现顺序队列

数据结构实验二(2)顺序队列


1.实验内容
掌握队列的定义;
掌握队列的顺序存储。
掌握队列的基本操作,如建立、入队和出队等。
运用队列搜索网格中两点之间的最短路径,运用二维数组表示网格,如a[i][j]表示位于第i+1行第j+1列的格子, a[i][j] =1则表示该格子可走, a[i][j] =0则表示该格子不可走,定义一个函数:通过队列完成给定两点之间的路径搜索,在main函数里进行点的位置输入(以行列坐标表示一个点),然后通过调用上述函数得到最短距离
2.顺序队列功能
const int MAXSIZE = 100;
template<class ElemType>
class orderQueue {
private:
	ElemType* base;
	int front;
	int rear;
public:
	bool initQueue();
	bool enQueue(ElemType e);
	bool deQueue(ElemType& e);
	bool isEmpty();
	int length();
	ElemType getFront();
};

template<class ElemType>
inline bool orderQueue<ElemType>::initQueue() {
	base = new ElemType[MAXSIZE];
	if (!base)
		return false;
	front = rear = 0;
	return true;
}
template<class ElemType>
inline bool orderQueue<ElemType>::enQueue(ElemType e) {
	if ((rear + 1) % MAXSIZE == front)
		return false;
	base[rear] = e;
	rear = (rear + 1) % MAXSIZE;
	return true;
}

template<class ElemType>
inline bool orderQueue<ElemType>::deQueue(ElemType& e) {
	if (front == rear)
		return false;
	e = base[front];
	front = (front + 1) % MAXSIZE;
	return true;
}

template<class ElemType>
inline bool orderQueue<ElemType>::isEmpty() {
	if (front == rear)
		return true;
	return false;
}

template<class ElemType>
inline int orderQueue<ElemType>::length() {
	return (rear - front + MAXSIZE) % MAXSIZE;
}

template<class ElemType>
inline ElemType orderQueue<ElemType>::getFront() {
	if (front != rear)
		return base[front];
}
3.利用BFS算法实现求两点之间最短距离
const int inaccessible = 99999;//表示不可达
typedef pair<int, int>Pair;//用Pair类型存储坐标
orderQueue<Pair>Q;
int BFS(int m,int n,int startX,int startY, int endX, int endY,char maze[100][100]) {
	int routeLength[100][100];//记录距离
	int x[4] = { 1,0,-1,0 };
	int y[4] = { 0,1,0,-1 };//数组x,y定义了移动方向,分别为右(1,0)、上(0,1)、左(-1,0)、下(0,-1)
	Q.initQueue();

	//初始化路径长度,全部为不可达
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++)
			routeLength[i][j] = inaccessible;
	}

	Q.enQueue(Pair(startX, startY));  //起点入队
	routeLength[startX][startY] = 0;
	while (!Q.isEmpty()) {
		Pair p = Q.getFront();
		Q.deQueue(p);  //将队头元素出队并获取其坐标
		//如果该元素等于终点坐标,结束循环
		if (p.first == endX && p.second == endY)
			break;
		//尝试向该元素的右(1,0)、上(0,1)、左(-1,0)、下(0,-1)四个方向遍历
		for (int i = 0; i < 4; i++) {
			int nx = p.first + x[i];
			int ny = p.second + y[i];
			//防止越界以及保证访问的结点没有访问过,0表示障碍,1表示路径
			if (nx >= 0 && nx < m && ny >= 0 && ny < n && maze[nx][ny] != '0' && routeLength[nx][ny] == inaccessible) {
				//将该顶点入队
				Q.enQueue(Pair(nx, ny));
				//路径长度加1
				routeLength[nx][ny] = routeLength[p.first][p.second] + 1;
			}
		}
	}
	return routeLength[endX][endY];
}
main函数
char maze[100][100];
int m, n;//自定义迷宫大小,m行n列
int startX, startY, endX, endY;//迷宫起点和终点坐标
int main() {
	cout << "请输入迷宫规模(行数和列数,空格隔开):" << endl;
	cin >> m >> n;
	cout << "请输入迷宫(0表示不可走,1表示可走,S表示起点,E表示终点):" << endl;
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			cin >> maze[i][j];
			if (maze[i][j] == 'S') {
				startX = i;
				startY = j;
			}
			if (maze[i][j] == 'E')
			{
				endX = i;
				endY = j;
			}
		}
	}
	cout << "最短路径为:" << BFS(m, n, startX, startY, endX, endY, maze);
	return 0;
}
//m=5,n=6
/*
111011
1S0111
101011
1100E1
111111
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值