poj3278

数组必须开大点

#include <iostream>
#include <stdio.h>
#include
<string.h>
#include <stdlib.h>

using namespace std;
struct node
{
int
x,ans;
}q[1000005];
int jx[]={-1,1};
int n,k;
int
map[1000005],v[1000005];
void bfs()
{
struct node t,f;
int
e=0,s=0;
t.x=n;
v[t.x]=1;
t.ans=0;
q[e++]=t;

while(s<e)
{
t=q[s++];
if(t.x==k)

{
printf("%d\n",t.ans);
break;
}

for(int i=0;i<3;i++)
{
if(i==2)

f.x=t.x*2;
else f.x=t.x+jx[i];

if(!v[f.x]&&f.x>=0&&f.x<=100000)
{

f.ans=t.ans+1;
q[e++]=f;

v[f.x]=1;
}
}
}
}
int main()
{

while(scanf("%d%d",&n,&k)!=EOF)
{

memset(map,0,sizeof(map));
memset(v,0,sizeof(v));

bfs();
}
return 0;
}

poj3278,布布扣,bubuko.com

时间: 2024-11-05 11:42:52

poj3278的相关文章

POJ3278,Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46459   Accepted: 14574 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (0 ≤ N ≤ 100,000

poj3278(Catch That Cow)

题目大意: 一个农主寻找牛.给出农主的位置n和牛的位置k.农主可以通过n-1或者n+1或者n*2的步伐找牛,问至少多少步才能找到自己的牛. 解题思路: 简单的BFS.把农主的每一种可能的步伐通过BFS存到栈中,然后看最少多少步到达K坐标. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstri

POJ3278 HDU2717 Catch That Cow

问题链接:POJ3278 HDU2717 Catch That Cow. 题意简述:一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N.若FJ在x位置,FJ有三种走法,分别是走到x-1.x+1或2x位置.求从K走到N的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点. 需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧. AC的C++语言程

【poj3278】Catch 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 Jo

【转】POJ-3278 Catch That Cow:BFS

能想到用广搜来解这道题也够diao了:广搜到 目标节点 就可以得到答案 steps #include<iostream> #include<queue> #include<cstring> using namespace std; struct node { int Value, Steps; node( int N, int S ): Value(N), Steps(S){} }; queue<node>Que; int Visited[100000];

poj3278 Catch 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 Jo

【BFS】Catch That Cow(POJ3278)

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 Jo

超超超简单的bfs——POJ-3278

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 89836   Accepted: 28175 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

POJ3278(KB1-C 简单搜索)

Catch 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