The definition of a two-dimensional array:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
It represents a labyrinth, in which 1 represents the wall, 0 way to go, can only go sideways or vertically to go, can not go sideways, requiring programmed to find the shortest route from the top left to the bottom right corner.
A 5 × 5 two-dimensional array, showing a maze. Ensure data has a unique solution.
Left to bottom right shortest path, the format as shown in the sample.
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
Find the path is very simple, basic bfs subject, but also to the path of the recording of this question, the method used here thinking and focused search and similar parent array, also used a Root parent node record for each array point, then, when the output of the direct recursion reverse output on the vans.
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
int _map[5][5];
bool vis[5][5];
int dic[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct point
{
int x,y,root;
};
point ans[25];
bool judge(int _x,int _y)
{
return _x>=0&&_x<5&&_y>=0&&_y<5;
}
void print(point p)
{
while(p.root!=-1)
{
print(ans[p.root]);
cout<<"("<<p.x<<", "<<p.y<<")\n";
return;
}
cout<<"(0, 0)\n";
}
void bfs(int x,int y)
{
point p;
p.x=x;
p.y=y;
queue<point> num;
num.push(p);
int cnt=0,k=0;
p.root=-1;
ans[0]=p;
cnt++;
while(!num.empty())
{
point q=num.front();
num.pop();
if(p.x==4&&p.y==4)
{
print(p);
return;
}
for(int i=0; i<4; i++)
{
int _x=q.x+dic[i][0];
int _y=q.y+dic[i][1];
if(!vis[_x][_y]&&!_map[_x][_y]&&judge(_x,_y))
{
vis[_x][_y]=true;
p.x=_x,p.y=_y;
p.root=k;
num.push(p);
ans[cnt++]=p;
}
}
k++;
}
}
int main()
{
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
vis[i][j]=false;
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
cin>>_map[i][j];
bfs(0,0);
}
Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15775 Accepted: 9389 Description The definition of a two-dimensional array: which represents a labyrinth, where 1 represen...
Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11844 Accepted: 7094 Description defines a two-dimensional array: which represents a labyrinth, where ...
http://poj.org/problem?id=3984 Ideas: the most basic bfs Recording the coordinates of the parent node and the final print path stl queue: Handwriting queue: ...
Topic: POJ3984, find the shortest path from the starting point to the upper left and lower right end print...
Maze Time Limit: 1000MS Memory Limit: 65536K Description The definition of a two-dimensional array: int maze[5][5] = { }; It represents a labyrinth, in which 1 represents the wall, 0 way to go, can on...
answer: With a two-dimensional array structure to save the coordinates of each point on a point by recursively reach the starting point when printing path backtracking ...
A node with a node pre [x] [y] recording pre [x] [y] by a recursive thereby reverse printing result....
Maze problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31780 Accepted: 18180 Description Define a two-dimensional array: It represents a maze, where 1 represents the wal...
Sample Input Sample Output Ideas Use a path array to store the labels that can be reached in each step for recursive output path. For example, the path array data in the sample is (below). You can cle...
K-The maze problem (POJ - 3984) Ideas: bfs+ path output, there are many ways to record the path....