【求助】BFS 类似迷宫问题

图片说明
运行没有结果 附上我自己写的代码 有人能帮忙看看吗 麻烦啦
#include
#include
#include
#include
using namespace std;
struct node
{
int x;
int y;
char dir;
}pre[105][105];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int visit[105][105];
int map[105][105];
node sta, end;
int flag = 0;
int m, n;

void bfs()
{
queuep;
queueq;
int i;
visit[sta.x][sta.y] = 1;
node temp,next;
p.push(sta.x);
q.push(sta.y);
while(!p.empty())
{
temp.x = p.front();
p.pop();
temp.y = q.front();
q.pop();
for(i = 0; i < 4; i++)
{
if((i == 0) || (i == 1)) next.dir = 'h';
else next.dir = 's';
next.x = temp.x + dx[i];
next.y = temp.y + dy[i];
if((map[next.x][next.y] == 0) && (!visit[next.x][next.y]))
{
visit[next.x][next.y] = 1;
p.push(next.x);
q.push(next.y);
pre[next.x][next.y].x = temp.x;
pre[next.x][next.y].y = temp.y;
pre[next.x][next.y].dir = next.dir;
if( ((next.x == end.x - 1) &&(next.y == end.y)) || ((next.x == end.x + 1) &&(next.y == end.y)) )
{
pre[end.x][end.y].x = next.x;
pre[end.x][end.y].y = next.y;
pre[end.x][end.y].dir = 's';
flag = 1;
break;
}

else if( ((next.x == end.x) &&(next.y == end.y - 1)) ||((next.x == end.x) &&(next.y == end.y + 1)) )
{
pre[end.x][end.y].x = next.x;
pre[end.x][end.y].y = next.y;
pre[end.x][end.y].dir = 'h';
flag = 1;
break;
}
}
}
if(flag) break;
}
}

int main()
{
int i, j, k, p, q, count = 1;
char direction[105];
memset(direction,'\0',sizeof(direction));
memset(map,0,sizeof(map));
memset(visit, 0, sizeof(visit));
scanf("%d %d", &m, &n);
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &map[i][j]);
scanf("%d %d",&sta.x, &sta.y);
scanf("%d %d",&end.x, &end.y);

bfs();
k = 0;
if(flag)
{
i = end.x;
j = end.y;
while((pre[i][j].x != sta.x) && (pre[i][j].y != sta.y))
{
direction[k++] = pre[i][j].dir;
p = pre[i][j].x;
q = pre[i][j].y;
i = p;
j = q;
}
for(i = 0; direction[i + 1] != '\0'; i++)
{
if(direction[i + 1] != direction[i])
count++;

}
if(count <= 3)
printf("TRUE\n");
else printf("FALSE\n");
}
else printf("FALSE\n");
return 0;

}

1个回答

问题已经自己解决了,开心~~~

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问