Catch That Cow

 

Catch That Cow

题目描述:

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. 

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute 
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute. 

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

输入:

Line 1: Two space-separated integers: N and K

输出:

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

提示:

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

题目的意思是,知道农夫和牛的位置,农夫有两种移动方式——移动一格或是传送到当前位置坐标的两倍处,求抓到牛的最短步数。
解题思路启发:https://blog.csdn.net/qq_38620461/article/details/78445374
这题是我做的第一道,也是一道入门的BFS,大概的解题思路就是:从起点开始,一层一层向下遍历,直到遍历出正确答案,需要用到结构体来记录位置和当前的深度,用queue来一层层遍历。
这是第一发代码:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int vis[100005];
struct point
{
	int place,count;
};
int main()
{
	int n,k;
	point x,temp;
	while(~scanf("%d %d",&n,&k))
	{
		if(n==k) {printf("%d\n",0);continue;}
		fill(vis,vis+100005,0);
		queue<point> q;
		point head={n,0};
		q.push(head);
		vis[n]=1;
		while(!q.empty())
		{
			x=q.front();
			q.pop();
			temp=x;
			temp.place-=1;
			if(temp.place>=0&&vis[temp.place]==0)
			{
				++temp.count;
				if(temp.place==k) {printf("%d\n",temp.count);break;}
				q.push(temp);
			}
			temp=x;
			temp.place+=1;
			if(temp.place<=100000&&vis[temp.place]==0)
			{
				++temp.count;
				if(temp.place==k) {printf("%d\n",temp.count);break;}
				q.push(temp);
			}
			temp=x;
			temp.place*=2;
			if(temp.place<=100000&&vis[temp.place]==0)
			{
				++temp.count;
				if(temp.place==k) {printf("%d\n",temp.count);break;}
				q.push(temp);
			}
		}
	}
	return 0;
 }
 
 

  然后,就碰到了我人生中的第一个Memory Limit Exceeded...

很快意识到漏掉了对搜索过的位置进行标记的步骤,于是作出更改,顺手把标记数组改成bool型,节省空间,之后

信心满满的第二发:

#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
bool vis[100005];
struct point
{
    int place,count;
};
int main()
{
    int n,k;
    point x,temp;
    while(~scanf("%d %d",&n,&k))
    {
        if(n==k) {printf("%d\n",0);continue;}
        memset(vis,0,sizeof(vis));
        queue<point> q;
        point head={n,0};
        q.push(head);
        vis[n]=1;
        while(!q.empty())
        {
            x=q.front();
            q.pop();
            temp=x;
            temp.place-=1;
            if(temp.place>=0&&vis[temp.place]==0)
            {
                ++temp.count;
                if(temp.place==k) {printf("%d\n",temp.count);break;}
                q.push(temp);vis[temp.place]==1;
            }
            temp=x;
            temp.place+=1;
            if(temp.place<=100000&&vis[temp.place]==0)
            {
                ++temp.count;
                if(temp.place==k) {printf("%d\n",temp.count);break;}
                q.push(temp);vis[temp.place]==1;
            }
            temp=x;
            temp.place*=2;
            if(temp.place<=100000&&vis[temp.place]==0)
            {
                ++temp.count;
                if(temp.place==k) {printf("%d\n",temp.count);break;}
                q.push(temp);vis[temp.place]==1;
            }
        }
    }
    return 0;
}

然而,依旧是Memory Limit Exceeded...

自己又跑了一遍,输入了比较大的数字,电脑的内存瞬间满了

这下就被迷住了,反复比对了正确代码和自己的代码,感觉整体上差不多一样啊。。。

最后经过了2个小时的debug,终于发现是将赋值运算符打成了关系运算符,改正之后,终于ac了= =

第三发:

#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
bool vis[100005];
struct point
{
    int place,count;
};
int main()
{
    int n,k;
    point x,temp;
    while(~scanf("%d %d",&n,&k))
    {
        if(n==k) {printf("%d\n",0);continue;}
        memset(vis,0,sizeof(vis));
        queue<point> q;
        point head={n,0};
        q.push(head);
        vis[n]=1;
        while(!q.empty())
        {
            x=q.front();
            q.pop();
            temp=x;
            temp.place-=1;
            if(temp.place>=0&&vis[temp.place]==0)
            {
                ++temp.count;
                if(temp.place==k) {printf("%d\n",temp.count);break;}
                q.push(temp);vis[temp.place]=1;
            }
            temp=x;
            temp.place+=1;
            if(temp.place<=100000&&vis[temp.place]==0)
            {
                ++temp.count;
                if(temp.place==k) {printf("%d\n",temp.count);break;}
                q.push(temp);vis[temp.place]=1;
            }
            temp=x;
            temp.place*=2;
            if(temp.place<=100000&&vis[temp.place]==0)
            {
                ++temp.count;
                if(temp.place==k) {printf("%d\n",temp.count);break;}
                q.push(temp);vis[temp.place]=1;
            }
        }
    }
    return 0;
}

不过我还是不知道为什么关系运算符会让它爆内存


纪念第一个内存超限

 

转载于:https://www.cnblogs.com/-y-i-/p/10163875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值