POJ 3278 Catch That Cow
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <queue>
- #include <cstdlib>
- #include <cmath>
- #include <cctype>
- #define N 200010
- using namespace std;
- bool maps[N];//用来判断此点约翰有没有走过
- int a, b, step[N];//a, b 为分别为N, K, //step[i]用来保存约翰走到i点时用了几步 (注 : 不要用结构体保存步数, 不然有可能MLE)
- void BFS()
- {
- int q1 = a;
- maps[a] = true;
- queue<int>q;
- q.push(q1);
- while(!q.empty()) {
- int q2 = q.front();
- q.pop();
- if(q2 == b) {//找到之后输出走了几步
- printf("%d\n", step[q2]);
- return ;
- }
- //约翰向走 -1 只有此时他的坐标为正数才会走
- int x = q2 - ;
- if(!maps[x] && q2 > ) {
- maps[x] = true;
- q1 = x;
- step[q1] = step[q2] + ;
- q.push(q1);
- }
- //由于约翰向后走只能 -1 所以只在q2 < b时才向前走
- if(q2 < b){
- x = q2 + ;
- if(!maps[x]) {
- maps[x] = true;
- q1 = x;
- step[q1] = step[q2] + ;
- q.push(q1);
- }
- x = q2 * ;
- if(!maps[x]) {
- maps[x] = true;
- q1 = x;
- step[q1] = step[q2] + ;
- q.push(q1);
- }
- }
- }
- }
- int main()
- {
- while(~scanf("%d %d", &a, &b)){
- memset(maps, , sizeof(maps));
- BFS();
- }
- return ;
- }
POJ 3278 Catch That Cow的更多相关文章
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
随机推荐
- 介绍MFSideMenu左右滑动控件的使用
昨天刚写完侧滑菜单的实例,今天在CocoaChina网站上看到一篇非常好的侧滑菜单设计案例文章,分享给大家.http://www.cocoachina.com/macdev/uiue/2013/071 ...
- 使用日期控件datePicker,阻止移动端的自动调取键盘的事件
方法:简单来说就是阻止input的默认事件. 因为datePicker就是用input来封装的,所以直接阻止input的输入事件就ok: 很简单,把input field属性readonly设置为tr ...
- 复旦高等代数 I(16级)思考题
思考题的说明 一.本学期高代I的思考题面向16级的同学,将不定期地进行更新; 二.欢迎16级的同学通过微信或书面方式提供解答图片或纸质文件给我,优秀的解答可以分享给大家: 三.请大家先独立解答思考题, ...
- Cheatsheet: 2016 04.01 ~ 04.30
.NET String format Setting up Ubuntu for .NET Development ASP.NET Core and Angular2 - Part 1 - Upda ...
- 把数据输出到Word (非插件形式)
项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...
- Linux时间设置及同步
Linux系统安装时选择的UTC时间是国际标准时间,而中国处于UTC+8时区,因此安装系统时不要选择UTC时区. 还有就是Linux有两个时钟: 1.Bios时钟及硬件时间 2.Kernel时钟及系统 ...
- Selenium:元素等待的4种方法
1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用. 2.隐示等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间.默认值是 ...
- Oracle SQL explain/execution Plan
From http://blog.csdn.net/wujiandao/article/details/6621073 1. Four ways to get execution plan(anyti ...
- PageObject小结
写之前想把这次的灵感记录下来: 之前看PO模式几次,始终不得法,感觉一片混乱,可就在一天早上,正在照着别人的代码写自己项目时突然脑海中想通了几个问题:1.为什么要封装页面.2.各个模块的作用以及为什么 ...
- Gvim常用命令
这是一篇较全的vim命令.特记录下来,有稍作修改说明.摘http://hi.baidu.com/ui176/item/b00ae7c0eeaba52847d5c0c5 Vim常用命令 跳到指定的行号: ...