国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博白县| 海丰县| 辛集市| 建始县| 彭阳县| 天全县| 毕节市| 临漳县| 兴山县| 江孜县| 沅江市| 邛崃市| 库尔勒市| 乐清市| 睢宁县| 庄河市| 门头沟区| 洪泽县| 武山县| 孙吴县| 兴安县| 平江县| 霍山县| 桓台县| 肥乡县| 安远县| 郸城县| 太湖县| 奎屯市| 修武县| 聊城市| 四会市| 镇远县| 珠海市| 黄山市| 广水市| 宣汉县| 泊头市| 威信县| 建昌县| 阜康市|