国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Catch That Cow(BFS)

2019-11-08 02:45:58
字體:
來源:轉載
供稿:網友

Think: BFS +隊列 題目, 農夫有兩種種不同的移動方式

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

題目大意: 1. 數軸上有A  B2點 2. 農夫 有兩種移動方式:walk 每分鐘移動一格             Teleporting 每次移動 2*i個單位 3.求最短時間

#include<bits/stdc++.h> using namespace std; bool v[1000005]; int step[1000005]; void BFS(int n, int k); queue<int>q; int main() { int n, k; while(cin >> n >> k) { while(!q.empty()) q.pop(); memset(v, 0, sizeof(v)); if (n >= k) cout << n - k << endl; else BFS(n, k); } return 0; } void BFS(int n, int k) { int head, next, i; q.push(n); step[n] = 0; while(!q.empty()) { head = q.front(); q.pop(); for (i = 0;i < 3;i ++) { if (i == 0) next = head + 1; if (i == 1) next = head - 1; if (i == 2) next = head * 2; if (next < 0 || next > 100000) continue ; if (v[next] == 0) { q.push(next); step[next] = step[head] + 1; v[next] = 1; } if (next == k) { cout << step[next] << endl; return ; } } } }/***************************************************User name: Result: AcceptedTake time: 0msTake Memory: 468KBSubmit time: 2017-02-18 19:26:43****************************************************/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天祝| 扶沟县| 龙游县| 广德县| 志丹县| 拜城县| 兴安县| 许昌市| 彭泽县| 威远县| 山丹县| 沂水县| 古浪县| 徐汇区| 贡嘎县| 迁安市| 吴桥县| 嘉兴市| 寿阳县| 喀喇沁旗| 林甸县| 息烽县| 新和县| 灯塔市| 固原市| 平阴县| 察隅县| 墨竹工卡县| 武强县| 合肥市| 凌云县| 镇远县| 交口县| 光泽县| 英吉沙县| 县级市| 遂宁市| 开化县| 泰兴市| 陆河县| 沈阳市|