Attempting to solve it, swept me through the full range of emotions, going from cocky over-confidence to being nastily surprised to bewilderment to despair, frustration and finally to immense relief of eventually getting rid of it by solving it ! There was absolutely no elation at all on solving it, just massive relief, that's all !

The statement goes like this :

Centuries ago, King Arthur and the Knights of the Round Table used to meet every year on New Year's Day to celebrate their fellowship. In remembrance of these events, we consider a board game for one player, on which one chess piece king and several knight pieces are placed on squares, no two knights on the same square. During the play, the player can place more than one piece in the same square. The board squares are assumed big enough so that a piece is never an obstacle for any other piece to move freely. The player's goal is to move the pieces so as to gather them all in the same square - in the minimal number of moves. To achieve this, he must move the pieces as prescribed above. Additionally, whenever the king and one or more knights are placed in the same square, the player may choose to move the king and one of the knights together from that point on, as a single knight, up to the final gathering point. Moving the knight together with the king counts as a single move. Write a program to compute the minimum number of moves the player must perform to produce the gathering. The pieces can gather on any square, of course. PROGRAM NAME: camelot INPUT FORMAT Line 1: Two space-separated integers: R,C, the number of rows and columns on the board. There will be no more than 26 columns and no more than 30 rows. Line 2..end: The input file contains a sequence of space-separated letter/digit pairs, 1 or more per line. The first pair represents the board position of the king; subsequent pairs represent positions of knights. There might be 0 knights or the knights might fill the board. Rows are numbered starting at 1; columns are specified as upper case characters starting with `A'. SAMPLE INPUT (file camelot.in) 8 8 D 4 A 3 A 8 H 1 H 8 The king is positioned at D4. There are four knights, positioned at A3, A8, H1, and H8. OUTPUT FORMAT A single line with the number of moves to aggregate the pieces. SAMPLE OUTPUT (file camelot.out) 10 SAMPLE OUTPUT ELABORATION They gather at B5. Knight 1: A3 - B5 (1 move) Knight 2: A8 - C7 - B5 (2 moves) Knight 3: H1 - G3 - F5 - D4 (picking up king) - B5 (4 moves) Knight 4: H8 - F7 - D6 - B5 (3 moves) 1 + 2 + 4 + 3 = 10 moves.

You could assume that the King and Knight pieces move just like the standard chess moves. The board co-ordinate annotation is exactly the same too : The columns (A-Z) start go from left to right, while the rows (1 - 30) go from down to upwards.

Basically it requires full appreciation of breadth first search. To solve it, you need to use breadth first search in novel ways, tweaking it to suit the fact that the King and the Knight pieces can join/merge while moving and adjust shortest distances accordingly.

I struggled with the problem since initially I (wrongly) assumed that even two or more Knights could join just like a King and a Knight. That could make this problem even tougher. But as such, the join only happens between King and a Knight.

The input limits on the problem are very delicately adjusted to time-out the solutions that have even one excessive nested iteration. Suppose for 'R' rows, 'C' columns and 'P' pieces, the expected time complexity should be O(R*C*P) ... For the maximum input dimensions, it works out to be approximately 600K operations, should be comfortable for a time limit of 1 second. My initial solution was working OK, but had an extra iteration on number of pieces in the innermost loop, that was timing out my solution.

Finally, I got the code below, that does these things :

1. Records the distance to each square for the King.

2. For each square on the board, records the shortest path to every other square, using breadth first search, and saves all of it.

3. Tweaks breadth first search to record shortest path from each Knight to all other squares & assuming that this Knight picks up the King. This uses information stored in (1)

4. Iterates over the entire board, checking if the square can be the final gathering point. In the innermost loop, each Knight is examined if that Knight can pick up the King to reach the candidate gathering square in the cheapest way.

#include <fstream> #include <vector> #include <queue> #include <math.h> const int INF = 10000000; int min(int a, int b){if(a < b) return a; return b;} int max(int a, int b){if(a > b) return a; return b;} int kr[8] = {-2, -1, 1, 2, 2, 1, -1, -2}; int kc[8] = {1, 2, 2, 1, -1, -2, -2, -1}; class co_ords{ public: int r; int c; }; class Knight_King{ public: int for_knight; int for_king; int r; int c; }; void init(Knight_King dists[30][26], int rows, int cols, int king_row, int king_col){ for(int rr = 0; rr < rows; ++rr){ for(int cc = 0; cc < cols; ++cc){ dists[rr][cc].for_king = max(abs(rr - king_row), abs(cc - king_col)); dists[rr][cc].for_knight = INF; } } } class Knight_Cost{ public: Knight_King KK[30][26]; }; bool valid(int r, int c, int rows, int cols){ if(r >= 0 && c >= 0 && r < rows && c < cols) return true; return false; } void reset(int arr[30][26], int rows, int cols){ for(int x = 0; x < rows; ++x){ for(int y = 0; y < cols; ++y){ arr[x][y] = INF; } } } void knights_shortest(int dists[30][26], int rows, int cols, int r, int c, std::queue& q){ dists[r][c] = 0; co_ords rc; rc.r = r; rc.c = c; q.push(rc); int rr, cc; while(!q.empty()){ co_ords rc = q.front(); q.pop(); for(int i = 0; i < 8; ++i){ rr = rc.r + kr[i]; cc = rc.c + kc[i]; if(valid(rr, cc, rows, cols) && dists[rr][cc] == INF){ co_ords rrcc; rrcc.r = rr; rrcc.c = cc; dists[rr][cc] = min(dists[rr][cc], dists[rc.r][rc.c] + 1); q.push(rrcc); } } } } void knights_paths(Knight_King costs[30][26], int rows, int cols, int r, int c, std::queue & q){ costs[r][c].for_knight = 0; Knight_King rc; rc.r = r; rc.c = c; rc.for_king = costs[r][c].for_king; rc.for_knight = costs[r][c].for_knight; q.push(rc); int rr, cc; while(!q.empty()){ Knight_King rc = q.front(); q.pop(); for(int i = 0; i < 8; ++i){ rr = rc.r + kr[i]; cc = rc.c + kc[i]; if(valid(rr, cc, rows, cols)){ bool enq = ((min(rc.for_king, costs[rr][cc].for_king) + rc.for_knight + 1) < (costs[rr][cc].for_king + costs[rr][cc].for_knight)); if(enq){ Knight_King rrcc; rrcc.r = rr; rrcc.c = cc; rrcc.for_king = min(rc.for_king, costs[rr][cc].for_king); rrcc.for_knight = rc.for_knight + 1; costs[rr][cc].for_king = min(rc.for_king, costs[rr][cc].for_king); costs[rr][cc].for_knight = rrcc.for_knight; q.push(rrcc); } } } } } int get_sum(int dists[30][26], const std::vector & knights, int num_knights){ int ret = 0; for(int i = 0; i < num_knights; ++i){ ret += dists[knights[i].r][knights[i].c]; } return ret; } class square{ public: int dists[30][26]; square(int rows, int cols){ for(int i = 0; i < rows; ++i){ for(int j = 0; j < cols; ++j){ dists[i][j] = INF; } } } }; int main(){ int rows, cols; char col; std::vector knights; std::queue qkk; std::queue q; int row, num_knights = 0; int king_row, king_col; std::ifstream ifs("camelot.in"); ifs >> rows >> cols; ifs >> col >> row; king_col = (col - 'A'); king_row = (rows - row); while(ifs >> col){ ifs >> row; ++num_knights; co_ords rc; rc.r = rows - row; rc.c = col - 'A'; knights.push_back(rc); } ifs.close(); std::ofstream ofs("camelot.out"); if(num_knights == 0){ ofs << "0\n"; ofs.close(); return 0; } std::vector board[30]; for(int i = 0; i < 30; ++i){ for(int j = 0; j < 26; ++j){ square s(rows, cols); board[i].push_back(s); knights_shortest(board[i][j].dists, rows, cols, i, j, q); } } std::vector cost(num_knights); for(int x = 0; x < num_knights; ++x){ init(cost[x].KK, rows, cols, king_row, king_col); knights_paths(cost[x].KK, rows, cols, knights[x].r, knights[x].c, qkk); } int min_so_far = INF; int sum = INF, tmp_sum, sim_king_dist; for(int r = 0; r < rows; ++r){ for(int c = 0; c < cols; ++c){ sim_king_dist = max(abs(king_row - r), abs(king_col - c)); sum = get_sum(board[r][c].dists, knights, num_knights) + sim_king_dist; for(int k = 0; k < num_knights; ++k){ tmp_sum = sum; sum -= (sim_king_dist + board[r][c].dists[knights[k].r][knights[k].c]); sum += (cost[k].KK[r][c].for_king + cost[k].KK[r][c].for_knight); if(sum < min_so_far) min_so_far = sum; sum = tmp_sum; } } } ofs << min_so_far << "\n"; ofs.close(); return 0; }

A result of a rivetting journey over the weekend...

## No comments:

Post a Comment