#以下代码 在蓝桥杯dfs 迷宫的题目中 提交是正确的
但是 在dev测试的时候 却一直输出99999
谁能帮我看一下问题在哪
#include
#include
#include
#include
typedef struct Node {
int x;//位置信息
int y;
int pre;//前一个结点下标
char aa;//当前结点的下一步走法L/R/D/U
} node;
node nodes[1500];
int mg[30][50]; //迷宫
int f[30][50]= {0}; //辅助数组,记录已经访问结点
int pos[4][2]= {{1,0},{0,-1},{0,1},{-1,0}}; //上下左右
char sol[1500];
int s=0;
void print(int head) {
while(nodes[head].pre!=-1) {
sol[s++]=nodes[head].aa;
print(nodes[head].pre);
break;
}
}
void bfs(int x,int y) {
int head=0,tail=1;
int i;
nodes[0].x=x;
nodes[0].y=y;
nodes[0].pre=-1;
struct Node next;
while(headif(nodes[head].x==29&&nodes[head].y==49) {
print(head);
}
for(i=0; i<4; i++) {
next.x=nodes[head].x+pos[i][0];
next.y=nodes[head].y+pos[i][1];
next.pre=head;
if(next.x<30&&next.x>-1&&next.y<50&&next.y>-1) {
if(mg[next.x][next.y]!=1&&f[next.x][next.y]!=1) {
f[next.x][next.y]=1;
if(i==0) next.aa='D';
if(i==1) next.aa='L';
if(i==2) next.aa='R';
if(i==3) next.aa='U';
nodes[tail]=next;
tail++;
}
}
}
head++;
}
}
int main() {
int i,j;
for(i=0; i<30; i++) {
for(j=0; j<50; j++) {
scanf("%1d",&mg[i][j]);
}
}
bfs(0,0);
int len=strlen(sol);
for(i=0; iprintf("%c\n",sol[i]);
}
}
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
深度优先遍历(Depth_First_Search),也称为深度优先搜索,简称DFS。