宽搜题
#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
using namespace std;
typedef unsigned long long ull;
struct point {
int r;
int c;
};
int vis[10][10];
int dre[8][2] = {{-2, -1}, {-1, -2}, {-2, 1}, {-1, 2}, {2, 1}, {1, 2}, {2, -1}, {1, -2}};
//ull cmp(int pos, ull s) {
// ull x = 1;
// x <<= (64 - pos);
// s |= x;
// return s;
//}
map<ull, int> mp;
int bfs(int orow, int ocol, int drow, int dcol) {
queue<point> q;
point p;
p.r = orow;
p.c = ocol;
vis[orow][ocol] = 1;
q.push(p);
int k = 0;
while (!q.empty()) {
int sz = q.size();
for (int i = 0; i < sz; i++) {
point pt = q.front();
q.pop();
int row = pt.r;
int col = pt.c;
int newr, newc, newpos;
for (int i = 0; i < 8; i++) {
newr = row + dre[i][0];
newc = col + dre[i][1];
if (newr >= 1 && newr <= 8 && newc >= 1 && newc <= 8) {
if (!vis[newr][newc]) {
if (newr == drow && newc == dcol) return (k + 1);
vis[newr][newc] = 1;
point newp = {newr, newc};
q.push(newp);
}
}
}
}
k++;
}
}
int main() {
string o, d;
while (cin >> o >> d) {
memset(vis, 0, sizeof(vis));
int orow = (o[0] - 'a') + 1;
int ocol = (o[1] - '0');
int drow = (d[0] - 'a') + 1;
int dcol = (d[1] - '0');
if (o == d) {
cout << "To get from " << o << " to " << d << " takes " << 0 << " knight moves." << endl;
continue;
}
int step = bfs(orow, ocol, drow, dcol);
cout << "To get from " << o << " to " << d << " takes " << step << " knight moves." << endl;
}
}