怎么没有源程序参考一下啊,
[CODE]
#ifndef SStack_H
#define SStack_H
// 版权没有。。。。欢迎盗版.... -_-ii
// Made By [Stupid.ET] Cedric Porter
#include <iostream>
#include <cassert>
using namespace std;
template<class T>
class Stack;
template<class T>
class Node
{
T data;
Node *next;
public:
Node(T d, Node<T>* n) { data = d; next = n;}
friend class Stack<T>;
};
template<class T>
class Stack
{
Node<T> *head, *iter;
unsigned long sz;
public:
Stack() {head = iter = NULL; sz = 0;}
~Stack() {empty();}
void empty();
void push(T);
void pop();
T top();
unsigned long size();
};
template<class T>
void Stack<T>::empty()
{
while (head != NULL)
{
iter = head;
head = head->next;
delete head;
}
sz = 0;
}
template<class T>
void Stack<T>::push(T d)
{
if (head == NULL)
head = new Node<T>(d, NULL);
else
head = new Node<T>(d, head);
sz++;
}
template<class T>
void Stack<T>::pop()
{
assert(head != NULL);
sz--;
if (head->next == NULL)
{
delete head;
head = NULL;
return;
}
iter = head;
head = head->next;
delete iter;
}
template<class T>
T Stack<T>::top()
{
assert(head != NULL);
return head->data;
}
template<class T>
unsigned long Stack<T>::size()
{
return sz;
}
#endif
[/CODE]
[CODE]
#include "SStack.h"
#include <iostream>
#include <fstream>
// The way to output
#define output_1
// Option
using namespace std;
struct Position
{
int x,y;
};
const int x = 6, y = 6;
int main()
{
int a[x][y];
ifstream inf("m.txt");
for (int i = 0; i <= x; i++)
for (int j = 0; j <= y; j++)
inf >> a[i][j];
Stack<Position> s;
//stack< Position , list<Position> > s;
bool go = true;
Position p;
p.x = p.y = 0;
s.push(p);
while (go)
{
if (s.top().x > x || s.top().y > y)
{
s.pop();
s.pop();
}
go = false;
if (a[s.top().x+1][s.top().y] == 0)
{
p.x = s.top().x + 1;
p.y = s.top().y;
s.push(p);
a[p.x][p.y] = 1;
go = true;
}
else if (a[s.top().x][s.top().y+1] == 0)
{
p.x = s.top().x;
p.y = s.top().y + 1;
s.push(p);
a[p.x][p.y] = 1;
go = true;
}
else if (a[s.top().x-1][s.top().y] == 0)
{
p.x = s.top().x - 1;
p.y = s.top().y;
s.push(p);
a[p.x][p.y] = 1;
go = true;
}
else if (a[s.top().x][s.top().y-1] == 0)
{
p.x = s.top().x;
p.y = s.top().y - 1;
s.push(p);
a[p.x][p.y] = 1;
go = true;
}
else
{
s.pop();
go = true;
}
if (s.top().x == x && s.top().y == y)
{
break;
}
}
//*
// Output
#ifdef output_1
Stack<Position> t;
while (s.size() != 0)
{
p = s.top();
p.x++;
p.y++;
t.push(p);
s.pop();
}
while (t.size() != 0)
{
cout << t.top().x << ' ' << t.top().y << endl;
t.pop();
}
#else
int ii, jj;
for (ii = 0; ii < x; ii++)
for (jj = 0; jj < y; jj++)
a[ii][jj] = 1;
while (s.size() != 0)
{
a[s.top().x][s.top().y] = 31;
s.pop();
}
for (ii = 0; ii < x; ii++)
{
for (jj = 0; jj < y; jj++)
cout << char(a[ii][jj]+1) << ' ';
cout << endl;
}
#endif
//*/
system("pause");
return 0;
}
[/CODE]