POJ 3278 Catch That Cow --- 简单BFS
2015-12-19 02:00
495 查看
/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, sizeof visit); queue<int> q; //对中存储已访问但下一个点待判断的结点 q.push(n); step = 0; visit = 1; int x,next; while (!q.empty()){ x = q.front(); q.pop(); for (int i = 0; i < 3; ++i){ //i = 0,1,2表示分别按不同的方向广搜 if (i == 0) next = x - 1; else if (i == 1) next = x + 1; else next = x * 2; //是否越界和是否已访问的判断 if (visit[next] || next < 0 ||next >= maxn) continue; visit[next] = 1; step[next] = step[x] + 1; //步数+1 if (next == k) return step[next]; q.push(next); } //q.pop(); } return -1; } int main() { int n, k; while (scanf("%d%d", &n, &k) == 2){ printf("%d\n", bfs(n, k)); } return 0; }
View Code
相关文章推荐
- 【MFC】 Unicode下CString和char *之间的互相转换
- 普法知识(42):什么是“法治政府”?
- Unity5.3Json测试学习
- Linux SWAP 交换分区配置说明
- Linux SWAP 交换分区配置说明
- Linux SWAP 交换分区配置说明
- [hdu1693 Eat the Trees]插头dp
- memcpy的用法
- python之类--socket
- angular 双向绑定的实现原理
- Linux中的SWAP交换分区
- Linux中的SWAP交换分区
- Linux中的SWAP交换分区
- 各种编程语言做逆向工程UML图的软件
- 2015GPNU新生赛题解
- Leetcode: Implement Stack using Queues
- BugPhobia开发篇章:Beta阶段第IV次Scrum Meeting
- BugPhobia开发篇章:Beta阶段第IV次Scrum Meeting
- Storyboard应用:快速创建及设置View
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)解题报告