DFS maze

DFS (non-recursive)

The first to write a maze.

In fact, it is not difficult, although not previously been hot, know what conditions it

For example, the maze is represented by two-dimensional array, there is a starting point and end point, road, and obstacles four elements

From the start, followed by searching the four directions, plus judgment conditions.

The end of the end of the encounter. Otherwise go down a number of conditions should be noted that grid.

1. is not an obstacle

2. Is there been accessed

3. The cross-border issues (as represented next to a grid coordinate is x or y is incremented or decremented 1, it is determined to have no bounds)

If the problem does not go to the next grid

And modifying the variable, then the appropriate action

If they meet certain conditions, the direction impassable, continue to search for the other direction, if other directions are impassable,

The anti-back on a grid, began to continue to search for the other direction from the grid (here more difficult to understand, I think,

Because DFS is a direction if it has to go through in that direction, once the barrier, the return on the grid there are other direction is not found, so after returning to continue the search in other directions

), So you can find the finish line, as to return to this path, there should be a different approach, I used to save storage path stack structure.

 

#include <bits/stdc++.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

//坐标
typedef struct{
int x;
int y;
}position;

stack<position> dfs(int map[5][4],int tag[5][4],position start,position end){
position curpos=start;
curpos.x=start.x;
curpos.y=start.y;
stack<position> s;
s.push(start);
while(true){
if(curpos.x==2 && curpos.y==3){
break;
}
if(tag[curpos.x][curpos.y+1]==0 && map[curpos.x][curpos.y+1]!=1 && (curpos.y+1)<4 ){
curpos.y+=1;
s.push(curpos);
tag[curpos.x][curpos.y]=1;
}else if(tag[curpos.x][curpos.y-1]==0 && 1!=map[curpos.x][curpos.y-1] && (curpos.y-1)>=0){
curpos.y-=1;
s.push(curpos);
tag[curpos.x][curpos.y]=1;

}else if(tag[curpos.x+1][curpos.y]==0 && 1!=map[curpos.x+1][curpos.y] && (curpos.x+1)<5){
curpos.x+=1;
s.push(curpos);
tag[curpos.x][curpos.y]=1;

}else if(tag[curpos.x-1][curpos.y]==0 && 1!=map[curpos.x-1][curpos.y] && (curpos.x-1)>=0){
curpos.x-=1;
s.push(curpos);
tag[curpos.x][curpos.y]=1;

}else{
s.pop();
curpos=s.top();
}
}
return s;
}


int main(int argc, char** argv) {
int exitforstarandend=0;
int map[5][4]={
{666,0,0,1},
{1,0,1,1},
{0,0,1,999},
{0,1,0,0},
{0,0,0,1}
};
int tag[5][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
position start,end,pos;
//找到起点666和终点999
for(int i=0;i<5;i++){
for(int j=0;i<4;j++){
if(666==map[i][j]){
start.x=i;
start.y=j;
exitforstarandend++;
}
if(999==map[i][j]){
end.x=i;
end.y=j;
exitforstarandend++;
}
if(2==exitforstarandend){
break;
}
}
}
//获取路径栈(倒叙)
stack<position> s1=dfs(map,tag,start,end);
stack<position> s2;
position p1;
//正序
while(!s1.empty()){
p1=s1.top();
s2.push(p1);
s1.pop();
}
//打印路径(按坐标打印)
while(!s2.empty()){
p1=s2.top();
cout<<p1.x<<","<<p1.y<<endl;
s2.pop();
}
return 0;
}

Guess you like

Origin www.cnblogs.com/hyf001/p/11994841.html