dengxinyi il y a 6 ans
Parent
commit
946cac5e7d

+ 48 - 0
poj/3278.catch-that-cow/Main.java

@@ -0,0 +1,48 @@
+import java.io.*;
+import java.util.*;
+
+public class Main {
+  public static void main(String[] args) throws IOException {
+    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
+    String s;
+    int N, K;
+    boolean[] set = new boolean[200001];
+    Queue<Integer> queue = new LinkedList<Integer>();
+    while ((s = in.readLine()) != null) {
+      String[] nk = s.split("\\s+");
+      N = Integer.parseInt(nk[0]);
+      K = Integer.parseInt(nk[1]);
+      Arrays.fill(set, 0, Math.max(N + 1, 2 * K + 1),false);
+      queue.clear();
+      queue.offer(N);
+      set[N] = true;
+      int cnt = 1, min = 0;
+      while (cnt != 0) {
+        int n = queue.poll();
+        cnt--;
+        if (n == K) break;
+        if (n < K) {
+          if (!set[2 * n]) {
+            queue.offer(2 * n);
+            set[2 * n] = true;
+          }
+          if (!set[n + 1]) {
+            queue.offer(n + 1);
+            set[n + 1] = true;
+          }
+        }
+        if (0 < n && !set[n - 1]) {
+          queue.offer(n - 1);
+          set[n - 1] = true;
+        }
+        if (cnt == 0) {
+          cnt = queue.size();
+          min++;
+        }
+      }
+      out.println(min);
+      out.flush();
+    }
+  }
+}

+ 46 - 0
poj/3278.catch-that-cow/Solution.java

@@ -0,0 +1,46 @@
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.Scanner;
+
+public class Solution {
+    private static int catchCow(int N, int K) {
+        boolean[] used = new boolean[100001];
+        Queue<Integer> queue = new LinkedList<Integer>();
+        queue.offer(N);
+        used[N] = true;
+        int size = queue.size();
+        int cnt = 0;
+        while (!queue.isEmpty()) {
+            Integer i = queue.poll();
+            size--;
+            if (i == K) return cnt;
+            if (i - 1 >= 0 && !used[i - 1]) {
+                queue.offer(i - 1);
+                used[i - 1] = true;
+            }
+            if (i + 1 <= 100000 && !used[i + 1]) {
+                queue.offer(i + 1);
+                used[i + 1] = true;
+            }
+            if (i * 2 <= 100000 && !used[i * 2]) {
+                queue.offer(i * 2);
+                used[i * 2] = true;
+            }
+            if (size == 0) {
+                size = queue.size();
+                cnt++;
+            }
+        }
+        return -1;
+    }
+
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        while (scanner.hasNextInt()) {
+          int N = scanner.nextInt();
+          int K = scanner.nextInt();
+          System.out.println(catchCow(N, K));
+        }
+        scanner.close();
+    }
+}

+ 58 - 0
poj/3278.catch-that-cow/Test.java

@@ -0,0 +1,58 @@
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class Test {
+  private static final int MAXN = 100000;
+  private static boolean[] used = new boolean[MAXN + 1];
+  private static Queue<Integer> queue = new LinkedList<Integer>();
+
+  public static void main(String[] args) throws IOException{
+    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
+    String s;
+    int N, K;
+    while ((s = in.readLine()) != null) {
+      String[] nk = s.split("\\s+");
+      N = Integer.parseInt(nk[0]);
+      K = Integer.parseInt(nk[1]);
+      out.println(catchCow(N, K));
+    }
+    out.flush();
+  }
+
+  private static int catchCow(int N, int K) {
+    queue.clear();
+    Arrays.fill(used, false);
+    queue.offer(N);
+    used[N] = true;
+    int size = queue.size(), cnt = 0;
+    while (size != 0) {
+      int i = queue.poll();
+      size--;
+      if (i == K) break;
+      if (i - 1 >= 0 && !used[i - 1]) {
+        queue.offer(i - 1);
+        used[i - 1] = true;
+      }
+      if (i + 1 <= MAXN && !used[i + 1]) {
+        queue.offer(i + 1);
+        used[i + 1] = true;
+      }
+      if (i * 2 <= MAXN && !used[i * 2]) {
+        queue.offer(i * 2);
+        used[i * 2] = true;
+      }
+      if (size == 0) {
+        size = queue.size();
+        cnt++;
+      }
+    }
+    return cnt;
+  }
+}

+ 21 - 0
poj/3278.catch-that-cow/data.cc

@@ -0,0 +1,21 @@
+#include <cstdio>
+#include <cstdlib>
+#include <ctime>
+
+const unsigned long long N = 100001;
+
+int get_rand() {
+  unsigned long long r = rand() * rand() + rand();
+  return (int) (r % N);
+}
+
+int main(int argc, char **argv) {
+  int n, k, c = atoi(argv[1]);
+  srand((int) time(0));
+  for (int i = 0; i < c; i++) {
+    n = get_rand();
+    k = get_rand();
+    printf("%d %d\n", n, k);
+  }
+  return 0;
+}

+ 29 - 27
poj/3278.catch-that-cow/main.cc

@@ -10,36 +10,38 @@ bool set[L];
 
 int main() {
   int N, K;
-  scanf("%d %d", &N, &K);
-  queue<int> q;
-  q.push(N);
-  memset(set, 0, sizeof(set));
-  set[N] = true;
-  int s = 1, t = 0;
-  while (s != 0) {
-    int n = q.front();
-    q.pop();
-    s--;
-    if (n == K) break;
-    if (n < K) {
-      if (!set[2 * n]) {
-        q.push(2 * n);
-        set[2 * n] = true;
+  while (scanf("%d %d", &N, &K) != EOF) {
+    queue<int> q;
+    q.push(N);
+    memset(set, 0, sizeof(set));
+    set[N] = true;
+    int s = 1, t = 0;
+    while (s != 0) {
+      int n = q.front();
+      q.pop();
+      s--;
+      if (n == K) break;
+      if (n < K) {
+        if (!set[2 * n]) {
+          q.push(2 * n);
+          set[2 * n] = true;
+        }
+        if (!set[n + 1]) {
+          q.push(n + 1);
+          set[n + 1] = true;
+        }
       }
-      if (!set[n + 1]) {
-        q.push(n + 1);
-        set[n + 1] = true;
+      if (0 < n && !set[n - 1]) {
+        q.push(n - 1);
+        set[n - 1] = true;
+      }
+      if (s == 0) {
+        s = q.size();
+        t++;
       }
     }
-    if (0 < n && !set[n - 1]) {
-      q.push(n - 1);
-      set[n - 1] = true;
-    }
-    if (s == 0) {
-      s = q.size();
-      t++;
-    }
+    printf("%d\n", t);
   }
-  printf("%d\n", t);
+
   return 0;
 }

+ 8 - 0
poj/3278.catch-that-cow/test.cc

@@ -0,0 +1,8 @@
+#include <cstdlib>
+
+int main() {
+  system("./a.out < in.txt > a.txt");
+  system("java Test < in.txt > b.txt");
+  system("sdiff -s a.txt b.txt > c.txt");
+  return 0;
+}