Catch That Cow
来源:互联网 发布:云计算系统 编辑:程序博客网 时间:2024/06/05 20:32
题意:从n点走到k点,有三种走法,可以走x-1或x+1或2*x,要求走的步数最少,输出步数
解题思路:专题——简单搜索.bfs求最短路,很基础的bfs,只需要注意一下边界问题就可以了
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <vector>#include <queue>using namespace std;#define INF 0x3f3f3f3fconst int maxn=100005;int dir[]={1,-1,0};int st,ed;int dist[maxn];bool inside(int x){ return x>=0&&x<maxn;}int bfs(int s){ for(int i=0;i<maxn;i++) { dist[i]=INF; } queue<int> que; dist[st]=0; que.push(st); while(!que.empty()) { int p=que.front();que.pop(); if(p==ed)break; for(int i=0;i<3;i++) { int xx; if(dir[i]) { xx=p+dir[i]; if(inside(xx)&&dist[xx]==INF) { dist[xx]=dist[p]+1; que.push(xx); } } else { xx=p*2; if(inside(xx)&&dist[xx]==INF) { dist[xx]=dist[p]+1; que.push(xx); } } } } return dist[ed];}int main(){ while(cin>>st>>ed) { int ans=bfs(st); cout<<ans<<endl; } return 0;}/*5 17*/
阅读全文
0 0
- POJ3278 Catch That Cow
- Catch That Cow
- Catch That Cow
- poj3278 Catch That Cow
- 3278. Catch That Cow
- 【HDU2717】-Catch that cow
- Catch That Cow
- F - Catch That Cow
- poj3278 - Catch That Cow
- HDOJ Catch That Cow
- 2717Catch That Cow
- POJ3278 Catch That Cow
- 3278Catch That Cow
- Catch That Cow(bfs)
- Catch that cow (H2717)
- POJ3278--Catch That Cow
- Catch That Cow
- Catch That Cow+BFS
- Django中modelform的使用
- IoC容器
- 深入理解React 高阶组件
- Linux命令总结
- (学习java)链队列的实现
- Catch That Cow
- Divide and Conquer:169. Majority Element
- logback日志组件使用案例
- 排序算法总结
- TCP/IP <1>
- 【安全牛学习笔记】基于PHP的SQL注入漏洞原理及解决办法
- 第十三单元笔记总结
- ResNet学习
- 【网络爬虫】-WP0001-Anaconda_Python2_Python3_conda_Pip_pycharm环境配置