C - Catch That Cow
2015-07-15 13:40
267 查看
题目大意农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在NN (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ≤ 100,000) ,约翰下次可以移动到x+1或者x-1或者2*x的地方,问约翰最少需要多少步才能找到他的牛。
也是非常非常水的题目,求最快当然是用广搜...
//////////////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 100001
int v[maxn];//标记是否走过,防止重走
int BFS(int s, int e)
{
queue<int> Q;
Q.push(s);
v[s] = 1;
while(Q.size())
{
s = Q.front();
Q.pop();
if(s == e)
return v[s]-1;
for(int i=0; i<3; i++)
{
int q=s;
if(i == 0)
q--;
else if(i == 1)
q++;
else
q *= 2;
if(q>=0 && q <=maxn && v[q] == 0)
{
Q.push(q);
v[q] = v[s]+1;
}
}
}
return -1;
}
int main()
{
int s, e;
while(scanf("%d%d", &s, &e) != EOF)
{
memset(v, 0, sizeof(v));
int ans = BFS(s, e);
printf("%d\n", ans);
}
return 0;}
也是非常非常水的题目,求最快当然是用广搜...
//////////////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 100001
int v[maxn];//标记是否走过,防止重走
int BFS(int s, int e)
{
queue<int> Q;
Q.push(s);
v[s] = 1;
while(Q.size())
{
s = Q.front();
Q.pop();
if(s == e)
return v[s]-1;
for(int i=0; i<3; i++)
{
int q=s;
if(i == 0)
q--;
else if(i == 1)
q++;
else
q *= 2;
if(q>=0 && q <=maxn && v[q] == 0)
{
Q.push(q);
v[q] = v[s]+1;
}
}
}
return -1;
}
int main()
{
int s, e;
while(scanf("%d%d", &s, &e) != EOF)
{
memset(v, 0, sizeof(v));
int ans = BFS(s, e);
printf("%d\n", ans);
}
return 0;}
相关文章推荐
- springMvc配置文件
- UEFI Boot Flow
- java中fail-fast 和 fail-safe的区别
- [轉]获取SQL数据库中的数据库名、所有表名、所有字段名、列描述
- 视频网站如何让用户形成使用订阅的习惯
- How-to: deploy hadoop client with some special user based on acl enbaled cluster
- mysql 数据导入时的编码设置
- android访问php webservice简单一例
- 部分会计科目含义
- USB键盘背后的那些事儿--Legacy USB实现 .
- rhc常用命令及错误排查
- Legacy USB在两种情况下的实现
- Genymotion video card not rupport OpenGL2.0
- 互联网时代的精准招聘-Uber新手游有感
- app后端开发三:laravel中使用百度的消息推送
- Mac 如何安装Homebrew?
- eclipse 访问 hive1.2.1
- Interrupt--PIC--APIC
- shell计算总结
- jcaptcha进阶