Description
Centuries ago, King Arthur and the Knights of the Round Table used to meet every year in 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 is P Laced at the 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 of 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 is assumed big enough so a piece was 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 MO Ves. To achieve this, he must move the pieces as prescribed above. Additionally, whenever the king and one or more knights is placed in the same square, the player could choose to move the K ING 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 was 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 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 PR Oduce the Gathering.
Sample Input
D4a3a8h1h8
Sample Output
10
Test instructions, there is a king and N Knights in a 8*8 chessboard, the king can go to the neighboring 8 points, the Knight walk the surname, ask the last to go to the same lattice need a few steps, pay attention to the king once with a knight encounter, they are a whole, according to Knight's method to move
Idea: First use Floyd to find the shortest path of all knights and kings from various points to other points, then enumerate the collection points and enumerate which Knights and Kings met
<pre name= "code" class= "CPP" > #include <iostream> #include <stdio.h> #include <string.h># Include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm>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 (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) # Define LL long longconst double pi = ACOs ( -1.0); #define Len 63#define mod 19999997const int INF = 0x3f3f3f3f;const int to1 [8] [2] = {1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,1,-1,-1};const int to2[8][2] = {1,2,2,1,1,-2,2,-1,-1,2,-2,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,0,7) {up (k,0,7) {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; }}} (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<len; i+=2) saber[l++] = (str[i]-' A ') *8+ (str[i+1]-' 1 '); Up (I,0,len) {up (J,0,len) {sum = King[arthur][j];//Suppose the king encounters a knight up at J position (K,0,L-1) sum+=knight[saber[k]][i];//assume that all knights are assembled at point I, adding up the number of steps for all knights to this point tem = INF; Up (K,0,L-1)//enumeration is the K knight and the King meet, then this actually must first move to J Point, and then take the silly King to return to I point Tem=min (tem,sum-knight[saber[k]][i]+knight[saber[k]][ J]+knight[j][i]); ans = min (ans,tem); }} printf ("%d\n", ans); } return 0;}
Poj1178:camelot (FLOYD+DP)