Sicily 1936. Knight Moves

时间:2021-12-19 22:18:36

题目地址:1936. Knight Moves

思路:

这道题一开始不理解题意…orz...囧,看大神们理解的。

题意是说一个8*8的国际象棋,骑士以马的形式走动(“日”字型),指定两个点,输出最小的步骤。

可以利用广度搜索解决。

具体代码如下:

 #include <iostream>
#include <queue>
#include <cstring>
#include <string>
using namespace std; int dx[] = {-, -, -, -, , , , }; //可以走八个方向
int dy[] = {-, -, , , , , -, -}; bool visited[]; int main() {
int t;
cin >> t;
while (t--) {
memset(visited, false, sizeof(visited));
int distance[] = {}; string node1, node2;
cin >> node1 >> node2; int X = (node1[]-'a')* + node1[]-'';
int Y = (node2[]-'a')* + node2[]-''; queue<int> store;
store.push(X);
while (!store.empty()) {
if (store.front() == Y)
break; int x = store.front()/;
int y = store.front()%; for (int i = ; i < ; i++) {
int nx = x+dx[i];
int ny = y+dy[i]; if (nx < ||nx > ||ny < ||ny > )
continue;
int temp = nx* + ny; if (!visited[temp]) {
store.push(temp);
visited[temp] = true;
distance[temp] = distance[store.front()] + ;
}
}
store.pop();
}
cout << "To get from " << node1
<< " to " << node2 << " takes "
<< distance[Y] << " knight moves.\n";
} return ;
}