采用栈的结构实现:
void dfs(point start)
{point next,tem;
stack<point>q;//建栈,最好写在函数里面
q.push(start);//入栈
while(!q.empty())
{
tem=q.top();//提取栈顶元素
if(tem.x==m&&tem.y==n)//处理过程
{
ok=1;
return ;
}
q.pop();//处理过后出栈
v[tem.x][tem.y]=1//标记已被访问
;
for(int i=0;i<4;i++)//访问四周的点,访问不仅限于四周,具体情况具体分析。{
next.x=tem.x+x[i],next.y=tem.y+y[i];
if(inmap(next)&&a[next.x][next.y]&&!v[next.x][next.y])//满足条件则入栈
q.push(next);
}
}