#include <iostream>
#include <assert.h>
using namespace std;
int qi1, qi2;
int n;
int m1, p1;
int** Maze = NULL;
int** mark = NULL;
int m = 0, p = 0;
static int count1 = 0;
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();
void Baocun();
int gtop() {
return top;
}
//friend ostream& operator<<(ostream& os, SeqStack<T>& s) {}
T* elements;
int top;
int maxSize;
void overflowProcess();
};
SeqStack<items> Array[100];
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);
}
cout << "(" << p << "," << m << ")" << endl;
while (temp.IsEmpty() == false) {
temp.Pop(item);
cout << "(" << item.x << "," << item.y << ")" << endl;
}
}
template <class T>
void SeqStack<T>::Baocun() {
items item;
SeqStack<items> temp(n);
SeqStack<items> temp2(n);
while (this->IsEmpty() == false) {
this->Pop(item);
temp.Push(item);
temp2.Push(item);
}
Array[count1] = temp;
count1++;
while (temp2.IsEmpty() == false) {
temp2.Pop(item);
this->Push(item);
}
this->Pop(item);
}
void Sort(SeqStack<items> *Array1) {
int min;
int sclect = 0;
min = Array1[0].gtop();
for (int i = 0; i < count1; i++) {
if (Array1[i].gtop() < min) {
min = Array1[i].gtop();
sclect = i;
}
}
Array1[sclect].print();
}
offsets move1[8] = {
{-1, 0, (char*)""},
{1, 1, (char*)""},
{1,0,(char*)"N" },
{1,-1,(char*)"W"},
{0,-1,(char*)"S"},
{-1,-1,(char*)"E"},
{-1, 1, (char*)""},
{0, 1, (char*)""}
};
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;
//mark[i][j] = 0;
while (d < 8) {
g = i + move1[d].a;
h = j + move1[d].b;
if (g == m1 && h == p1) {
tmp.x = i; tmp.y = j; tmp.dir = d;
st.Push(tmp);
st.Baocun();
d++;
continue;
}
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 main() {
int i, j, num;
cin >> p >> m;
n = m * p;
int** arr = new int* [p + 2];
for (int i = 0; i < p + 2; i++) {
arr[i] = new int[m + 2];
for (int j = 0; j < m + 2; j++) {
arr[i][j] = 1;
}
}
for (int i = 1; i < p + 1; i++) {
for (int j = 1; j < m + 1; j++) {
cin >> num;
arr[i][j] = num;
}
}
Maze = arr;
qi1 = 1; qi2 = 1;
m1 = p; p1 = m;
int** arr2 = new int* [p + 2];
for (int i = 0; i < p + 2; i++) {
arr2[i] = new int[m + 2];
for (int j = 0; j < m + 2; j++) {
arr2[i][j] = 0;
}
}
mark = arr2;
path(m1, p1);
Sort(Array);
return 0;
}
有点问题,希望大佬指教。^_^