POJ - 3278 Catch That Cow (BFS)
2017-07-24 10:41
357 查看
题目大意:FJ 初始在点 N,奶牛初始在点 K,两点在同一条线上。
FJ 有两种移动方式:
1、x+1 或 x-1
2、2*x
奶牛不动,问最少移动几次 FJ 可以抓住奶牛。
解题思路:三个入口的 BFS,注意一下边界,不控制一下会 RE
FJ 有两种移动方式:
1、x+1 或 x-1
2、2*x
奶牛不动,问最少移动几次 FJ 可以抓住奶牛。
解题思路:三个入口的 BFS,注意一下边界,不控制一下会 RE
#include<iostream> #include<stdio.h> #include<algorithm> #include<cmath> #include<string.h> #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) const int INF = 0x3f3f3f3f; const int NINF = -INF -1; const int MAXN = 100000+10; using namespace std; int N, K, tmp; struct point { int x; int t; point(int a = 0, int b = 0):x(a),t(b){} }; point p[MAXN]; int vis[MAXN]; int bfs(int x) { if (x == K) return 0; p[0] = point(x, 0); int t1 = 0, t2 = 1; while (t1 < t2) { point now = p[t1++]; // printf("%d\n", now.x); for (int i = 0; i < 3; i++) { if (i == 0) tmp = now.x-1; else if (i == 1) tmp = now.x+1; else if (i == 2) tmp = now.x*2; if (tmp < 0 || tmp > MAXN) continue; if (!vis[tmp]) { if (tmp == K) return now.t+1; p[t2++] = point(tmp, now.t+1); vis[tmp] = 1; } } } return -1; } int main() { while (scanf("%d%d", &N, &K) != EOF) { memset(vis, 0, sizeof(vis)); printf("%d\n", bfs(N)); } return 0; }
相关文章推荐
- POJ-3278 Catch That Cow bfs
- POJ_3278 Catch That Cow (bfs)
- POJ 3278 Catch That Cow (BFS)
- catch that cow(简单bFS)抓牛 POJ - 3278
- 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)
- POJ 3278 Catch That Cow (BFS)
- poj 3278 Catch That Cow (bfs搜索)
- POJ 3278 Catch That Cow --- 简单BFS
- poj 3278(hdu 2717) 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
- poj3278 Catch That Cow 一维广搜 bfs入门
- poj 3278 Catch That Cow(bfs)