poj 3278(bfs)

一条线上bfs搜就行

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100000+100;
int n,k;
bool vis[maxn*2+100];
struct note
{
    int x;
    int cnt;
};
int bfs(int x,int y)
{
    queue<note> q;
    q.push(note{x,0});
    while(q.size())
    {
        note hh=q.front();
        q.pop();
         int mm=hh.cnt;
         int xx=hh.x;
         int aa;
         aa=xx+1;
        if(aa>=0&&aa<=2*maxn&&!vis[aa])
        {
            if(aa==y) return mm+1;
            vis[aa]=1;
            q.push(note{aa,mm+1});
        }
         aa=xx-1;
         if(aa>=0&&aa<=2*maxn&&!vis[aa])
        {
            if(aa==y) return mm+1;
            vis[aa]=1;
            q.push(note{aa,mm+1});
        }
         aa=xx*2;
        if(aa>=0&&aa<=2*maxn&&!vis[aa])
        {
            if(aa==y) return mm+1;
            vis[aa]=1;
            q.push(note{aa,mm+1});
        }
    }
    return -1;

}
int main()
{
      while(~scanf("%d%d",&n,&k))
      {  memset(vis,0,sizeof(vis));
          if(n==k) printf("0\n");
          else
          {
              int mm=bfs(n,k);
               printf("%d\n",mm);
          }
      }
    return 0;
}
时间: 2024-10-12 16:44:11

poj 3278(bfs)的相关文章

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

POJ 3278 Catch That Cow(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

POJ 3278: Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 44613   Accepted: 13946 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,00

poj 3278 Catch That Cow(广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 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,00

poj 3278:Catch That Cow(简单一维广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 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,00

poj 3278

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 43851   Accepted: 13674 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,00

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

poj 2243 bfs 利用 结构体中的step成员保存步数 ,STL的队列

//BFS #include <iostream> #include <queue> using namespace std; bool used[8][8]; int move[8][2]={1,2, -1,2, -2,1, -2,-1, -1,-2, 1,-2, 2,-1, 2,1}; struct position { int i,j; int step; position(int a,int b,int c) { i=a; j=b; step=c; } }; int mai

poj 1915 BFS 利用 pre 计算步数------------------路径

// BFS #include <stdio.h> #include <string.h> int visited[301][301]; // visited 已经访问过了 int dic[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; int head,end,n,ex,ey,sx,sy; struct quene { int x,y,pre; // pre 前一个结点 }q[100000]; vo