我寫的code:
#include <iostream>
#include <fstream>
using namespace std;
const int DefaultSize = 10000;
enum Boolean {FALSE, TRUE};
template <class KeyType>
class Stack
{
template <class K>
friend ostream& operator<<(ostream&, Stack<K>& );
public:
Stack(int MaxStackSize = DefaultSize);
Boolean IsFull();
void Add (const KeyType& item);
// if IsFull(), then StackFull(); else insert item into the top of the stack.
Boolean IsEmpty();
// if number of elements in the stack is equal to 0, return TRUE (1) else return FALSE (0).
KeyType* Delete(KeyType&);
// if IsEmpty(), then StackEmpty(); else remove and return the topmost element of the stack
void StackEmpty() {cout << "empty" << endl;};
void StackFull() {cout << "full" << endl;};
void Output();
private:
int top;
KeyType *stack;
int MaxSize;
};
template <class KeyType>
Stack<KeyType>::Stack (int MaxStackSize) : MaxSize(MaxStackSize)
{
stack = new KeyType[MaxSize];
top = -1;
}
template <class KeyType>
inline Boolean Stack<KeyType>::IsFull()
{
if (top == MaxSize -1) return TRUE;
else return FALSE;
}
template <class KeyType>
inline Boolean Stack<KeyType>::IsEmpty()
{
if (top == -1) return TRUE;
else return FALSE;
}
template <class KeyType>
void Stack<KeyType>::Add (const KeyType& x)
// add x to the stack
{
if (IsFull()) StackFull();
else stack[++top] = x;
}
template <class KeyType>
KeyType* Stack<KeyType>::Delete (KeyType& x)
// remove and return top element from stack
{
if (IsEmpty()) {StackEmpty(); return 0;}
x = stack[top--];
return &x;
}
template <class KeyType>
ostream& operator<<(ostream& os, Stack<KeyType>& s)
{
os << "top = " << s.top << endl;
for (int i = 0; i <= s.top; i++)
os << i << ":" << s.stack[i] << endl;
return os;
}
struct offsets {
int a, b;
};
enum directions {N, NE, E, SE, S, SW, W, NW};
offsets movement[8];
struct items {
int x, y, dir;
};
ostream& operator<<(ostream& os, items& item)
{
return os << item.x << "," << item.y << "," << item.dir;
}
int maze2[100][100];
int mark2[100][100];
bool findPath(int i,int j){;
items temp;
temp.dir = E;
int d = temp.dir;
while (d < 8) // move forward
{
int g = i + movement[d].a; int h = j + movement[d].b;
if ((g == 9) && (h == 9)) return true;// reached exit
if((!maze2[g][h])&&!(mark2[g][h])){
findPath(g,h);
}
}
return false;
}
void driver(){
if(findPath(1,1))
cout<<"Success"<<endl;
else
cout<<"No path in maze"<<endl;
}
int main(){
ifstream inFile2( "/Volumes/DATA/大四下/Data structure/fds/maze1.in", ios::in);
if (!inFile2) {
cerr << "cannot open maze.in for input" << endl;
return 0;
}
char ch2;
for (int i = 0; i < 11; i++)
for (int j = 0; j < 11; j++)
{
if ( (!i) || (!j) || (i == 10) || (j == 10))
maze2[i][j] = 1;
else {
inFile2 >> ch2;
maze1[i][j] = ch2 - '0';
};
mark2[i][j] = 0;
}
}
老師給的Pseudo code:
感謝大大!:)
先简单描述一下出现了什么问题吧。
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y