qq_36014942 2016-12-26 08:41 采纳率: 0%
浏览 2376

【求助】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条回答

  • qq_36014942 2016-12-26 14:02
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler