[洛谷P1747]好奇怪的游戏

题目大意:有两匹马,马可以走"日",也可以像象走"田",求它走到(1,1)的步数。 

题解:bfs

卡点:边界判断成了可以走到(0,y)或(x,0) 

 

C++ Code:

#include<cstdio> 
#include<cstring>
using namespace std;
const int maxn=51;
int dx[12]={2,-2,2,-2,1,1,-1,-1,2,2,-2,-2};
int dy[12]={2,2,-2,-2,2,-2,2,-2,1,-1,1,-1};
int sx,sy,h,t;
int q[maxn*maxn+100][2],s[maxn][maxn];
bool cj(int x,int y){
	if (x<1||y<1||x>50||y>50)return true;
	if (s[x][y])return true;
	return false;
}
int bfs(int sx,int sy){
	if (sx==1&&sy==1)return 0;
	s[sx][sy]=1;
	q[t=1][h=0]=sx;q[1][1]=sy;
	while (h<t){
		int x=q[++h][0],y=q[h][1];
		for (int i=0;i<12;i++){
			int xx=x+dx[i],yy=y+dy[i];
			if (cj(xx,yy))continue;
			q[++t][0]=xx;q[t][1]=yy;
			s[xx][yy]=s[x][y]+1;
			if (xx==1&&yy==1)return s[1][1]-1;
		}
	}
	return 0;
}
int main(){
	scanf("%d%d",&sx,&sy);
	printf("%d\n",bfs(sx,sy));
	memset(s,0,sizeof(s));
	scanf("%d%d",&sx,&sy);
	printf("%d\n",bfs(sx,sy));
	return 0;
}

 

转载于:https://www.cnblogs.com/Memory-of-winter/p/8168829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值