欢迎大家访问我的老师的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;
}