BZOJ 1646: [Usaco2007 Open]Catch That Cow
生活随笔
收集整理的這篇文章主要介紹了
BZOJ 1646: [Usaco2007 Open]Catch That Cow
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://www.lydsy.com/JudgeOnline/problem.php?id=1646
這道題我剛開始就直接硬生生地廣搜,然后妥妥地TLE了,后來乖乖地加了一些剪枝。。。
具體程序里看吧,反正就是典型的廣搜,但是有一些細節
程序:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; queue< pair<int,int> >q; int n,k; bool f[2000000]; bool check(int x) {if ((x<0)||(x>100000)) return false; //檢查這個點是不是已經超范圍了if (f[x]) return false; //哈這個很重要,如果已經如果隊列了,就沒必要再進一遍return true; } int main() {scanf("%d%d",&n,&k);q.push(make_pair(n,0));while (!q.empty()){pair<int,int> now=q.front(); q.pop();if (now.first==k){printf("%d",now.second);return 0;}if (now.first<k){if (check(now.first*2)) {q.push(make_pair(now.first*2,now.second+1));f[now.first*2]=true;}if (check(now.first+1)) {q.push(make_pair(now.first+1,now.second+1));f[now.first+1]=true;}if (check(now.first-1)){q.push(make_pair(now.first-1,now.second+1));f[now.first-1]=true;}} //如果這個點已經超過目標點了,它只能一步步往回挪了if ((now.first>k)&&(check(now.first-1))) {q.push(make_pair(now.first-1,now.second+1));f[now.first-1]=true;}}return 0; }?
轉載于:https://www.cnblogs.com/2014nhc/p/6660451.html
總結
以上是生活随笔為你收集整理的BZOJ 1646: [Usaco2007 Open]Catch That Cow的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mongodb 怎样检测 安装成功 以
- 下一篇: 电子游戏市场的演变————零售