poj 3278 Catch That Cow(第一道广搜题,(*^__^*) 嘻嘻……)

这两天因为想学搜索,所以把栈和队列给好好学了,看书学搜索。。。那《算法导论》和《数据结构》都是把搜索运用到图上,而且给的还是伪代码,不给具体例子,我怎么拿图实现啊??!!好吧。找题。。。找某亮百度空间滴~

 

他做的第一个是机器人那个,因为是中文题,看着好不习惯。。。看第二道英文题,抓住那头牛??额。好久没在北大站上做题了,界面好难看好难看啊,比浙大的还难看。。。还是喜欢poj(百练)的风格~废话不多说了。

 

搜索是吧,广搜,本来觉得还要把时间给存上,后来发现某亮代码上没有比较时间的,问之,理解了。广搜搜到的第一个人的位置与牛的位置相等的时候,那么肯定是最短的时间,因为搜索的每一步都是耗费相等的时间进行的,理解这个就好办啦~嘻嘻~~

 

基本代码都是边做边想边看他的。。。呵呵。。。不管啦,总之理解万岁~欧耶~~~~

 

PS:数组需要开大点。  +1  -1  *2,或者*2 +1 -1的顺序,要不会RE。运用队列知识。

 

 

#include <stdio.h> #include <stdlib.h> #include <string.h> int Q[300000]; int count[300000]; int sta[300000]; int n,head,tail; void Enq(int x) { Q[head++] = x; } int Deq(void) { return Q[tail++]; } int Qempty() { if( head == tail) return 1; return 0; } int main(void) { int N,K,temp; while(scanf("%d%d",&N,&K)!=EOF) { head = 0; tail = 0; memset(count,0,sizeof(count)); memset(Q,0,sizeof(Q)); memset(sta,0,sizeof(sta)); Enq(N); sta[N] = 1; while( !Qempty() ) { N = Deq(); if( N == K) { printf("%d/n",count[N]); break; } temp = N+1; if( temp<=100000 && sta[temp] == 0) { sta[temp] = 1; count[temp] = count[N] + 1; Enq(temp); } temp = N-1; if( temp>=0 && sta[temp] == 0) { sta[temp] = 1; count[temp] = count[N] + 1; Enq(temp); } temp = N*2; if( temp<=100000 && sta[temp] == 0) { sta[temp] = 1; count[temp] = count[N] + 1; Enq(temp); } } } system("pause"); return 0; }

你可能感兴趣的:(数据结构,算法,百度,System)