本文共 2393 字,大约阅读时间需要 7 分钟。
#include这道题和几乎一模一样。只是输入处理费了点功夫,C语言忘了好多。#include using namespace std;bool checked[64];//int xstep[8] = {-1, -2, -2, -1, 1, 2, 2, 1};//int ystep[8] = {-2, -1, 1, 2, -2, -1, 1, 2};int knightmove(int sx, int sy, int ex, int ey, int len){ int move = 0; queue > que; que.push(make_pair (sx, sy)); int cur = 1, next = 0; while(!que.empty()) { pair now = que.front(); que.pop(); cur--; if(now.first == ex && now.second == ey) { return move; } if(!checked[(now.first - 1) * len + (now.second - 2)] && now.first - 1 >= 0 && now.second - 2 >= 0) { que.push(make_pair (now.first - 1, now.second - 2)); checked[(now.first - 1) * len + (now.second - 2)] = true; next++; } if(!checked[(now.first - 2) * len + (now.second - 1)] && now.first - 2 >= 0 && now.second - 1 >= 0) { que.push(make_pair (now.first - 2, now.second - 1)); checked[(now.first - 2) * len + (now.second - 1)] = true; next++; } if(!checked[(now.first - 2) * len + (now.second + 1)] && now.first - 2 >= 0 && now.second + 1 < len) { que.push(make_pair (now.first - 2, now.second + 1)); checked[(now.first - 2) * len + (now.second + 1)] = true; next++; } if(!checked[(now.first - 1) * len + (now.second + 2)] && now.first - 1 >= 0 && now.second + 2 < len) { que.push(make_pair (now.first - 1, now.second + 2)); checked[(now.first - 1) * len + (now.second + 2)] = true; next++; } if(!checked[(now.first + 1) * len + (now.second - 2)] && now.first + 1 < len && now.second - 2 >= 0) { que.push(make_pair (now.first + 1, now.second - 2)); checked[(now.first + 1) * len + (now.second - 2)] = true; next++; } if(!checked[(now.first + 2) * len + (now.second - 1)] && now.first + 2 < len && now.second - 1 >= 0) { que.push(make_pair (now.first + 2, now.second - 1)); checked[(now.first + 2) * len + (now.second - 1)] = true; next++; } if(!checked[(now.first + 2) * len + (now.second + 1)] && now.first + 2 < len && now.second + 1 < len) { que.push(make_pair (now.first + 2, now.second + 1)); checked[(now.first + 2) * len + (now.second + 1)] = true; next++; } if(!checked[(now.first + 1) * len + (now.second + 2)] && now.first + 1 < len && now.second + 2 < len) { que.push(make_pair (now.first + 1, now.second + 2)); checked[(now.first + 1) * len + (now.second + 2)] = true; next++; } if(cur == 0) { move++; cur = next; next = 0; } } //cout<<"somewhere impossible."<
转载地址:http://sxxli.baihongyu.com/