poj3278 Catch That Cow |bfs|队列|Java
2018-03-21 20:52
369 查看
http://poj.org/problem?id=3278
import java.sql.Time; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main{ public static int k, n; public static int MAXN = 100000; public static int[] vis = new int[100005]; public static class P { int p; int time; P(int x, int y) { p = x; time = y; } } public static void bfs() { return; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { Arrays.fill(vis, 0); n = scanner.nextInt(); k = scanner.nextInt(); P p = new P(n, 0); Queue<P> q = new LinkedList<P>(); q.offer(p); vis = 1; while (!q.isEmpty()) { P t = q.poll(); if (t.p == k) { System.out.println(t.time); break; } else { if (t.p - 1 >= 0 && vis[t.p - 1] != 1) { q.offer(new P(t.p - 1, t.time + 1)); vis[t.p - 1] = 1; } if (t.p + 1 <= MAXN && vis[t.p + 1] != 1) { q.offer(new P(t.p + 1, t.time + 1)); vis[t.p + 1] = 1; } if (t.p * 2 <= MAXN && vis[t.p*2] != 1) { q.offer(new P(t.p * 2, t.time + 1)); vis[t.p * 2] = 1; } //System.out.print(t.p*2+"="); //System.out.println(t.time); } } } return; } }
相关文章推荐
- poj3278 Catch That Cow (BFS算法和队列)
- poj3278 Catch That Cow(bfs java)
- 广度优先搜索bfs与抓住那头奶牛(Catch that cow, poj3278)
- [bfs] poj3278 Catch that Cow
- poj3278 Catch That Cow bfs
- POJ3278(BFS广度优先搜索)之Catch That Cow
- 【poj3278】Catch that cow——bfs
- POJ3278 Catch That Cow(BFS)
- POJ3278 Catch That Cow(BFS)
- POJ3278 HDU2717 Catch That Cow【BFS】
- poj3278--Catch That Cow(BFS+裁剪记录)
- poj3278 Catch That Cow(简单的一维bfs)
- poj3278 Catch that cow 代码比很多人精简。。。BFS
- POJ3278 Catch That Cow —— BFS
- 【POJ】3278 - Catch That Cow(bfs,队列)
- POJ3278 Catch That Cow(广搜BFS)
- POJ3278 Catch That Cow(广搜BFS)
- POJ3278 Catch That Cow(BFS)
- Poj3278 BFS Catch That Cow
- POJ3278 Catch That Cow(BFS入门)