HDU-2717-Catch That Cow
2013-08-12 15:54
309 查看
//只有三种搜素情况 #include<iostream> #include<queue> #include<cstring> using namespace std; int map[100010]; int k,n; struct node { int x; int time; }; int check(int x) { if(x<0||x>100000||map[x]) return 0; return 1; } int bfs(int x) { queue<node>q; node a,b; a.x=x; a.time=0; map[x]=1; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(a.x==k) return a.time; b=a; b.x=a.x+1; if(check(b.x)) { b.time=a.time+1; map[b.x]=1; q.push(b); } b.x=a.x-1; if(check(b.x)) { b.time=a.time+1; map[b.x]=1; q.push(b); } b.x=2*a.x; if(check(b.x)) { b.time=a.time+1; map[b.x]=1; q.push(b); } } return -1; } int main() { int ans; while(cin>>n>>k) { memset(map,0,sizeof(map)); ans=bfs(n); cout<<ans<<endl; } return 0; }
相关文章推荐
- hdu 2717 Catch That Cow (BFS)
- HDU 2717 Catch That Cow
- C语言BFS(3)___Catch That Cow(Hdu 2717)
- HDU_2717_Catch That Cow
- 题解: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
- 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
- hdu 2717 Catch That Cow
- hdu 2717 Catch That Cow
- hdu 2717——Catch That Cow
- 【HDU 2717 Catch That Cow】+ 队列
- HDU-2717 Catch That Cow(搜索)