Catch That Cow(HDU 2717)
来源:互联网 发布:软件需求 功能需求 编辑:程序博客网 时间:2024/06/11 06:01
Catch That Cow
题目就不摘了-。-
题目链接
这道题一般用广搜写,可以用循环队列或队列模板来解决数组开得不够大的问题。
测试时试试 0 10000这个数据,其结果为22,一定要保证 0 10000 不会栈溢出,否则出现running time error
本题关键是在入队之前就进行判断,这样就会减少入队的次数。
1,循环队列(c,c++)
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<string.h>#include<stdlib.h>using namespace std;int main(){ int n, k; struct node { int s; int num; }que[200010];//队列 int book[200010];//用来标记 while (scanf("%d%d", &n, &k) == 2) { if (n >= k) printf("%d\n", n - k);//负的直接输出 else { int min = k - n; memset(book, 0, sizeof(book)); memset(que, 0, sizeof(que)); int head, tail; head = 1; tail = 1; que[tail].num = n; que[tail].s = 0; tail++; while (head < tail) { book[que[head].num] = 1; que[tail].num = que[head].num + 1; if (que[tail].num == k) break; if (book[que[tail].num] == 0 && que[tail].num >= 0 && que[tail].num <= 100000)//入队判断条件有三个 { book[que[tail].num] = 1; que[tail++].s = que[head].s + 1; } if (tail >= 200000) tail = 1; //接下来两个方向相当于复制-粘贴 que[tail].num = que[head].num * 2; if (que[tail].num == k) break; if (book[que[tail].num] == 0 && que[tail].num >= 0 && que[tail].num <= 100000) { book[que[tail].num] = 1; que[tail++].s = que[head].s + 1; } if (tail >= 200000) tail = 1; que[tail].s = que[head].s + 1; que[tail].num = que[head].num - 1; if (que[tail].num == k) break; if (book[que[tail].num] == 0 && que[tail].num >= 0 && que[tail].num <= 100000) { book[que[tail].num] = 1; que[tail++].s = que[head].s + 1; } if (tail >= 200000) tail = 1; head++; if (head >= 200000) head = 1; } printf("%d\n", que[head].s+1); } } return 0;}
2,队列模板(c++)
#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;int cow[100010];int n, k;void bfs(){ cow[n] = 0; queue<int> Q; int x, y; Q.push(n);//入队 while (!Q.empty())//head == tail ?? { x = Q.front();//返回队首 Q.pop();//出队 if (x == k) return; y = x + 1; if (y >= 0 && y <= 100000 && cow[y] == 0) { cow[y] = cow[x] + 1; Q.push(y); } y = x - 1; if (y >= 0 && y <= 100000 && cow[y] == 0) { cow[y] = cow[x] + 1; Q.push(y); } y = x * 2; if (y >= 0 && y <= 100000 && cow[y] == 0) { cow[y] = cow[x] + 1; Q.push(y); } }}int main(){ while (cin >> n >> k) { memset(cow, 0, sizeof(cow)); bfs(); cout << cow[k] << endl; } return 0;}
0 0
- hdu 2717 Catch That Cow(搜索)
- hdu(2717)Catch That Cow
- hdu 2717 Catch That Cow(bfs)
- hdu 2717 Catch That Cow(搜索)
- hdu 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow (BFS)
- 【HDU 2717】Catch That Cow(BFS)
- 【HDU】-2717-Catch That Cow(BFS)
- Catch That Cow(HDU 2717)
- HDU 2717 Catch That Cow(bfs)
- HDU 2717: Catch that cow (BSF)
- HDU-2717 Catch That Cow(搜索)
- hdu 2717 Catch That Cow
- HDU-2717-Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717Catch That Cow
- hdu 2717 Catch That Cow
- 摘自我鲁哥的博客
- asp.net mvc 菜鸟常犯错误总结
- 函数指针和回调函数
- JNDI 之web项目数据源配置
- 关于OKHttp的一些理解
- Catch That Cow(HDU 2717)
- Java IO字节输出流常见类进行分析(三)
- Android 5.0以下XML定义的drawable不识别?attr/属性的解决思路
- A very hard Aoshu problem HDU-4403
- TCP在握手、挥手或连接已建立时,如果发生意外会怎样?
- W3C 不要再使用废弃的 HTML 标签
- Python HackerRank|The Minion Game
- html5+js拖拽上传
- 浅谈什么是MVC设计模式