武士风度的牛[CH2906]

欢迎大家访问我的老师的OJ———caioj.cn

题面描述

传送门

思路

就是一道普通的宽搜题,
就是马字形的有点鬼畜的说?

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<iostream>
#include<cstdlib>
using namespace std;
const int dx[8]={-2,2,1,-1,-2,2,1,-1};
const int dy[8]={-1,1,-2,2,1,-1,2,-2};
struct node
{
	int x,y,d;
	node(){x=0,y=0,d=0;}
};
queue<node>q;
char s[1100][1100];
int stx,sty,edx,edy;
bool v[1100][1100];int n,m;
void bfs()
{
	node now;
	now.x=stx,now.y=sty,now.d=0;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();q.pop();
		if(now.x==edx&&now.y==edy)printf("%d\n",now.d);
		for(int i=0;i<8;i++)
		{
			node k=now;
			k.x+=dx[i],k.y+=dy[i];
			if(v[k.x][k.y]||k.x<1||k.x>n||k.y<1||k.y>m)continue;
			v[k.x][k.y]=1;
			k.d++;
			q.push(k);
		}
	}
}
int main()
{
	scanf("%d%d",&m,&n);
	for(int i=1;i<=n;i++)cin>>s[i]+1;
	memset(v,0,sizeof(v));
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(s[i][j]=='H')
			{
				edx=i,edy=j;
			}
			else if(s[i][j]=='K')
			{
				stx=i,sty=j;
				v[i][j]=1;
			}
			else if(s[i][j]=='*')
			{
				v[i][j]=1;
			}
		}
	}
	bfs();
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值