https://community.topcoder.com/stat?c=problem_statement&pm=2915&rd=5853
https://github.com/salman-awan/topcoder-srm-solutions/blob/master/SRM%20207/CaptureThemAll.h
int fastKnight(string knight, string rook, string queen)
{
// get initial positions of knight, rook and queen
auto knight_pos = Position(knight[1] - '1', knight[0] - 'a');
auto rook_pos = Position(rook[1] - '1', rook[0] - 'a');
auto queen_pos = Position(queen[1] - '1', queen[0] - 'a');
// get shortest path from knight -> rook -> queen
int path1 = shortestPath(knight_pos, rook_pos) + shortestPath(rook_pos, queen_pos);
// get shortest path from knight -> queen -> rook
int path2 = shortestPath(knight_pos, queen_pos) + shortestPath(queen_pos, rook_pos);
// return the minimum of path1 and path2
return min(path1, path2);
}
private:
// returns the shortest path length from the knight start position to the knight end position using BFS
int shortestPath(const Position& start, const Position& end)
{
// maintain a set of all visited positions
set<Position> visited;
// maintain a queue of all positions that we need to visit
// the queue will store a std::pair to capture the shortest path lengths
// for each position starting from the start position
queue<pair<Position, int>> q;
// add the start position to the queue to get started
q.push(make_pair(start, 0));
while (!q.empty())
{
// pop next position in the queue
auto cur = q.front();
q.pop();
// add this position to the set of visited positions
visited.insert(cur.first);
// if we have reached the end position, return the shortest path length for this position
if (cur.first == end)
return cur.second;
// otherwise get all valid knight moves from this position
auto moves = moveKnight(cur.first);
// push these knight moves into the queue if we have not visited those positions before
// their shortest path length will be +1 the current shortest path length
for (auto m : moves)
{
if (visited.find(m) == visited.end())
q.push(make_pair(m, cur.second + 1));
}
}
// just a fail-safe to return an arbitrarily large value (in terms of chess board moves)
return 64;
}
https://github.com/smalex-als/smalex-at-topcoder/blob/master/src/p200/srm207/CaptureThemAll.java
public int fastKnight(String knight, String rook, String queen) {
int[] coorsKnight = toCoords(knight);
int[] coorsRook = toCoords(rook);
int[] coorsQueen = toCoords(queen);
int[][] delta = new int[][]{
{-2, -1},
{-2, 1},
{2, -1},
{2, 1},
{-1, -2},
{1, -2},
{-1, 2},
{1, 2}
};
return Math.min(
countStep(coorsKnight, coorsRook, delta) + countStep(coorsRook, coorsQueen, delta),
countStep(coorsKnight, coorsQueen, delta) + countStep(coorsQueen, coorsRook, delta));
}
http://algoexplained.blogspot.com/2016/12/capturethemall-topcoder.html
public int fastKnight(String knight, String rook, String queen) {
boolean[][] joe = new boolean[8][8];
<8 i="" p=""><8 j="" p="">
int hx = Integer.valueOf(knight.substring(1))-1;
int hy = knight.charAt(0) - 'a';
joe[Integer.valueOf(rook.substring(1))-1][rook.charAt(0) - 'a'] = true;
joe[Integer.valueOf(queen.substring(1))-1][queen.charAt(0) - 'a'] = true;
LinkedList queue = new LinkedList();
queue.add(new Harry(hx,hy,0));
int[] captured = new int[4];
while (queue.size() > 0) {
Harry current = queue.removeFirst();
if (capture(current, joe, captured)) {
if (captured[0] != 1) {
captured[0] = 1;
captured[1] = current.move;
queue.clear();
queue.add(new Harry(current.x,current.y,current.move));
} else {
captured[2] = 1;
captured[3] = current.move;
}
}
if (captured[0] == 1 && captured[2] == 1)
return current.move;
addNeighbors(queue, current);
}
return -1;
}
public boolean capture(Harry harry, boolean[][] joe, int[] captured) {
if (joe[harry.x][harry.y]){
// only capture one in the same neighbors
if (!(captured[0] == 1 && captured[1] == harry.move)) {
joe[harry.x][harry.y] = false;
return true;
}
}
return false;
}
public void addNeighbors(LinkedList queue, Harry current) {
int[] dx = {-2,-2,2,2,-1,1,-1,1};
int[] dy = {-1,1,-1,1,-2,-2,2,2};
for (int i=0;i<8 i="" p=""> int x = current.x + dx[i];
int y = current.y + dy[i];
if (x >=0 && y >= 0 && x < 8 && y <8 br=""> queue.add(new Harry(x,y,current.move + 1));
}
}
}
class Harry{
int x;
int y;
int move;
public Harry(int xx, int yy, int m) {
x = xx;
y = yy;
move = m;
}
}
Harry is playing magical chess. In this version of the game, all pieces move the same way as in regular chess, but players can cast some magical spells. Unfortunately, Harry's opponent, Joe, has captured all of Harry's pieces except one knight; Joe, on the other hand, still has a queen and a rook. The only chance Harry has to win this game is to cast a spell, "Haste", that will allow Harry's knight to make several moves in a row. You should determine the minimal number of moves the knight needs to capture both the rook and the queen, assuming neither of them moves. You may capture them in either order - rook first or queen first. You will be given a String, knight, containing information about the knight. You will also be given a String, queen, and a String, rook, giving you information about Joe's pieces. knight, rook and queen will be formatted as "cr", where c is a character between 'a' and 'h', inclusive, determining the column on the board ('a' is the first column, 'h' is the last), and r is a digit between '1' and '8', inclusive, determining the row (1 is the lowest, 8 is the highest). | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | A knight's jump moves him 2 cells along one of the axes, and 1 cell along the other one. In other words, if knight is in the (0,0) now, it can be in (-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (1, -2), (-1, 2) or (1, 2) after his move. | ||||||||||||
- | The knight will capture one of Joe's pieces if it ends its move in the cell that the piece occupies. | ||||||||||||
- | The knight cannot jump out of the board. | ||||||||||||
- | A chessboard has 8 rows and 8 columns. | ||||||||||||
Constraints | |||||||||||||
- | knight, rook and queen will all be distinct. | ||||||||||||
- | knight, rook and queen will be formatted as "cr", where c is a lowercase character between 'a' and 'h', inclusive, and r is a digit between '1' and '8', inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
|
https://github.com/salman-awan/topcoder-srm-solutions/blob/master/SRM%20207/CaptureThemAll.h
int fastKnight(string knight, string rook, string queen)
{
// get initial positions of knight, rook and queen
auto knight_pos = Position(knight[1] - '1', knight[0] - 'a');
auto rook_pos = Position(rook[1] - '1', rook[0] - 'a');
auto queen_pos = Position(queen[1] - '1', queen[0] - 'a');
// get shortest path from knight -> rook -> queen
int path1 = shortestPath(knight_pos, rook_pos) + shortestPath(rook_pos, queen_pos);
// get shortest path from knight -> queen -> rook
int path2 = shortestPath(knight_pos, queen_pos) + shortestPath(queen_pos, rook_pos);
// return the minimum of path1 and path2
return min(path1, path2);
}
private:
// returns the shortest path length from the knight start position to the knight end position using BFS
int shortestPath(const Position& start, const Position& end)
{
// maintain a set of all visited positions
set<Position> visited;
// maintain a queue of all positions that we need to visit
// the queue will store a std::pair to capture the shortest path lengths
// for each position starting from the start position
queue<pair<Position, int>> q;
// add the start position to the queue to get started
q.push(make_pair(start, 0));
while (!q.empty())
{
// pop next position in the queue
auto cur = q.front();
q.pop();
// add this position to the set of visited positions
visited.insert(cur.first);
// if we have reached the end position, return the shortest path length for this position
if (cur.first == end)
return cur.second;
// otherwise get all valid knight moves from this position
auto moves = moveKnight(cur.first);
// push these knight moves into the queue if we have not visited those positions before
// their shortest path length will be +1 the current shortest path length
for (auto m : moves)
{
if (visited.find(m) == visited.end())
q.push(make_pair(m, cur.second + 1));
}
}
// just a fail-safe to return an arbitrarily large value (in terms of chess board moves)
return 64;
}
https://github.com/smalex-als/smalex-at-topcoder/blob/master/src/p200/srm207/CaptureThemAll.java
public int fastKnight(String knight, String rook, String queen) {
int[] coorsKnight = toCoords(knight);
int[] coorsRook = toCoords(rook);
int[] coorsQueen = toCoords(queen);
int[][] delta = new int[][]{
{-2, -1},
{-2, 1},
{2, -1},
{2, 1},
{-1, -2},
{1, -2},
{-1, 2},
{1, 2}
};
return Math.min(
countStep(coorsKnight, coorsRook, delta) + countStep(coorsRook, coorsQueen, delta),
countStep(coorsKnight, coorsQueen, delta) + countStep(coorsQueen, coorsRook, delta));
}
http://algoexplained.blogspot.com/2016/12/capturethemall-topcoder.html
public int fastKnight(String knight, String rook, String queen) {
boolean[][] joe = new boolean[8][8];
<8 i="" p=""><8 j="" p="">
int hx = Integer.valueOf(knight.substring(1))-1;
int hy = knight.charAt(0) - 'a';
joe[Integer.valueOf(rook.substring(1))-1][rook.charAt(0) - 'a'] = true;
joe[Integer.valueOf(queen.substring(1))-1][queen.charAt(0) - 'a'] = true;
LinkedList
queue.add(new Harry(hx,hy,0));
int[] captured = new int[4];
while (queue.size() > 0) {
Harry current = queue.removeFirst();
if (capture(current, joe, captured)) {
if (captured[0] != 1) {
captured[0] = 1;
captured[1] = current.move;
queue.clear();
queue.add(new Harry(current.x,current.y,current.move));
} else {
captured[2] = 1;
captured[3] = current.move;
}
}
if (captured[0] == 1 && captured[2] == 1)
return current.move;
addNeighbors(queue, current);
}
return -1;
}
public boolean capture(Harry harry, boolean[][] joe, int[] captured) {
if (joe[harry.x][harry.y]){
// only capture one in the same neighbors
if (!(captured[0] == 1 && captured[1] == harry.move)) {
joe[harry.x][harry.y] = false;
return true;
}
}
return false;
}
public void addNeighbors(LinkedList
int[] dx = {-2,-2,2,2,-1,1,-1,1};
int[] dy = {-1,1,-1,1,-2,-2,2,2};
for (int i=0;i<8 i="" p=""> int x = current.x + dx[i];
int y = current.y + dy[i];
if (x >=0 && y >= 0 && x < 8 && y <8 br=""> queue.add(new Harry(x,y,current.move + 1));
}
}
}
class Harry{
int x;
int y;
int move;
public Harry(int xx, int yy, int m) {
x = xx;
y = yy;
move = m;
}
}