贴错代码,贡献了一次WA // zju1091 Knight Moves #include <cstdio> #include <cstring> //using namespace std; char chessboard[8][8]; struct POS { long x; long y; POS(char str[]) { x = str[0]-'a'; y = str[1]-'1'; } POS(long in_x, long in_y) { x = in_x; y = in_y; } POS() {} POS operator+ (const POS& right) { return POS(x+right.x, y+right.y); } bool operator== (const POS& right) { return (x==right.x)&&(y==right.y); } inline bool valid() { return ((0<=x) && (x<8) && (0<=y) && (y<8)); } inline bool isOccupy() { return (chessboard[x][y] > 0); } inline void occupy(char steps) { chessboard[x][y] = steps; } inline char steps() { return chessboard[x][y]; } }; POS moves[8] = {POS(1,2), POS(2,1), POS(2,-1), POS(1,-2), POS(-1,-2), POS(-2,-1), POS(-2,1), POS(-1,2)}; POS queue[100]; long queueStart, queueEnd; int main() { long i; char szStart[5], szEnd[5]; while (2 == scanf("%s%s", szStart, szEnd)) { POS posStart(szStart); POS posEnd(szEnd); memset(chessboard, 0, 8*8*sizeof(chessboard[0][0])); queue[0] = posStart; queueStart = 0; queueEnd = 0; posStart.occupy(1); char steps = 0; while (queueStart <= queueEnd) { POS posCur = queue[queueStart++]; steps = posCur.steps(); if (posCur == posEnd) break; for (i=0; i<8; i++) { POS posNext = posCur + moves[i]; if (posNext.valid() && !posNext.isOccupy()) { posNext.occupy(steps+1); queue[++queueEnd] = posNext; } } } printf("To get from %s to %s takes %ld knight moves./n", szStart, szEnd, (long)(steps-1)); } return 0; }