目录:
题目:
分析:
我们可以通过广搜、深搜、记搜来AC,这边小编用的就是深搜+记忆化
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
inline LL read() {
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
int f[10][10],ans;
int ex,ey,bs;
int mx[8]={1,2,2,1,-1,-2,-2,-1},my[9]={-2,-1,1,2,2,1,-1,-2};
//列出八个方向
void dfs(int x,int y)
{
int nx,ny;
if(x==ex&&y==ey) {ans=min(ans,bs);return;}
for(int i=0;i<8;i++)
{
nx=x+mx[i];ny=y+my[i];
if(nx>0&&nx<=8)
if(ny>0&&ny<=8)
{
if(f[nx][ny]==0)
{
f[nx][ny]=-1;bs++;
dfs(nx,ny);
f[nx][ny]=bs;bs--;
}
if(bs<f[nx][ny])//记忆化
{
f[nx][ny]=-1;bs++;
dfs(nx,ny);
f[nx][ny]=bs;bs--;
}
}
}
}
int main()
{
int n=read();
char c;int a,b;
int king=0;
while(n!=-1)
{
king++;
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
cin>>c;
a=c-96;b=read();
f[b][a]=-1;
}
cin>>c;a=c-96;b=read();
cin>>c;ey=c-96;ex=read();
ans=99999999;
f[b][a]=-1;
bs=0;
dfs(b,a);
if(ans==99999999) printf("Board %d: not reachable\n",king);
else printf("Board %d: %d moves\n",king,ans);
n=read();
}
return 0;
}