POJ 3278 Catch that Cow (BFS)

Source: Internet
Author: User

Topic Link: Catch that Cow


Resolution: two numbers n and K, three operations: + 1,-1, * *, q, n at least how many times the operation can be equal to K.

The simplest BFS template has, note

Condition of +1: x+1 <= k

-1 conditions: x-1 >= 0

Conditions: x <= k && 2*x <= 100000




AC Code:

#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include < Cstring>using namespace std;struct Node {int n, step;};    BOOL Vis[100005];int BFS (int n, int k) {memset (Vis, false, sizeof (VIS));    Queue<node> Q;    Q.push (node{n, 0}); while (!        Q.empty ()) {Node now = Q.front ();        Q.pop ();        if (NOW.N = = k) return now.step;            if (now.n+1 <= k &&!vis[now.n+1]) {vis[now.n+1] = true;        Q.push (node{now.n+1, now.step+1});            } if (now.n-1 >= 0 &&!vis[now.n-1]) {vis[now.n-1] = true;        Q.push (node{now.n-1, now.step+1});            } if (NOW.N <= k && 2*now.n <= 100000 &&!VIS[2*NOW.N]) {VIS[2*NOW.N] = true;        Q.push (NODE{2*NOW.N, now.step+1}); }} return-1;}    int main () {//Freopen ("In.txt", "R", stdin);    int n, K; while (scanf ("%d%d", &n, &k) = = 2) {printf ("%d\n", BFS (N, K)); } return 0;}



Copyright NOTICE: This article is sxk original article, reprint please attach this article link ^_^

POJ 3278 Catch that Cow (BFS)

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.