POJ-3278 Catch That Cow

9 篇文章 0 订阅
7 篇文章 0 订阅

题目通道

解法

bfs+queue

写法一
//Accepted	736K	0MS	C++	1017B	2012-11-10 04:24:09
//#include"pch.h"
//#pragma warning(disable:4996);
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
//#define _CRT_SECURE_NO_DEPRECATE;
//#define _CRT_SECURE_NO_WARNINGS;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x = 0, y = 1; char a = getchar(); while (a > '9' || a < '0') { if (a == '-')y = -1; a = getchar(); }
	while (a >= '0' && a <= '9') { x = 10 * x + a - '0'; a = getchar(); }return x * y;
}
const int maxn=100001;
bool vis[maxn];
int n,k;
struct node
{
    int x,step;
};
queue<node>q; 
int bfs()
{
    int i;
    node now,next;
	now.x = n; now.step = 0; 
	q.push(now);
    vis[n]=true;
    while( q.empty() == 0 )
    {
		now = q.front();
		q.pop();        
		if ( now.x == k ){
			return now.step; 
		}
		for(i=0;i<3;i++)
        {
            if(i==0) next.x=now.x-1;
            else if(i==1) next.x=now.x+1;
            else next.x=2*now.x;
            if(next.x<0 || next.x>=maxn) continue;//剪枝、排除越界
            if( vis[next.x] )	continue; 
            vis[next.x]=true;
            next.step=now.step+1;
            q.push(next);
//          if(next.x==k) return next.step;
            
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(vis,0,sizeof(vis));
		printf("%d\n",bfs());
    }
    return 0;
}
写法二
//Accepted	736K	0MS	C++	1017B	2012-11-10 04:24:09
//#include"pch.h"
//#pragma warning(disable:4996);
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
//#define _CRT_SECURE_NO_DEPRECATE;
//#define _CRT_SECURE_NO_WARNINGS;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x = 0, y = 1; char a = getchar(); while (a > '9' || a < '0') { if (a == '-')y = -1; a = getchar(); }
	while (a >= '0' && a <= '9') { x = 10 * x + a - '0'; a = getchar(); }return x * y;
}
const int maxn=100001;
bool vis[maxn];
int n,k;
struct node
{
    int x,step;
};
queue<node>q; 
int bfs()
{
    node now,next;
	now.x = n; now.step = 0; 
	q.push(now);
    vis[n]=true;
    while( q.empty() == 0 )
    {
		now = q.front();
		q.pop();        
		for(int i=0;i<3;i++)
        {
            if(i==0) next.x=now.x-1;
            else if(i==1) next.x=now.x+1;
            else next.x=2*now.x;
            if(next.x<0 || next.x>=maxn) continue;//剪枝、排除越界
            if( vis[next.x] )	continue; 
            vis[next.x]=true;
            next.step=now.step+1;
            q.push(next);
            if(next.x==k) return next.step;
            
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(vis,0,sizeof(vis));
		printf("%d\n",bfs());
    }
    return 0;
}

写法三
//Accepted	736K	0MS	C++	1017B	2012-11-10 04:24:09
//#include"pch.h"
//#pragma warning(disable:4996);
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std;
//#define _CRT_SECURE_NO_DEPRECATE;
//#define _CRT_SECURE_NO_WARNINGS;
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x = 0, y = 1; char a = getchar(); while (a > '9' || a < '0') { if (a == '-')y = -1; a = getchar(); }
	while (a >= '0' && a <= '9') { x = 10 * x + a - '0'; a = getchar(); }return x * y;
}
const int maxn=100001;
bool vis[maxn];
int n,k;
struct node
{
    int x,step;
};
queue<node>q; 
int bfs()
{
    int i;
    node now,next;
	now.x = n; now.step = 0; 
	q.push(now);
    vis[n]=true;
    while( q.empty() == 0 )
    {
		now = q.front();
		q.pop();        
		if ( now.x == k ){
			return now.step; 
		}
		next.step = now.step+1; 
		next.x = now.x * 2;
 
        if ( next.x >= 0 && next.x < maxn && vis[next.x]==0){
        	vis[next.x]=true;    
	        q.push(next); 		  	
        }
        
		next.step = now.step+1; 
		next.x = now.x + 1;
        if ( next.x >= 0 && next.x < maxn && vis[next.x]==0){
        	vis[next.x]=true;    
	        q.push(next); 		  	
        }  		

		next.step = now.step+1; 
		next.x = now.x - 1;	
        if ( next.x >= 0 && next.x < maxn && vis[next.x]==0){
        	vis[next.x]=true;    
	        q.push(next); 		  	
        }		
    }
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        memset(vis,0,sizeof(vis));
		printf("%d\n",bfs());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值