hdu 2717Catch That Cow
来源:互联网 发布:国际象棋跑分软件 编辑:程序博客网 时间:2024/05/22 01:42
/*
BFS
+1 -1 *2 三个方向搜索
*/
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int N,K; // 起点 终点
bool isVisited[200001]; //标记是否访问过
struct Node
{
int data; //point
int times; //找到这个点的次数
};
bool ismid(int x) //判断区间
{
if(x >= 0 && x <= 100000)
return true;
return false;
}
int BFS(int N,int K)
{
queue<Node> Q;
Node st,ed;
memset(isVisited,0,sizeof(isVisited));
st.data = N;
st.times = 0;
Q.push(st); //初始化队列第一个元素
isVisited[N] = 1;
while(!Q.empty())
{
st = Q.front();//去队列第一个元素
Q.pop(); //队列第一个元素出队
if(st.data == K)
{
return st.times;
}
if(ismid(st.data+1) && !isVisited[st.data+1])
{
ed.data = st.data+1;
ed.times = st.times + 1;
isVisited[st.data+1] = 1;//标记
Q.push(ed);
}
if(ismid(st.data-1) && !isVisited[st.data-1])
{
ed.data = st.data - 1;
ed.times = st.times + 1;
isVisited[st.data - 1] = 1;
Q.push(ed);
}
if(ismid(2*st.data) && !isVisited[2*st.data])
{
ed.data = 2*st.data;
ed.times = st.times + 1;
isVisited[2*st.data] = 1;
Q.push(ed);
}
}
}
int main()
{
while(cin>>N>>K)
{
if(N >= K)
{
cout<<N - K<<endl;
}
else
{
cout<<BFS(N,K)<<endl;
}
}
return 0;
}
BFS
+1 -1 *2 三个方向搜索
*/
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int N,K; // 起点 终点
bool isVisited[200001]; //标记是否访问过
struct Node
{
int data; //point
int times; //找到这个点的次数
};
bool ismid(int x) //判断区间
{
if(x >= 0 && x <= 100000)
return true;
return false;
}
int BFS(int N,int K)
{
queue<Node> Q;
Node st,ed;
memset(isVisited,0,sizeof(isVisited));
st.data = N;
st.times = 0;
Q.push(st); //初始化队列第一个元素
isVisited[N] = 1;
while(!Q.empty())
{
st = Q.front();//去队列第一个元素
Q.pop(); //队列第一个元素出队
if(st.data == K)
{
return st.times;
}
if(ismid(st.data+1) && !isVisited[st.data+1])
{
ed.data = st.data+1;
ed.times = st.times + 1;
isVisited[st.data+1] = 1;//标记
Q.push(ed);
}
if(ismid(st.data-1) && !isVisited[st.data-1])
{
ed.data = st.data - 1;
ed.times = st.times + 1;
isVisited[st.data - 1] = 1;
Q.push(ed);
}
if(ismid(2*st.data) && !isVisited[2*st.data])
{
ed.data = 2*st.data;
ed.times = st.times + 1;
isVisited[2*st.data] = 1;
Q.push(ed);
}
}
}
int main()
{
while(cin>>N>>K)
{
if(N >= K)
{
cout<<N - K<<endl;
}
else
{
cout<<BFS(N,K)<<endl;
}
}
return 0;
}
- hdu 2717 Catch That Cow
- HDU-2717-Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717Catch 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
- 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 2717Catch That Cow
- HDU 2717 Catch That Cow
- hdu 2717 Catch That Cow
- hdu 2717 Catch That Cow
- android布局属性: android:visibility
- hdu4436
- 80X86 汇编指令符号大全
- SimpleDateFormat使用详解
- LVS比较好的介绍,较为全面
- hdu 2717Catch That Cow
- 浅谈数据库设计技巧
- viewDidUnload 和 dealloc 的区别
- xrdp开源项目的基础知识-1 函数指针和回调函数
- OpenCV参考手册之Mat类详解
- WinDbg ACPI debug tip
- 【转载】程序员:伤不起的三十岁
- 新人常犯的错误(留给自己看)
- 校园地图框架设计