POJ-3278 catch that cow 广度优先搜索
来源:互联网 发布:java怎么定义函数 编辑:程序博客网 时间:2024/05/18 18:15
以牛的位置为起始点向前搜索,注意状态更新就好了
不要问我为什么是从后往前搜...我也不知道,我第一遍从前往后搜的wa了 然后换从后往前搜就AC了...
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <queue>#include <stack>using namespace std;int dis[100005];int bfs(int n){ for(int i=0;i<=100000;i++){ dis[i]=99999999; } queue<int>q; q.push(n); dis[n]=0; while(!q.empty()){ int t=q.front(); q.pop(); if(dis[t-1]>dis[t]+1&&t>=1){ dis[t-1]=dis[t]+1; q.push(t-1); } if(dis[t+1]>dis[t]+1&&t<=100000){ dis[t+1]=dis[t]+1; q.push(t+1); } if(t%2==0) if(dis[t/2]>dis[t]+1&&t>=0){ dis[t/2]=dis[t]+1; q.push(t/2); } }}int main(){ int N,K; while(cin>>N>>K){ bfs(K); cout<<dis[N]<<endl; } return 0;}
阅读全文
0 0
- POJ 3278 Catch That Cow(BFS广度优先搜索)
- POJ 3278-Catch That Cow 广度优先搜索BFS
- poj 3278 Catch That Cow 广度优先搜索
- Catch That Cow POJ 3278 BFS 广度优先搜索
- POJ-3278 catch that cow 广度优先搜索
- POJ-3278 Catch That Cow【广度优先搜索】
- poj3287 Catch That Cow(广度优先搜索)
- Catch That Cow-----广度优先搜索
- POJ3278 - Catch That Cow - 广度优先搜索
- Catch That Cow(广度优先搜索_bfs)
- Catch That Cow-广度优先搜索-POJ3278
- Catch That Cow广度优先搜索
- poj 3278 Catch That Cow 广度优先搜索 + 回溯 队列 剪枝
- poj 3278 catch that cow (广度优先BFS)
- poj 3278 Catch That Cow 广度优先算法
- POJ3278 Catch That Cow 广度优先搜索版
- poj3278 hdu2717 Catch That Cow 广度优先搜索
- (广度优先搜索)算法题-poj3278 Catch That Cow
- 复制文字到剪切板的方法
- 创建固定长度列表
- Zbar二维码扫描
- C++中的static关键字的总结
- react-native热更新之codepush使用
- POJ-3278 catch that cow 广度优先搜索
- 运算符优先级以及特例
- stringByEvaluatingJavaScriptFromString实现iOS 和jsp交互
- "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
- Linux下SOAP工具生成ONVIF框架
- 给QDockWidget添加边框
- si4438 的初始化
- JavaScript和HTML事件处理程序属性中的this的含义
- 阿里云部署redis服务器,以及远程访问