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)