当前位置: 首页 > news >正文

数据结构—线性实习题目(二)5迷宫问题(栈)

迷宫问题(栈)

#include <iostream>​
#include <assert.h>
using namespace std;int qi1, qi2;
int n;
int m1, p1;
int** Maze = NULL;
int** mark = NULL;struct items
{int x, y, dir;
};struct offsets {int a, b;char* dir;
};const int stackIncreament = 20;
template <class T>
class SeqStack {
public:SeqStack(int sz = 50);~SeqStack() { delete[] elements; }void Push(const T& x);bool Pop(T& x);bool getTop(T& x);bool IsEmpty() const { return (top == -1) ? true : false; }bool IsFull() const { return (top == maxSize - 1) ? true : false; }int getSize() const { return top + 1; }void MakeEmpty() { top = -1; }void print();//friend ostream& operator<<(ostream& os, SeqStack<T>& s) {}
private:T* elements;int top;int maxSize;void overflowProcess();
};template <class T>
SeqStack<T>::SeqStack(int sz) : top(-1), maxSize(sz) {elements = new T[maxSize];assert(elements != NULL);
}template <class T>
void SeqStack<T>::overflowProcess() {T* newArray = new T[maxSize + stackIncreament];if (newArray == NULL) {cerr << "存储分配失败!" << endl;exit(1);}for (int i = 0; i < top; i++) newArray[i] = elements[i];maxSize += stackIncreament;delete[] elements;elements = newArray;
}template <class T>
void SeqStack<T>::Push(const T& x) {if (IsFull() == true) overflowProcess();elements[++top] = x;
}template <class T>
bool SeqStack<T>::Pop(T& x) {if (IsEmpty() == true) return false;x = elements[top--];return true;
}template <class T>
bool SeqStack<T>::getTop(T& x) {if (IsEmpty() == true) return false;x = elements[top];return true;
}template <class T>
void SeqStack<T>::print() {items item;SeqStack<items> temp(n);while (this->IsEmpty() == false) {this->Pop(item);temp.Push(item);}while (temp.IsEmpty() == false) {temp.Pop(item);cout << item.y << " " << item.x << endl;}
}offsets move1[4] = {{1,0,(char*)"N" },{0,-1,(char*)"W"},{-1,0,(char*)"S"},{0,1,(char*)"E"}};void path(int m, int p) {int i, j, d, g, h;mark[qi1][qi2] = 1;SeqStack<items> st(m * p);items tmp;tmp.x = qi1;tmp.y = qi2;tmp.dir = 0;st.Push(tmp);while (st.IsEmpty() == false) {st.Pop(tmp);i = tmp.x;j = tmp.y;d = tmp.dir;while (d < 4) {g = i + move1[d].a;h = j + move1[d].b;if (g == m1 && h == p1) {st.print();cout << j << " " << i << endl;cout << p1 << " " << m1 << endl;return;}if (Maze[g][h] == 0 && mark[g][h] == 0) {mark[g][h] = 1;tmp.x = i;tmp.y = j;tmp.dir = d;st.Push(tmp);i = g; j = h; d = 0;   // 修改}else d++;}}cout << "no path in Maze" << endl;
}
int m = 0, p = 0;
int main() {int i, j, num;cin >> m >> p;n = m * p;int** arr = new int* [p];for (int i = 0; i < p; i++) {arr[i] = new int[m];for (int j = 0; j < m; j++) {cin >> num;if (num == 4) {m1 = i;p1 = j;}if (num == 3) {qi1 = i; qi2 = j;num = 0;}arr[i][j] = num;}}Maze = arr;int** arr2 = new int* [p];for (int i = 0; i < p; i++) {arr2[i] = new int[m];for (int j = 0; j < m; j++) {arr2[i][j] = 0;}}mark = arr2;path(m1, p1);return 0;
}

http://www.lryc.cn/news/210426.html

相关文章:

  • Nginx 的配置文件(负载均衡,反向代理)
  • 项目管理49个过程定义与作用、五大过程组
  • MySQL篇---第六篇
  • QA新人入职任务
  • 更新电脑显卡驱动的操作方法有哪些?
  • [Docker]三.Docker 部署nginx,以及映射端口,挂载数据卷
  • 【0基础学Java第三课】-- 运算符
  • unocss和tailwindcss css原子引擎
  • HIT_OS_LAB1 调试分析 Linux 0.00 引导程序
  • C语言每日一题(18)数组匹配
  • redroid11 集成 nvidia gpu hals
  • 在 Visual Studio 中远程调试 C++ 项目
  • AAOS CarMediaService 问题分析
  • 06-Flask-蓝图的使用
  • 【LeetCode力扣】189 53 轮转数组 | 最大子数组和
  • Go学习第十七章——Gin中间件与路由
  • 真实感渲染的非正式调研与近期热门研究分享
  • matlab中字符串转换为数字(str2double函数)
  • 基于java的ssm框架农夫果园管理系统设计与实现
  • ctf md5爆破
  • 不同碳化硅晶体面带来的可能性
  • Kafka集群
  • 国腾GM8775C完全替代CS5518 MIPIDSI转2 PORT LVDS
  • 搜索与图论:匈牙利算法
  • 明星艺人类的百度百科怎么创建 ?
  • 类EMD的“信号分解方法”及MATLAB实现(第八篇)——离散小波变换DWT(小波分解)
  • python随手小练10(南农作业题)
  • How to install mongodb-7.0 as systemd service with podman
  • 一文彻底理解python浅拷贝和深拷贝
  • 什么是软件的生命周期?全方位解释软件的生命周期