hdu 2717 Catch That Cow
2015-03-30 23:08
267 查看
暴力搜索(广搜),注意要先判断再加入队列,否则会超内存
#include<iostream> #include<queue> #include<cmath> #include<cstring> #define maxn 100000+5 using namespace std; int dir[2]={1,-1}; int vis[maxn]={0}; int n,m,re; int maxx; struct stu { int x,s; }; void bfs(int n) { memset(vis,0,sizeof(vis)); stu xx,yy; xx.x=n;xx.s=0; queue<stu>root; root.push(xx); while(root.size()) { xx=root.front(); root.pop(); if(xx.x==m) {re=xx.s;return;} for(int i=0;i<2;i++) { yy.x=xx.x+dir[i]; yy.s=xx.s+1; if(yy.x>=0&&yy.x<=maxn) { if(!vis[yy.x]) vis[yy.x]=1,root.push(yy); } } yy.x=xx.x*2; yy.s=xx.s+1; if(yy.x>=0&&yy.x<=maxn) { if(!vis[yy.x]) { vis[yy.x]=1; root.push(yy); } } } } int main() { while(cin>>n>>m) { bfs(n); cout<<re<<endl; } return 0; }
相关文章推荐
- HDU-2717-Catch That Cow
- poj 3278(hdu 2717) Catch That Cow(bfs)
- HDU 2717 Catch That Cow【BFS】
- hdu 2717 Catch That Cow
- HDU 2717 Catch That Cow(BFS)
- 文章标题:HDU 2717 Catch That Cow
- 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搜索 解题报告
- 【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
- hdu 2717 Catch That Cow
- HDU 2717 Catch That Cow 水题BFS