POJ1178: Camelot (FLOYD + DP)
Description
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 king and several knight pieces are placed at random on distinct squares.
The Board is an 8x8 array of squares. the King can move to any adjacent square, as shown in Figure 2, as long as it does not fall off the board. A Knight can jump as shown in Figure 3, as long as it does not fall off the board.
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 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 smallest possible 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 henceforth, 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.
Input
Your program is to read from standard input. the input contains the initial board configuration, encoded as a character string. the string contains a sequence of up to 64 distinct board positions, being the first one the position of the king and the remaining ones those of the knights. each position is a letter-digit pair. the letter indicates the horizontal board coordinate, the digit indicates the vertical board coordinate.
0 <= number of knights <= 63
Output
Your program is to write to standard output. The output must contain a single line with an integer indicating the minimum number of moves the player must perform to produce the gathering.
Sample Input
D4A3A8H1H8
Sample Output
10
There is a king and n knights in a 8x8 board. The king can go to the neighboring eight o'clock. The knight takes the Japanese surname, it takes a few steps to go to the same grid at the end of the question. Note that once the king meets a server guard, they are a whole and move them as a server guard.
Train of Thought: first use floyd to find the shortest path between all the server guard and the King from each point to other points, then enumerate the meeting points, and enumerate which server guard and the king meet
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include using namespace std; # define ls 2 * I # define rs 2 * I + 1 # define up (I, x, y) for (I = x; I <= y; I ++) # define down (I, x, y) for (I = x; I> = y; I --) # define mem (, x) memset (a, x, sizeof (a) # define w (a) while (a) # define LL long const double pi = acos (-1.0 ); # define Len 63 # define mod 19999997 const int INF = 0x3f3f3f3f; const int to1 [8] [2] = {,-, 0, -1,-,-1,-1}; const int to2 [8] [2] = {, 1,-,-1,-,-1,-2,-2,-1}; int king [65] [65], knight [65] [65]; int main () {int I, j, k; mem (king, INF); mem (knight, INF); up (I, 0, len) king [I] [I] = knight [I] [I] = 0; int x1, x2, y1, y2; up (I, 0, 7) {up (j,) {up (k,) {x1 = I + to1 [k] [0]; y1 = j + to1 [k] [1]; x2 = I + to2 [k] [0]; y2 = j + to2 [k] [1]; if (x1> = 0 & x1 <8 & y1> = 0 & y1 <8) king [I * 8 + j] [x1*8 + y1] = 1; if (x2> = 0 & x2 <8 & y2> = 0 & y2 <8) knight [I * 8 + j] [x2*8 + y2] = 1 ;}} up (k, 0, Len) {up (I, 0, Len) {up (j, 0, Len) {king [I] [j] = min (king [I] [j], king [I] [k] + king [k] [j]); knight [I] [j] = min (knight [I] [j], knight [I] [k] + knight [k] [j]);} char str [1000]; int len; int arthur, saber [100], l, ans, tem, sum; w (~ Scanf ("% s", str) {len = strlen (str); l = 0; ans = INF; arthur = (str [0]-'A ') * 8 + (str [1]-'1'); for (I = 2; I