poj3278 Catch That Cow

http://poj.org/problem?id=3278

Title: John, the farmer, learned of the whereabouts of a fleeing cow and wanted to catch her immediately. He starts from a point, N (0≤N≤100000) is on the number line, and the cattle point K (0≤K≤100000) is on the same number line. Farmer John has two modes of transportation: walking and teleportation.

*Walk: FJ can move from any point X to point X - 1 or X + 1 in one minute.

* Teleportation: FJ can move from any point X to point 2 in one minute.

If the cow doesn't know its quest and doesn't move at all, how long will it take Farmer John to get it back?

 

That is to say, if the position of John is greater than or equal to the position of the cow, then the optimal solution can only be obtained directly by going backwards. If John's position is smaller than the cow's position, then each node has three optional nodes, which are the current node +1, -1, *2.

Select an appropriate pruning function. When the current node is out of bounds, that is, <0 or >100000, the corresponding node will not enter the queue. When a feasible solution (optimal solution) is found, it will reach the position of the cow, and directly return to the most Optimal solution step[k].

 

Algorithm idea: Queue-type branch-and-bound method, searches the ternary tree with breadth first, and sets up a queue q to store the entry from John's position, then dequeue and start to expand three sub-nodes (three sub-nodes are enqueued). Set an array step[i] to record the minimum time to go to a certain position, and set an array vis[i] to record whether you have been to a certain position, if you have been there, it means that there is a better solution in front, you can discard the corresponding child node ( not queued).

Ps: If you don't know the usage of STL queue container, you can Baidu.

1 #include<queue> // Use queue container 
2 #include <iostream>
 3 #include <cstring>
 4  #define N 100001
 5  using  namespace std;
 6  int bfs( int n, int k) {
 7      queue< int >q ; // Use queue simulation to solve 
8      int step[N]; // The minimum time to go to a certain position 
9      bool vis[N];
 10      memset(vis, false , sizeof (vis));
 11      memset(step, 0, sizeof step);
 12      int x, next;
 13      step[n] = 0 ; // initialize at step 0 at n 
14      vis[n] = true ; // mark visited 
15      q.push(n);
 16      while (! q.empty()) {
 17          x = q.front(); // current position 
18          q.pop();
 19          for ( int i = 0 ; i < 3 ; i++) { // simulate three Case 
20              if (i == 0 ) next = x - 1;
 21              else  if (i == 1 ) next = x + 1 ;
 22              else  if (i == 2 ) next = x * 2 ;
 23              if (next< 0 || next>N) continue ;
 24              if (!vis [next]) { // If you have visited, there is a better solution 
25                  vis[next] = true ;
 26                  q.push(next);
 27                  step[next] = step[x] + 1 ; // Go to next Minimum time at location 
28              }
 29             if (next == k) return step[next]; // minimum time to k 
30          }
 31      }
 32  }
 33  int main() {
 34      int n, k;
 35      cin >> n >> k;
 36      if (n >= k) cout << n - k << endl;
 37      else cout << bfs(n, k) << endl;
 38      return  0 ;
 39 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324980610&siteId=291194637