zoj 1091 bfs

/** 

    换了个分类开始做题。。

     一道简单的搜索题。。一开始本来想把所有情况都打表出来的。。感觉数据量太小没必要了。。。

    

*/




#if 1
#include <iostream>
#include <string>
#include <queue>
#include <string.h>
#include <cstdio>
using namespace std;

int a[ 10][10]={0};
int b[10][10]={0};

struct step{
	int x;
	int y;
	int count;
	step(int c,int d,int e){x=c;y=d;count= e;}
	//step(struct step s){x= s.x;y=s.y;cout=s.count;}
};

queue <struct step> Q1,Q2;

int dx[]={1,2,2,1,-1,-2,-2,-1};
int dy[]={-2,-1,1,2,2,1,-1,-2};


int  isOK(int X,int Y)
{
	if(X > 0 && X< 9 && Y >0 && Y<9) return 1;
	else return 0;
}


int  dfs(int x1,int y1,int x2 ,int y2)
{
  
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		while(!Q1.empty())Q1.pop();
		Q1.push(step(x1,y1,0));
		b[x1][y1] = 1;
		while(!Q1.empty())
		{
			struct step nowStep = Q1.front(); 
			if(nowStep.x == x2 &&nowStep.y == y2)return nowStep.count;
			Q1.pop();
			for(int i = 0; i < 8 ; i++)
			{
				int X= dx[i] + nowStep.x;
				int Y= dy[i]	+ nowStep.y;
				if(!isOK(X,Y) || b[X][Y] > 0 )continue;
				b[X][Y] = 1;
				a[X][Y] = nowStep.count+1;
			  
				Q1.push(step(X,Y,nowStep.count+1));
			}
	  }
}

int main()
{
   string str1,str2;
   while(cin>>str1>>str2)
   {
     int  S = dfs(str1[0] - 'a' + 1 ,str1[1] - '0' ,str2[0] - 'a' + 1 ,str2[1]-'0');
	 printf("To get from %s to %s takes %d knight moves.\n",str1.c_str(),str2.c_str(),S);
   }
 return 0;
}




#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值