SDUT1028POJ3278Catch That Cow

mac2022-06-30  111

这题。。。

我想说是不是在家太舒服了以至于一个BFS都敲不出来了。。。

访问过的数要标记不能在访问了自己不知道啊!!!

老是RE想不到这个问题么!!!

非得看人家题解你才想起来啊!!!

人家说要不是别人告诉是BFS真的想不到这里,可是我知道是BFS都做不出来啊。。。sad

这是我看的题解链接:http://blog.csdn.net/lyy289065406/article/details/6647886

感谢他。

这个题有几点需要注意:

1、有可能起点是0。

2、注意剪枝。

3、注意标记。(我就是因为这个总是RE)

下面贴上我的代码(其实是完全借鉴模版的,但研究懂了自己再敲也是有收获):

#include<stdio.h> #include <string.h> const int large=200030; typedef struct node { int x,step; }pos; int n,k; bool vist[large]; //数组较大,必须开为全局数组,不然肯定RE pos qu[large]; int main() { while(scanf("%d%d",&n,&k)!=EOF) { memset(vist,false,sizeof(vist)); int head,tail; qu[head=tail=0].x=n; qu[tail++].step=0; vist[n]=true; while(head<tail) { pos w=qu[head++]; if(w.x==k) { printf("%d\n",w.step); break; } if(w.x-1>=0 && !vist[w.x-1]) //w.x-1>=0 是剪枝 { vist[w.x-1]=true; qu[tail].x=w.x-1; qu[tail++].step=w.step+1; } if(w.x<=k && !vist[w.x+1]) //w.x<=k 是剪枝 { vist[w.x+1]=true; qu[tail].x=w.x+1; qu[tail++].step=w.step+1; } if(w.x<=k && !vist[2*w.x]) //w.x<=k 是剪枝 { vist[2*w.x]=true; qu[tail].x=2*w.x; qu[tail++].step=w.step+1; } } } return 0; }

转载于:https://www.cnblogs.com/lin375691011/p/3996772.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)