您的位置:首页 > 其它

HDU 2717 Catch That Cow(BFS,每次有3种走路方式,问最少的步数到达目的地)

2015-11-01 18:21 417 查看
题目地址:点击打开链接

题意:农民要在一条线捉一头牛,假设他的坐标为x,则他有3种走路方式(1)x-1  (2)x+1 (3)2*x;问最少的步数捉到牛

思路:刚上来直接模拟也没判断,结果果断超内存,减了不少还是超,看了别人的题解,我是出来的时候判断结果T了,别人是进去的时候判断结果A了,有时间仔细研究一下,还有一个神减枝是应经走过的路标记一下,下一次走过的时候时间肯定比第一次走过的时间长,所以可以直接减掉

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

int n,k;
int visit[100010];

struct node
{
int a;
int step;
}from,temp;

bool check(int x)
{
if(x < 0 || x > 100000 || visit[x])
return false;
return true;
}

void bfs()
{
queue<node> q;
from.a = n;
from.step = 0;
q.push(from);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.a == k)
break;
from.a = 2 * temp.a;
if(check(from.a))
{
visit[from.a] = 1;
from.step = temp.step + 1;
q.push(from);
}
from.a = temp.a - 1;
if(check(from.a))
{
visit[from.a] = 1;
from.step = temp.step + 1;
q.push(from);
}
from.a = temp.a + 1;
if(check(from.a))
{
visit[from.a] = 1;
from.step = temp.step + 1;
q.push(from);
}
}
printf("%d\n",temp.step);
}

int main()
{
while(scanf("%d%d",&n,&k) != EOF)
{
memset(visit,0,sizeof(visit));
bfs();
}
return 0;
}


MLE代码1:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

int n,k;

struct node
{
int a;
int step;
}from,temp;

void bfs()
{
queue<node> q;
from.a = n;
from.step = 0;
q.push(from);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.a == k)
break;
from.a = 2 * temp.a;
from.step = temp.step + 1;
q.push(from);
from.a = temp.a - 1;
from.step = temp.step + 1;
q.push(from);
from.a = temp.a + 1;
from.step = temp.step + 1;
q.push(from);
}
printf("%d\n",temp.step);
}

int main()
{
while(scanf("%d%d",&n,&k) != EOF)
{
bfs();
}
return 0;
}


这个貌似有点太浪了

MLE代码2:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

int n,k;
int visit[100010];

struct node
{
int a;
int step;
}from,temp;

bool check(int x)
{
if(x < 0 || x > 100000 || visit[x])
return false;
return true;
}

void bfs()
{
queue<node> q;
from.a = n;
from.step = 0;
q.push(from);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.a == k)
break;
if(check(temp.a))
{
if(temp.a < k)
{
from.a = 2 * temp.a;
from.step = temp.step + 1;
q.push(from);
from.a = temp.a + 1;
from.step = temp.step + 1;
q.push(from);
}
if(temp.a > k / 2)
{
from.a = temp.a - 1;
from.step = temp.step + 1;
q.push(from);
}
}
}
printf("%d\n",temp.step);
}

int main()
{
while(scanf("%d%d",&n,&k) != EOF)
{
memset(visit,0,sizeof(visit));
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: