【POJ】3278-Catch That Cow 简单BFS
2018-02-27 21:32
387 查看
http://poj.org/problem?id=3278
一个数轴,点初始在N(0 ≤ N ≤ 100,000),有两种移动方式
1.当前在X,下一秒在X+1或X-1
2.当前在X,下一秒在2*X
最短多久到达K?
不用复杂话,共有3种移动方式,全部进行一次入队就好了。
一个数轴,点初始在N(0 ≤ N ≤ 100,000),有两种移动方式
1.当前在X,下一秒在X+1或X-1
2.当前在X,下一秒在2*X
最短多久到达K?
不用复杂话,共有3种移动方式,全部进行一次入队就好了。
#include <iostream> #include <queue> #include <cstring> using namespace std; const int maxn=1000001; int n,k; struct Node{ int dis; int step; }; bool vis[maxn]; int main(){ cin >> n >> k; memset(vis,false,sizeof(vis)); queue <Node> q; Node Now; Now.dis=n; Now.step=0; vis[Now.dis]=true; q.push(Now); while (!q.empty()){ Now=q.front(); if (Now.dis==k){ cout << Now.step << endl; return 0; } q.pop(); Node tmp; tmp.dis=Now.dis-1; tmp.step=Now.step+1; if (tmp.dis>=0&&tmp.dis<=100000&&!vis[tmp.dis]){ vis[tmp.dis]=true; q.push(tmp); } tmp.dis=Now.dis+1; tmp.step=Now.step+1; if (tmp.dis>=0&&tmp.dis<=100000&&!vis[tmp.dis]){ vis[tmp.dis]=true; q.push(tmp); } tmp.dis=Now.dis*2; tmp.step=Now.step+1; if (tmp.dis>=0&&tmp.dis<=100000&&!vis[tmp.dis]){ vis[tmp.dis]=true; q.push(tmp); } } }
相关文章推荐
- POJ 3278 Catch That Cow (简单BFS)
- catch that cow(简单bFS)抓牛 POJ - 3278
- POJ 3278 Catch That Cow --- 简单BFS
- !POJ 3278 Catch That Cow--BFS(隐蔽的BFS)
- POJ 3278 Catch That Cow 【BFS】
- poj 3278 Catch That Cow(bfs)
- POJ - 3278 Catch That Cow (BFS)
- poj 3278 Catch That Cow bfs
- POJ 3278 Catch That Cow(BFS广度优先搜索)
- poj 3278:Catch That Cow(简单一维广搜)
- poj 3278 Catch That Cow (bfs搜索)
- POJ 3278 Catch That Cow (BFS)
- POJ - 3278 Catch That Cow ——BFS
- poj 3278 catch that cow (广度优先BFS)
- POJ-3278 Catch That Cow bfs
- POJ 3278 Catch That Cow(bfs)
- POJ 3278 Catch That Cow (Java,bfs)
- poj 3278 Catch That Cow(经典bfs)
- poj 3278 Catch That Cow(BFS)
- poj3278 Catch That Cow(BFS)