Catch That Cow【廣搜】

Catch That Cow

?POJ - 3278?

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point?N?(0 ≤?N?≤ 100,000) on a number line and the cow is at a point?K?(0 ≤?K?≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point?X?to the points?X?- 1 or?X?+ 1 in a single minute
* Teleporting: FJ can move from any point?X?to the point 2 ×?X?in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers:?N?and?K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

?

依舊是廣搜的模板題

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
/*priority_queue<int,vector<int> ,greater<int> >q;*/
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
bool vis[maxn];
int step[maxn];
int bfs(int s,int e)
{queue<int> q;q.push(s);step[s]=0;vis[s]=true;while(!q.empty()){int front=q.front();q.pop();if(front+1>0&&front+1<=1e5&&vis[front+1]==false){q.push(front+1);vis[front+1]=true;step[front+1]=step[front]+1;if(front+1==e)return step[front+1];}if(front-1>0&&front-1<=1e5&&vis[front-1]==false){q.push(front-1);vis[front-1]=true;step[front-1]=step[front]+1;if(front-1==e)return step[front-1];}if(front*2>0&&front*2<=1e5&&vis[front*2]==false){q.push(front*2);vis[front*2]=true;step[front*2]=step[front]+1;if(front*2==e)return step[front*2];}}
}
int main()?
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int N,K;cin>>N>>K;ms(vis);ms(step);if(N>=K)cout<<N-K<<endl;elsecout<<bfs(N,K)<<endl;return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536401.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536401.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536401.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Go2Shell 已無法使用

在更新 Mac 系統時提醒了這個, 像我一樣對 Go2Shell 中毒的人來說, 這是無法忍受的。貌似 Go2Shell 沒有升級&#xff0c;沒有辦法&#xff0c;就直接找來了一個替代品。cd to, 下載入口如下&#xff1a;目前感覺良好。 轉載于:https://juejin.im/post/5cfe82e15188252b1b0366e…

Fliptile【搜索】

Fliptile POJ - 3279 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which…

JS異步開發總結

1 前言 眾所周知&#xff0c;JS語言是單線程的。在實際開發過程中都會面臨一個問題&#xff0c;就是同步操作會阻塞整個頁面乃至整個瀏覽器的運行&#xff0c;只有在同步操作完成之后才能繼續進行其他處理&#xff0c;這種同步等待的用戶體驗極差。所以JS中引入了異步編程&…

迷宮問題【廣搜】

迷宮問題 POJ - 3984 定義一個二維數組&#xff1a; int maze[5][5] {0, 1, 0, 0, 0,0, 1, 0, 1, 0,0, 0, 0, 0, 0,0, 1, 1, 1, 0,0, 0, 0, 1, 0,}; 它表示一個迷宮&#xff0c;其中的1表示墻壁&#xff0c;0表示可以走的路&#xff0c;只能橫著走或豎著走&#xff0c;不能…

大蝦對51單片機入門的經驗總結

回想起當初學習AT89S52的日子還近在眼前:畢業后的第一年呆在親戚公司做了10個月設備管理.乏味的工作和繁雜的瑣事讓我郁悶不已.思考很久后終于辭職.投奔我的同學去了,開始并不曾想到要進入工控行業,知識想找一份電子類技術職業,至于什么職業我根本沒有目標可言.經過兩個多月的挫…

mac安裝cnpm

1.先安裝node node的下載地址&#xff1a;http://nodejs.cn/download/ 這個沒什么好說的&#xff0c;安裝完成后測試一下&#xff0c;在終端輸入&#xff1a;node -v 這時候就可以看到安裝的node版本號&#xff0c;再輸入&#xff1a;npm -v 這時候就會看到npm的版本號了 2.用n…

A計劃【廣搜】

A計劃 HDU - 2102 可憐的公主在一次次被魔王擄走一次次被騎士們救回來之后&#xff0c;而今&#xff0c;不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在T時刻吃掉公主&#xff0c;因為他聽信謠言說吃公主的肉也能長生不老。年邁的國王正是心急如焚&#xff0c;告招天…

WordPress忘記密碼的5種解決方法

為什么80%的碼農都做不了架構師&#xff1f;>>> 無意中忘記wordpress的密碼了&#xff0c;恰巧在后臺又沒來得及設置郵件&#xff0c;只好四處苦尋解決辦法&#xff0c;還好總算找到了…… 1. WordPress內置的找加密碼方法 如果你的admin帳戶的電子郵件地址是正確的…

記錄一次,事務遇到消息發送,疏忽給自己挖坑

場景&#xff1a;一個異步重算功能&#xff08;任務新建后發送消息到RocketMq&#xff09;&#xff0c;每次重算單條記錄的時候&#xff0c;可以計算正確&#xff0c;但是當多條記錄批量重算時&#xff0c;結果總是莫名其妙的不對。排查了很久&#xff0c;終于找到原因 原因&am…

在linux上執行.net Console apps

為什么80%的碼農都做不了架構師&#xff1f;>>> 有個程序&#xff0c;在.net下寫了半天&#xff0c;總算跑起來了&#xff0c;發現有個問題&#xff0c;在windows上不好弄&#xff0c;而同事前一段時間已經有Linux下的解決方法了&#xff0c;于是想直接將.net程序放…

Android4.0設置界面修改總結

為什么80%的碼農都做不了架構師&#xff1f;>>> 筆者前段時間完成設置的圓角item風格的修改&#xff0c;但最近&#xff0c;客戶新增需求&#xff0c;想把設置做成Tab風格的&#xff0c;沒辦法&#xff0c;顧客就是上帝&#xff0c;咱得改啊。今天算是初步改完了&a…

敵兵布陣【線段樹】

敵兵布陣 HDU - 1166 C國的死對頭A國這段時間正在進行軍事演習&#xff0c;所以C國間諜頭子Derek和他手下Tidy又開始忙乎了。A國在海岸線沿直線布置了N個工兵營地,Derek和Tidy的任務就是要監視這些工兵營地的活動情況。由于采取了某種先進的監測手段&#xff0c;所以每個工兵…

Android之仿網易V3.5新特性

為什么80%的碼農都做不了架構師&#xff1f;>>> 最近&#xff0c;網易新聞更新到V3.5了&#xff0c;給我印象最深的是第一次進應用時顯示新特性的ViewPager變成垂直滑動了。于是&#xff0c;小小的模仿了一下&#xff0c;我們來看看效果&#xff1a; 本文源碼下載地…

Android_內存泄露

2019獨角獸企業重金招聘Python工程師標準>>> 1.資源對象沒關閉造成的內存泄漏 描述&#xff1a; 資源性對象比如&#xff08;Cursor&#xff0c;File文件等&#xff09;往往都用了一些緩沖&#xff0c;我們在不使用的時候&#xff0c;應該及時關閉它們&#xff0c;以…

CYQ.Data 輕量數據層之路 使用篇三曲 MAction 取值賦值(十四)

2019獨角獸企業重金招聘Python工程師標準>>> 上一篇&#xff1a;CYQ.Data 輕量數據層之路 使用篇二曲 MAction 數據查詢(十三&#xff09; 內容概要 本篇繼續上一篇內容&#xff0c;本節介紹所有取值與賦值的相關操作。1&#xff1a;原生&#xff1a;像操作Row一樣…

CYQ.Data 數據框架 發放V1.5版本源碼

2019獨角獸企業重金招聘Python工程師標準>>> 本篇的內容很簡單&#xff0c;就發放V1.5版本源碼&#xff0c;同時補充了所有發布版本的API文檔。 具體相關下載地址見&#xff1a; 秋色園下載中心&#xff1a;http://www.cyqdata.com/download/article-detail-426 如何…

New Bus Route

New Bus Route CodeForces - 792A There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1,?a2,?...,?an. All coordinates are pairwise distinct. It is possible to get from one city to …

愛說說技術原理:與TXT交互及MDataTable對Json的功能擴展(二)

2019獨角獸企業重金招聘Python工程師標準>>> 關于愛說說在技術選型的文章見&#xff1a;"愛說說"技術原理方案的定選思考過程 本篇將講述“愛說說”比較重大的技術問題點及解決手段&#xff1a; 愛說說&#xff1a;http://speak.cyqdata.com/ 雜說幾句&am…

ActiveXObject 安裝

將后綴名為ocx的文件拷貝至目錄 c:\Windows\SysWOW64\。執行如下命令&#xff0c;進行注冊&#xff1a;regsvr32 c:\Windows\SysWOW64\x.ocx轉載于:https://www.cnblogs.com/Currention/p/11024354.html

如何制作VSPackage的安裝程序

2019獨角獸企業重金招聘Python工程師標準>>> 第一步&#xff0c;生成一個REG文件&#xff1a; 收錢進入目錄: C:\Program Files\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Tools\Bin 這是SDK的目錄&#xff0c;使用regpkg.exe 命令 命令格式為: …