【题目链接】:http://hihocoder.com/problemset/problem/1308

【题意】

【题解】



用bfs处理出3个骑士到每个点的最短路;

然后枚举最后3个骑士到了哪一个点.

把3个骑士的最短路加起来取最小值就好;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,2,2,1,-1,-2,-2,-1,1};
const int dy[9] = {0,-1,1,2,2,1,-1,-2,-2};
const double pi = acos(-1.0);
const int N = 10; int t;
char s[4][4];
pii a[4];
int dis[4][N][N];
queue <pii> dl; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
ms(dis,0);
cin >> t;
while (t--)
{
rep1(i,1,3)
rep1(j,1,8)
rep1(k,1,8)
dis[i][j][k] = -1;
rep1(i,1,3)
{
cin >> s[i];
a[i].fi = s[i][0]-'A'+1,a[i].se = s[i][1]-'0';
}
rep1(i,1,3)
{
dl.push(a[i]);
dis[i][a[i].fi][a[i].se] = 0;
while (!dl.empty())
{
int x = dl.front().fi,y = dl.front().se;dl.pop();
rep1(j,1,8)
{
int tx = x+dx[j],ty = y+dy[j];
if (dis[i][tx][ty]==-1)
{
dis[i][tx][ty] = dis[i][x][y]+1;
dl.push(mp(tx,ty));
}
}
}
}
int ans = dis[1][1][1]+dis[2][1][1]+dis[3][1][1];
rep1(i,1,8)
rep1(j,1,8)
ans = min(ans,dis[1][i][j]+dis[2][i][j]+dis[3][i][j]);
cout << ans << endl;
}
return 0;
}
05-28 12:00