tags: algorithm data structure c++


#include<iostream>
#include<stack>
#include<queue>
using namespace std;
class Node
{
public:
Node(int a=0,int b=0,int c=0):x(a),y(b),di(c){}
int x;
int y;
int di;
};
int map[15][15] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //Sample maze, starting point (1,1), ending point (13,13)
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,1,1,1,1,0,0,0,0,1,1,1},
{1,1,0,1,1,1,1,1,0,1,1,0,1,1,1},
{1,1,0,1,1,1,1,1,1,1,1,0,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,1,1,1},
{1,1,1,1,1,0,1,1,1,1,1,0,1,1,1},
{1,1,1,1,1,0,0,1,1,1,0,0,1,1,1},
{1,1,1,1,1,1,0,0,1,1,0,1,1,1,1},
{1,1,1,1,1,1,1,0,1,1,0,1,1,1,1},
{1,1,1,1,0,0,0,0,1,1,0,1,1,1,1},
{1,1,1,1,1,1,1,0,1,1,0,1,1,1,1},
{1,1,1,1,0,1,1,0,1,1,1,1,1,1,1},
{1,1,1,1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
int passed[15][15] = { 0 }; //Judgement matrix
bool PassC(int x, int y)
{
if (!map[x][y] && !passed[x][y])
return true;
else
return false;
}
Node NextNode(Node temp)
{
switch (temp.di)
{
case 1:
temp.y++;
break;
case 2:
temp.x++;
break;
case 3:
temp.y--;
break;
case 4:
temp.x--;
break;
default:
break;
}
temp.di = 1;
return temp;
}
bool MazePathStack(stack<Node>& temp) //Stack
{
Node present(1, 1, 1);//
while (true)
{
if (PassC(present.x, present.y))
{
temp.push(present);
passed[present.x][present.y] = 1;
if (present.x == 13 && present.y == 13)
return true;
present=NextNode(present);
}
else
{
while (!temp.empty())
{
Node *t = &temp.top();
if (t->di != 4)
{
t->di++;
present = NextNode(*t);
break;
}
else
temp.pop();
}
if (temp.empty())
return false;
}
}
}
bool MazePathQueue(queue<Node>&temp) //queue
{
Node present(1, 1, 1);//
while (true)
{
if (PassC(present.x, present.y))
{
temp.push(present);
passed[present.x][present.y] = 1;
if (present.x == 13 && present.y == 13)
return true;
present = NextNode(present);
}
else
{
while (!temp.empty())
{
Node *t = &temp.back();
if (t->di != 4)
{
t->di++;
present = NextNode(*t);
break;
}
else
{
queue<Node> newq;
while (!(temp.front().x == t->x && temp.front().y == t->y))
{
newq.push(temp.front());
temp.pop();
}
temp.pop();
temp = newq;
}
}
if (temp.empty())
return false;
}
}
}
int main()
{
stack<Node>path;
queue<Node>Path;
Node point;
int choice;
cout << "Stack realizes input 1, queue realizes input 2";
cin >> choice;
if (choice == 1)
{
if (MazePathStack(path)) //Stack
while (!path.empty())
{
point = path.top();
cout << ++point.x << ":" << ++point.y << ",";
path.pop();
}
else
cout << "The path does not exist!";
}
else
{
if (MazePathQueue(Path)) //queue
{
while (!Path.empty())
{
point = Path.front();
cout << ++point.x << ":" << ++point.y << ",";
Path.pop();
}
}
else
cout << "The path does not exist!";
}
return 0;
}
1. What is the stack, what are the characteristics of the stack? stack: A special kind of linear table that allows only insert and delete element operations on a fixed end for data insertion and delet...
C language implementation data structure - stack and queue Stack Stack definition: Stack of ADT Stack sequential storage structure Stack chain storage structure Stack application queue Definition of q...
Article catalog Foreword Stack Stack.h Stack.c queue Queue.h Queue.c Conclude Foreword This article is mainly to make a paving for the next online OJ topic, because only the C language does the stack ...
This article records the stack and queue implementation code of the data structure and algorithm course Stack part stack.h stack.c Queue part queue.h queue.c...
Article Directory Definition of stack Stack related operations Push into the stack Unstack Empty the stack: Destroy the stack, release them one by one Calculate the current capacity of the pointer. Ca...
Article catalog First, data structure Data structure classification 2. List / array Second, the stack Stack implementation 2. Small application: parentheses matching problem Third, the queue 1. Realiz...
1. Stack advanced 2. Queue First in, first out ...
First, the queue 1) Queue is a first-in, first-out (FIFO) linear table. It only allows the deletion operation at the front end of the table. The insertion operation is performed at the back end of the...
JavaScript implementation stacks and queues This implementation is relatively simple, there is no function. Stack queue Stack queue...
Wonderfulness, encounter, fans! ! ! Stacks and queues are also a special linear watch. The stack is characterized by the first out, the queue is characterized by advanced first. Stack The stack is cha...