POJ 3278 Catch that Cow

Source: Internet
Author: User

Problem-Solving ideas: Simple wide search, the key is pruning.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 using namespacestd;6 Const intMAXN =100005;7 intVIS[MAXN];//Mark whether this point has passed8 intN, K;9 Ten  One structnode{ A     intx, CNT; - }p, now; -  theQueue<node>Q; -  - intBFsintXintCNT) - { +memset (Vis,0,sizeof(VIS));//initialized to never go through -      while(!q.empty ()) Q.pop (); +p.x = x, p.cnt =CNT; A Q.push (p); at  -      while(!q.empty ()) -     { -p =Q.front (); - Q.pop (); -  in         if(p.x = = k)returnp.cnt;//go to that point and return directly to the number of steps -  tonow.x = p.x +1, now.cnt = p.cnt +1; +         if(Now.x <=100000&&!vis[now.x])//critical pruning, not traversed and within the data range that meets the topic -         { theVis[now.x] =1;//marked as having traversed * Q.push (now); $         }Panax Notoginseng  -now.x = p.x-1, now.cnt = p.cnt +1; the         if(Now.x >=0&&!vis[now.x]) +         { AVis[now.x] =1; the Q.push (now); +         } -  $now.x =2* p.x, now.cnt = p.cnt +1; $         if(Now.x <=100000&&!vis[now.x]) -         { -Vis[now.x] =1; the Q.push (now); -         }Wuyi     } the     return-1;//won't come to this step - } Wu  - intMain () About { $      while(~SCANF ("%d%d", &n, &k)) -     { -         intAns = BFS (n,0); -printf"%d\n", ans); A     } +     return 0; the}
View Code

POJ 3278 Catch that Cow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.