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

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

POJ 3278 Catch That Cow 【BFS】

2019-11-08 03:26:05
字體:
來源:轉載
供稿:網友

題目鏈接:http://poj.org/PRoblem?id=3278 題意:假設一個人在位置x,那么他下一分鐘能到達的位置是x-1,x+1,2*x,現給出你n,k,讓你求從n到k最少所花費的時間 解析:直接bfs

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;typedef long long ll;const int maxn = 1e6+100;int n,k;int vis[maxn];struct node{ int x,step; node() {} node(int _x,int _step) { x = _x; step = _step; }};int bfs(){ queue<node> q; memset(vis,0,sizeof(vis)); vis[n] = 1; q.push(node(n,0)); while(!q.empty()) { node now = q.front(); q.pop(); if(now.x==k) return now.step; if(!vis[now.x+1]) { q.push(node(now.x+1,now.step+1)); vis[now.x+1] = 1; } if(now.x>0 && !vis[now.x-1]) { q.push(node(now.x-1,now.step+1)); vis[now.x-1] = 1; } if(now.x*2<maxn && !vis[now.x*2]) { q.push(node(now.x*2,now.step+1)); vis[now.x*2] = 1; } } return -1;}int main(){ while(~scanf("%d %d",&n,&k)) printf("%d/n",bfs()); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 哈巴河县| 尼勒克县| 黔江区| 芦山县| 自治县| 武清区| 剑阁县| 漳州市| 抚宁县| 临沭县| 林甸县| 武城县| 家居| 凉城县| 新疆| 鸡西市| 丰宁| 汉阴县| 呈贡县| 抚远县| 久治县| 兰西县| 阜南县| 潞西市| 镇巴县| 佛教| 台湾省| 绥棱县| 阿图什市| 渭南市| 平阴县| 邳州市| 屏边| 平乐县| 铜鼓县| 尖扎县| 古浪县| 洞头县| 山阴县| 尼勒克县| 满洲里市|