当前位置:网站首页>POJ 3278 catch the cow (width first search, queue implementation)
POJ 3278 catch the cow (width first search, queue implementation)
2022-06-11 11:49:00 【Python ml】
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=1e5+10;
bool visit[MAXN];
struct Status{
int position;
int time;
Status(int p,int t):position(p),time(t){
}
};
int BFS(int n,int k){
queue<Status>myQueue;
myQueue.push(Status(n,0));
while (!myQueue.empty()){
Status current=myQueue.front();
if(current.position==k) return current.time; // Has arrived , It takes time to return
myQueue.pop();
for(int i=0;i<3;++i){
Status next=current;
if(i==0) next.position-=1;
if(i==1) next.position+=1;
if(i==2) next.position*=2;
next.time+=1;
if(next.position<0||next.position>1e5||visit[next.position]) continue; // If you have visited or the location is out of bounds, skip
myQueue.push(next); // Press in the next possible state
visit[next.position]=true; // Mark visited
}
}
}
int main() {
int n,k;
scanf("%d%d",&n,&k);
fill(visit,visit+MAXN,false);
printf("%d\n",BFS(n,k));
system("pause");
return 0;
}
边栏推荐
- golang利用异或^交换两个变量以及加解密
- [go] interpretation of gin source code
- 2020-07 study notes sorting
- Node连接MySql数据库写模糊查询接口
- WordPress landing page customization plug-in recommendation
- Dominating set, independent set, covering set
- C# 读取txt文件生成Word文档
- 刷题笔记(十三)--二叉树:前中后序遍历(复习)
- 文件excel导出
- CVPR 2022 𞓜 text guided entity level image manipulation manitrans
猜你喜欢

苹果MobileOne: 移动端仅需1ms的高性能骨干

C# 设置或验证 PDF中的文本域格式

ELK - X-Pack设置用户密码

Uncaught typeerror: cannot set property 'next' of undefined

Maximum water container

ELK - ElastAlert最大的坑

How to form a good habit? By perseverance? By determination? None of them!

导师转我800块,让我仿真一个电路(电源设计)
![[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)](/img/30/79516390c2b2b50a224eaa84a0c1c9.jpg)
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)

刷题笔记(十四)--二叉树:层序遍历和DFS,BFS
随机推荐
It will be too late if you don't brush the questions. The most complete bat interview questions
[go] interpretation of gin source code
my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题
中文输入法输入事件composition的使用
Hamiltonian graph
刷题笔记(十四)--二叉树:层序遍历和DFS,BFS
WordPress database cache plug-in: DB cache Reloaded
Adapter mode -- can you talk well?
Bark – 自己给自己的 iPhone 发推送提醒 – 最简单的推送提醒服务,开源免费
Qt中radioButton使用
Recommend several gravatar avatar caching plug-ins
WordPress重新生成特色图像插件:Regenerate Thumbnails
JS prototype. The find () method has no effect on the object array. It is urgent...
File excel export
在畢設中學習03
202年最新热门收益较高的年金险产品是什么?
什么是Gerber文件?PCB电路板Gerber文件简介
WordPress用户名修改插件:Username Changer
Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Intl.NumberFormat 设置数字格式