找奶牛

有三种移动方式,作为BFS搜索的方向
利用 当 n > k 时只能向后走,优化结果

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 100005;
int n, k;
bool vis[maxn];
struct node {int n, step;
};int check(node h){if(h.n < 0 || h.n > 100001) return 0;else if(vis[h.n]) return 0;return 1;
}int bfs(){node now, next;queue<node> Q;now.n = n;now.step = 0;Q.push(now);while(!Q.empty()){now = Q.front();Q.pop();if(now.n == k) { return now.step; }next = now;for(int i = 1; i <= 3; i++){if(i == 1) next.n = now.n + 1;else if(i == 2) next.n = now.n - 1;else if(i == 3) next.n = now.n * 2;if(check(next)) {next.step = now.step + 1;vis[next.n] = 1;Q.push(next);}}}return 0;
}int main(){while(scanf("%d%d", &n, &k) == 2){memset(vis, 0, sizeof(vis));if(n > k) {cout << n-k << endl;}else {int ans = bfs();cout << ans << endl;}}return 0;
}

C - Catch That Cow(BFS)相关推荐

  1. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  2. HDU 2717 Catch That Cow(BFS)

    题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...

  3. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...

  4. HDU2717 Catch That Cow(bfs)

    题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...

  5. POJ3278Catch That Cow(BFS)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 70652   Accepted: 22216 ...

  6. HDU-2717-Catch That Cow(bfs)

                                           Catch That Cow Problem Description Farmer John has been infor ...

  7. (BFS)Catch That Cow(poj3278)

    题目: 农夫知道一头牛的位置,想要抓住它.农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) .农夫有两种移动方式 ...

  8. Catch That Cow(POJ-3278)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  9. hdoj2717Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

最新文章

  1. 前端相关html和css
  2. 零基础学怎么学Java_零基础学java难么?怎么自学?
  3. Zencart修改前台页面的字体颜色
  4. wow服务器列表不显示,二区所剩无几 新一轮wow大服务器实装公告
  5. linux配置文件、日志文件全备份
  6. 模态对话框和非模态对话框区别
  7. JS函数中的arguments
  8. 使用JWT的Cloud Native应用程序
  9. 部署WEB应用到云服务器时,出现的部分网页不能显示或者连接数据库问题。
  10. Linux上构建一个RADIUS服务器详解
  11. shell 截取ip地址最后一位_shell 截取IP地址
  12. 基于JAVA+Servlet+JSP+MYSQL的银行账户管理系统
  13. [Linux学习笔记] Linux常用命令 - 文件搜索命令
  14. 细谈最近上线的Vue2.0项目(一)
  15. numpy将所有数据变为0和1_PyTorch 学习笔记(二):张量、变量、数据集的读取、模组、优化、模型的保存和加载...
  16. 获取Android手机短信中心号码
  17. python商业爬虫学徒计划_(教程)下载:麻瓜编程Python商业爬虫学徒计划麻瓜编程的视频python办公自动化麻瓜...
  18. html毕业答辩ppt,毕业论文答辩ppt(要求和制作技巧)
  19. 神器-数学公式识别工具-mathpix
  20. 发送消息(SendMessage)常识普及

热门文章

  1. 计算机视觉专家:如何从C++转Python
  2. oracle 查询本周数据生成下周数据
  3. 【Java面试题】37 说出ArrayList,Vector, LinkedList的存储性能和特性
  4. Linux下同步模式、异步模式、阻塞调用、非阻塞调用总结
  5. VS 常见快捷键(转)
  6. IE下的li有空白问题
  7. mysql 控制函数库_数据库开发——MySQL——函数与流程控制
  8. android自定义文件选择,关于安卓自定义本地文件选择库的实现
  9. ucint核心边缘分析_5G、云计算、物联网与边缘计算的相辅相承
  10. Linux 的无障碍设置如何操作?