- Topic first
Maze problem
enter
The input contains multiple sets of test data. The first line of input is an integer T, which means there are T groups of test data.
The first line of each input is two integers N and M (1<=N, M<=100).
In the next N lines, enter M characters in each line, and each character represents a small square in the maze.
The meanings of the characters are as follows:
'S': start point,
'E': end point,
'-': open space, can be passed through
'#': obstacle, cannot be passed. The
input data is guaranteed to have one and only one starting point and end point.
Output
Output
For each group of inputs, output the shortest distance from the start point to the end point. If there is no way from the start point to the end point, output -1.
Sample
enter
Output
9
Problem solving ideas & illustrations
At first I felt that both stacks and queues could be used, but later I discovered the problem after careful study !
- Here assume that the direction of our pathfinding is upper right, lower left
Stack diagram
Visit sequentially in the order of pathfinding, and push to the stack if the conditions are met
Code
#include<bits/stdc++.h>
#define ll long long
#define mst(a) memset(a,0,sizeof(a))
using namespace std;
char mapp[105][105];
//char test[5][5] = { {'S','-','#','#','#'},{'-','-','-','-','-'},{'#','#','-','-','-',},{'E','#','-','-','-',},{'-','-','-','#','#',} };
int dex[4] = {
-1,0,1,0 };
int dey[4] = {
0,1,0,-1 };
int book[105][105] = {
0 };
typedef struct
{
int x, y, step;
}Position;
void maze(Position start, Position end, int n, int m) {
stack<Position> mystack;
start.step = 0;
mystack.push(start);
book[start.x][start.y] = 1;
Position temp = start;
while (!mystack.empty())
{
Position temp2 = mystack.top();
mystack.pop();
if (temp2.x == end.x && temp2.y == end.y) {
cout << temp2.step << endl;
return;
}
for (int i = 0; i < 4; i++) {
int dexs = temp2.x + dex[i];
int deys = temp2.y + dey[i];
if (dexs >= 0 && dexs < n && deys >= 0 && deys < m && !book[dexs][deys] && mapp[dexs][deys] != '#') {
temp.x = dexs;
temp.y = deys;
book[dexs][deys] = 1;
temp.step = temp2.step + 1;
mystack.push(temp);
}
}
}
cout << -1 << endl;
}
int main() {
int T;
Position start;
Position end;
int n, m;
while (cin >> T)
{
while (T--) {
mst(book);
mst(mapp);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> mapp[i][j];
if (mapp[i][j] == 'S') {
start = {
i,j };
}
else if (mapp[i][j] == 'E') {
end = {
i,j };
}
}
}
maze(start, end, n, m);
}
}
return 0;
}
Queue diagram
Code
#include<bits/stdc++.h>
#define ll long long
#define mst(a) memset(a,0,sizeof(a))
using namespace std;
char mapp[105][105];
//char test[5][5] = { {'S','-','#','#','#'},{'-','-','-','-','-'},{'#','#','-','-','-',},{'E','#','-','-','-',},{'-','-','-','#','#',} };
int dex[4] = {
-1,0,1,0 };
int dey[4] = {
0,1,0,-1 };
int book[105][105] = {
0 };
typedef struct
{
int x, y, step;
}Position;
void maze(Position start, Position end, int n, int m) {
queue<Position> mystack;
start.step = 0;
mystack.push(start);
book[start.x][start.y] = 1;
Position temp = start;
while (!mystack.empty())
{
Position temp2 = mystack.front();
mystack.pop();
if (temp2.x == end.x && temp2.y == end.y) {
cout << temp2.step << endl;
return;
}
for (int i = 0; i < 4; i++) {
int dexs = temp2.x + dex[i];
int deys = temp2.y + dey[i];
if (dexs >= 0 && dexs < n && deys >= 0 && deys < m && !book[dexs][deys] && mapp[dexs][deys] != '#') {
temp.x = dexs;
temp.y = deys;
book[dexs][deys] = 1;
temp.step = temp2.step + 1;
mystack.push(temp);
}
}
}
cout << -1 << endl;
}
int main() {
int T;
Position start;
Position end;
int n, m;
while (cin >> T)
{
while (T--) {
mst(book);
mst(mapp);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> mapp[i][j];
if (mapp[i][j] == 'S') {
start = {
i,j };
}
else if (mapp[i][j] == 'E') {
end = {
i,j };
}
}
}
maze(start, end, n, m);
}
}
return 0;
}
difference
Although only the declaration of queue and stack and the method of taking the top element of the container have been
changed, the change is terrible
Here I want to thank the tx boss for sacrificing time and studying with me--