简单搜索-POJ3278Catch That Cow (#####)

POJ3278Catch That Cow

Description

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?

Input

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

Output

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

Sample Input

5 17

Sample Output

4

Hint

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.
一个简单的bfs,就是其实本来不想写的题,但是!!
它re了
就要写一下它为什么re了
(蟹蟹师哥哥给debug)

RE代码

#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define PI         acos(-1)
#define inf        0x3f3f3f3f
#define EPS        1e-6
#define mem(a, b)  memset(a, b, sizeof(a))
#define ll long long
#define mian main
bool visit[100010];
int dir[2] = {1, -1};
int flag;
struct node
{
    int x;
    int s;
};
queue<node> q;
int n, k;
int bfs()
{
    while(!q.empty())
    {

        node m=q.front();
        //cout<<m.x<<' '<<m.s<<endl;
        q.pop();
        //visit[m.x] = 1;
        if(m.x == k)
        {
            return m.s;
        }
        node m1, m2;
        for(int i = 0; i < 2; i ++)
        {
            m1.x = m.x + dir[i];
            m1.s = m.s + 1;
            if(m1.x >= 0 && m1.x <= 100000 && !visit[m1.x])
            {
                 q.push(m1);
                 visit[m1.x] = 1;
            }

        }
        m2.x = m.x * 2;
        m2.s = m.s + 1;
        if(!visit[m2.x] && m2.x >= 0 && m2.x <= 100000)
        {
            q.push(m2);
            visit[m2.x] = 1;
        }

    }
    //return 0;
}
int main()
{
    while(cin>>n>>k)
    {
        mem(visit, 0);
        while(!q.empty())
        {
            q.pop();
        }
        node pre;
        visit[n] = 1;
        pre.x = n;
        pre.s = 0;
        q.push(pre);
        int ans  = bfs();
        cout<<ans<<endl;

    }
    return 0;
}

AC代码

#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define PI         acos(-1)
#define inf        0x3f3f3f3f
#define EPS        1e-6
#define mem(a, b)  memset(a, b, sizeof(a))
#define ll long long
#define mian main
bool visit[100010];
int dir[2] = {1, -1};
int flag;
struct node
{
    int x;
    int s;
};
queue<node> q;
int n, k;
int bfs()
{
    while(!q.empty())
    {

        node m=q.front();
        //cout<<m.x<<' '<<m.s<<endl;
        //
        q.pop();
        //visit[m.x] = 1;
        if(m.x == k)
        {
            return m.s;
        }
        node m1, m2;
        for(int i = 0; i < 2; i ++)
        {
            m1.x = m.x + dir[i];
            m1.s = m.s + 1;
            if(m1.x >= 0 && m1.x <= 100000 && !visit[m1.x])
            {
                 q.push(m1);
                 visit[m1.x] = 1;
            }

        }
        m2.x = m.x * 2;
        m2.s = m.s + 1;
        if(m2.x >= 0 && m2.x <= 100000 && !visit[m2.x])
        {
            q.push(m2);
            visit[m2.x] = 1;
        }

    }
    //return 0;
}
int main()
{
    while(cin>>n>>k)
    {
        mem(visit, 0);
        while(!q.empty())
        {
            q.pop();
        }
        node pre;
        visit[n] = 1;
        pre.x = n;
        pre.s = 0;
        q.push(pre);
        int ans  = bfs();
        cout<<ans<<endl;

    }
    return 0;
}

你看你看,这两个有什么区别
哇啊
就第56行那个if条件里面,哇啊就应该先判断他这个有没有越界再判断有没有visit
哇啊啊

猜你喜欢

转载自blog.csdn.net/wuswi0412/article/details/81205598
今日推荐