POJ 3278 Catch That Cow(BFS广度优先搜索)
2010-02-20 16:00
330 查看
//BFS广度优先搜索,结合数据结构队列 #include<iostream> #include<queue> using namespace std; int arr[200000]; int vis[200000];//已访问数组 queue<int> q1,q2;//用数据结构队列可节省搜索空间 int deep; void set(int n,int deep,int k) { if(arr[n-1] != deep-1 && n - 1 > 0 && !vis[n-1])//控制搜索范围,剪枝 { arr[n-1] = deep; q2.push(n-1); } if(arr[n+1] != deep-1 && !vis[n+1]) { arr[n+1] = deep; q2.push(n+1); } if(arr[2*n] != deep-1 && n < k && !vis[2*n])//控制搜索范围 { arr[2*n] = deep; q2.push(2*n); } } int bfs(int n,int k)//广度优先搜索——针对树的最浅深度 { arr = 1; q1.push(n); if(n >= k)//当农夫位置在牛的位置的后面时,直接相减得出结果 return n - k; else { while(arr[k] == 0) { while(!q1.empty()) { if(!vis[q1.front()]) { set(q1.front(),deep,k); vis[q1.front()] = 1; q1.pop(); } else q1.pop(); } while(!q2.empty())//将队列复制过去 { q1.push(q2.front()); q2.pop(); } ++deep; } return arr[k]-1; } } int main() { int n,k; cin >> n >> k; deep = 2; memset(arr,0,sizeof(arr)); memset(vis,0,sizeof(vis)); cout << bfs(n,k) << endl; return 0; }
相关文章推荐
- 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 BFS(不多说话) Catch That Cow
- POJ 3278 && HDU 2717 Catch That Cow(bfs)
- POJ-3278-Catch That Cow-广搜(BFS)
- poj 3278 Catch That Cow(bfs)
- Problem C 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(bfs实现)
- POJ_3278 Catch That Cow (bfs)
- catch that cow(简单bFS)抓牛 POJ - 3278
- poj 3278 Catch That Cow (bfs搜索)