BFS | 3278 | Catch That Cow
2013-05-21 13:50
218 查看
经典题
3278 Catch That Cow
3278 Catch That Cow
#include <cstdio> #include <iostream> #include <cstring> #include <queue> using namespace std; const int M = 200005; struct Node { int a; int step; Node(int _a, int _step) : a(_a), step(_step) {} }; int n, k; queue<struct Node> que; bool visited[M]; int getPlace(int i, int x) { if(i == 1) return x - 1; if(i == 2) return x + 1; if(i == 3) return x << 1; } bool check(int x) { if(x >= 0 && x <= 200000) return true; return false; } int bfs() { while(!que.empty()) que.pop(); memset(visited, false, sizeof(visited)); que.push(Node(n, 1)); visited = true; while (!que.empty()) { struct Node tmp = que.front(); que.pop(); for (int i = 1; i <= 3; i++) { int x = getPlace(i, tmp.a); if (check(x) && !visited[x]) { if (x == k) return tmp.step; else { que.push(Node(x, tmp.step + 1)); visited[x] = true; } } } } } int main() { while (scanf("%d%d", &n, &k) != EOF) { if (n >= k) printf("%d\n", n - k); else printf("%d\n", 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[基础BFS]
- Catch That Cow POJ - 3278 图论模型 BFS
- POJ 3278 Catch That Cow (Java,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 (BFS)
- Catch That Cow POJ - 3278 [bfs][最短路]
- POJ-3278-Catch That Cow(BFS)
- BFS —— POJ 3278 Catch That Cow
- poj 3278 Catch That Cow (bfs搜索)
- poj 3278 Catch That Cow(bfs广搜)
- Pku acm 3278 Catch That Cow数据结构题目解题报告(五) ----树的BFS