POJ 3287 Catch That Cow(bfs)
看懂意思就很简单了,给你一个数,每次只能加一或者减一或者乘2,问最少几次才能变成另一个数,就是一个简单的bfs过程,看代码吧。
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 100005;
struct Node{int x,step;
}Now,Next,S,E;
bool vis[MAXN]; // 用于标记这个数是否出现过int bfs(){queue<Node> q;memset(vis,0,sizeof(vis));S.step = 0;q.push(S);while(!q.empty()){Now = q.front();q.pop();if(Now.x == E.x){ // 结束的时候返回步数return Now.step;}for(int i=0;i<3;i++){ // 模拟变换过程if(i==0){Next.x = Now.x + 1;}else if(i==1){Next.x = Now.x - 1;}else{Next.x = Now.x * 2;}if(Next.x >= 0 && Next.x <= 100000 && vis[Next.x] == 0){vis[Next.x] = 1;Next.step = Now.step + 1;q.push(Next);}}}return -1;
}int main()
{while(~scanf("%d%d",&S.x,&E.x)){int temp = bfs();printf("%d\n",temp);}return 0;
}
/***[来源] POJ 3278[题目] Catch That Cow[大意]给你两个数n,m,n每次只能加一或者减一或者乘以2,问最少需要多少次才能变成m[输入]5 17[输出]4
*/
POJ 3287 Catch That Cow(bfs)相关推荐
- POJ 3278 Catch That Cow BFS
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32071 Accepted: 9866 D ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- BFS POJ 3278 Catch That Cow
题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30924 Accepted: 9536 D ...
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- bfs+dfs分析----poj 3278 Catch That Cow
题目详情 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 115430 Accepted: ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35043 Accepted: 10800 ...
- HDU2717 Catch That Cow ( BFS )
Catch That Cow Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- catch that cow(bfs)
catch that cow 题目描述 花花经过长时间的研究,终于研发出了能够跃迁的宇宙飞船.现在,他想要前往致远星.假设地球和致远星都在一个坐标轴上,其中地球位于坐标n,而致远星位于坐标k.而花花的 ...
最新文章
- C语言-数据数据类型、变量与常量
- 直接定址表---汇编学习笔记
- Ubuntu系统如何安装软件
- 【数据结构与算法】之深入解析“按要求补齐数组”的求解思路与算法示例
- Django中间件2
- 安卓百度地图附近poi搜索以及到指定poi的换乘方案
- CSS3 Transform、Transition和Animation属性总结
- IDEA——修改idea64.exe.vmoptions文件解决coding卡顿问题
- 容器编排技术 -- Kubernetes Replication Controller
- solid测序列原理_SOLID原理简介
- 分布式系统中的领导选举
- java智能提示_【Java】智能提示的设置
- Mybatis的两种分页方式:RowBounds和PageHelper
- 使用CrossOver安装第三方软件
- 关于Polar SI9000重装/启动后卡在Frequency Dependent Calculation界面不能切换。
- Android 程序清单详解
- 【html】表格table与表单form
- php表格中的caption,HTML中的caption属性是什么意思?caption标签在HTML中的用法(附实例)...
- 在WIN10中配置conda
- 质量团队的组建与升级