广度优先搜索学习

#include<stdio.h>
struct note
{
int x;
int y;
int f;
int s;

};
int main()
{
struct note que[2501];
int a[51][51] = { 0 }, book[51][51] = { 0 };
int m, n, i, j, k, tail=0, head=0, p, q, startx, starty, flag=0,tx,ty;
scanf("%d %d", &n, &m);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
scanf("%d%d%d%d", &startx, &starty, &p, &q);
int next[4][4] = { {0,1},{1,0},{0,-1},{-1,0} };
book[1][1] = 1;
que[tail].x = 1;
que[tail].y = 1;
que[tail].s = 0;
tail++;
while (head < tail)
{
for (k = 0; k <= 3; k++)
{
tx = que[head].x + next[k][0];
ty = que[head].y + next[k][1];
if (tx > n || tx<1 || ty>m || ty < 1)
continue;
if (a[tx][ty] == 0 && book[tx][ty] == 0)
{
que[tail].x = tx;
que[tail].y = ty;
que[tail].s = que[head].s + 1;
tail++;
}
if (tx == p && ty == q)
flag = 1;
}
if (flag == 1)
break;
head++;
}
printf("%d", que[tail - 1].s);
getchar(); getchar();
return 0;
}
学习了广度优先搜索
广搜和深搜有啥区别呢?
深搜运用了递归,不达目标不罢休,当step或者其他因素符合条件后,就达到条件了,就return到前一种状态,尝试其他方式。
广搜没用递归,只运用了两层循环,在向目标前进时,是一层一层式的,在到达终点后不需返回,因为在最后一层包含所有符合条件的情况了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值