think: 1廣度優先搜索(隊列思想) 2以結構數組為基礎的隊列思想 3反思:自己因為vis數組有的初始化位置不對,導致runtime error.???
hint; 一個農夫的奶牛跑了,他要追上奶牛,奶牛不動,他和奶牛在同一坐標軸上,他可以有3種移動情況,1>他可以向前移動1格2>他可以向后移動1格3>他可以移動目前格數的兩倍,三種移動方式花費的時間相同,求最少時間
poj原題鏈接
Catch That Cow Time Limit: 2000MS Memory Limit: 65536KB
PRoblem Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. * Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute * Teleporting: FJ can move from any point X to the point 2 × X in a single minute. If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input Line 1: Two space-separated integers: N and K
Output Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Example Input 5 17
Example Output 4
Hint poj3278 有鏈接提示的題目請先去鏈接處提交程序,AC后提交到SDUTOJ中,以便查詢存檔。 The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes. Author
以下為accepted代碼
#include <stdio.h>#include <string.h>struct node{ int Data; int size;}link[200000], ans, t;int tp, op, vis[200000];void BFS(int N, int K){ link[tp].Data = N; link[tp].size = 0; tp++; vis[N] = 1; while(op < tp) { ans = link[op++]; if(ans.Data == K) { printf("%d/n", ans.size); return; } if(vis[ans.Data+1] == 0 && ans.Data <= K) { link[tp].Data = ans.Data + 1; link[tp].size = ans.size + 1; //vis[ans.Data] = 1; vis[link[tp].Data] = 1; tp++; } if(vis[ans.Data-1] == 0 && ans.Data > 0) { link[tp].Data = ans.Data - 1; link[tp].size = ans.size + 1; //vis[ans.Data] = 1; vis[link[tp].Data] = 1; tp++; } if(vis[(ans.Data)*2] == 0 && ans.Data <= K) { link[tp].Data = (ans.Data)*2; link[tp].size = ans.size + 1; //vis[ans.Data] = 1; vis[link[tp].Data] = 1; tp++; } }}int main(){ int N, K; while(scanf("%d %d", &N, &K) != EOF) { tp = op = 0; memset(vis, 0, sizeof(vis)); BFS(N, K); } return 0;}/***************************************************User name: Result: AcceptedTake time: 0msTake Memory: 1896KBSubmit time: 2017-02-16 20:23:14****************************************************/以下為runtime error代碼
#include <stdio.h>#include <string.h>struct node{ int Data; int size;}link[200000], ans, t;int tp, op, vis[200000];void BFS(int N, int K){ link[tp].Data = N; link[tp].size = 0; tp++; vis[N] = 1; while(op < tp) { ans = link[op++]; if(ans.Data == K) { printf("%d/n", ans.size); return; } if(vis[ans.Data+1] == 0 && ans.Data <= K) { link[tp].Data = ans.Data + 1; link[tp].size = ans.size + 1; vis[ans.Data] = 1; //vis[link[tp].Data] = 1; tp++; } if(vis[ans.Data-1] == 0 && ans.Data > 0) { link[tp].Data = ans.Data - 1; link[tp].size = ans.size + 1; vis[ans.Data] = 1; //vis[link[tp].Data] = 1; tp++; } if(vis[(ans.Data)*2] == 0 && ans.Data <= K) { link[tp].Data = (ans.Data)*2; link[tp].size = ans.size + 1; vis[ans.Data] = 1; //vis[link[tp].Data] = 1; tp++; } }}int main(){ int N, K; while(scanf("%d %d", &N, &K) != EOF) { tp = op = 0; memset(vis, 0, sizeof(vis)); BFS(N, K); } return 0;}/***************************************************User name: Result: Runtime ErrorTake time: 0msTake Memory: 0KBSubmit time: 2017-02-16 20:22:20****************************************************/新聞熱點
疑難解答