#include#define fi first #define se second #define pb push_back #define mk make_pair #define sz(x) ((int) (x).size()) #define all(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef vector vi; typedef pair pa; int xs, ys, xe, ye; int dx[8] = {-2, -2, -1, 1, 2, 2, 1, -1}; int dy[8] = {-1, 1, 2, 2, 1, -1, -2, -2}; int bfs() { queue q; q.push(mk(xs, ys)); int vis[10][10]; fill((int*) vis, (int*) vis + 10 * 10, -1); vis[xs][ys] = 0; while (!q.empty()) { pa now = q.front(); q.pop(); if (now == mk(xe, ye)) return vis[xe][ye]; for (int i = 0; i < 8; i++) { int x = now.fi + dx[i]; int y = now.se + dy[i]; if (x < 0 || x >= 8 || y < 0 || y >= 8) continue; if (vis[x][y] != -1) continue; q.push(mk(x, y)); vis[x][y] = vis[now.fi][now.se] + 1; } } } int main() { string s1, s2; while (cin >> s1 >> s2) { xs = s1[0] - 'a'; ys = s1[1] - '1'; xe = s2[0] - 'a'; ye = s2[1] - '1'; cout << "To get from " << s1 << " to " << s2 << " takes " << bfs() << " knight moves.n"; } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)