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#include #include 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 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#include<string.h> #include #include 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 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#include<string.h> #include #include 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 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; }
不过我还是不知道为什么关系运算符会让它爆内存
纪念第一个内存超限