## Saturday, April 9, 2011

### UVa_00439_Knight_Moves.cpp

uva00439, poj2243,

# Knight Moves

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

## Input Specification

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

## Output Specification

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

```e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6```

## Sample Output

```To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.```

Solution:

## Source Code:

//Fri Apr  8 18:53:31 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class Square {
public:
int x;
int y;
int cost;
bool visited;

Square(int i, int j, int c, bool v) {
this->x = i;
this->y = j;
this->cost = c;
this->visited = v;
}
};

int xx[] = { -2, -1, +1, +2, +2, +1, -1, -2 };
int yy[] = { -1, -2, -2, -1, +1, +2, +2, +1 };

vector<vector<Square> > chess;
queue<Square> q;

void init() {
chess.clear();
while (q.empty() == false)
q.pop();
for (int i = 0; i < 8; i++) {
vector<Square> tmp;
for (int j = 0; j < 8; j++) {
tmp.push_back(Square(i, j, 0, false));
}
chess.push_back(tmp);
}
//  for (int i = 0; i < 8; i++)
//      for (int j = 0; j < 8; j++)
//          cout << "Chess at " << chess[i][j].x << chess[i][j].y << ": "
//                  << chess[i][j].cost << ", visited: "
//                  << (chess[i][j].visited == true ? 1 : 0) << endl;
}

bool checkBound(int x, int y) {
if (x >= 0 && x < 8 && y >= 0 && y < 8)
return true;
return false;
}

int bfs(string a, string b) {
int x = a[0] - 'a';
int y = a[1] - '1';
chess[x][y].cost = 0;
q.push(chess[a[0] - 'a'][a[1] - '1']);
while (q.empty() == false) {
Square tmp = q.front();
//      cout << "here?" << endl;
q.pop();
x = tmp.x;
y = tmp.y;
//      cout << x << ", " << y << endl;
if (chess[x][y].visited == false) {
chess[x][y].visited = true;
if (x == b[0] - 'a' && y == b[1] - '1')
return chess[x][y].cost;
for (int i = 0; i < 8; i++) {
int x1 = x + xx[i];
int y1 = y + yy[i];
if (checkBound(x1, y1)) {
chess[x1][y1].cost = 1 + chess[x][y].cost;
q.push(chess[x1][y1]);
}
}
}
}
return 0;
}

int main(int argc, const char* argv[]) {
//freopen("input.in", "r", stdin);
string a, b;
while (cin >> a >> b) {
init();
cout << "To get from " << a << " to " << b << " takes " << bfs(a, b)
<< " knight moves." << endl;
}
//fclose(stdin);
return 0;
}