2013
11-09

Camelot

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 8×8 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.

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

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.

D4A3A8H1H8

10

//* @author:
import java.util.*;
public class Main {

static int dis[][][][]=new int[8][8][8][8];
static int x[]=new int[64],y[]=new int[64], n;

static void floyd()
{
int i1, i2, i3, j1, j2, j3, t;

for( i1=0; i1< 8; i1++ )
for( j1=0; j1< 8; j1++ )
for( i2=0; i2< 8; i2++ )
for( j2=0; j2< 8; j2++ )
for( i3=0; i3< 8; i3++ )
for( j3=0; j3< 8; j3++ )
if( ( t = dis[i2][j2][i1][j1] + dis[i1][j1][i3][j3] ) < dis[i2][j2][i3][j3] )
dis[i2][j2][i3][j3] = t;
}

static void init()
{
char w[]=new char[200];
int i,i1,i2,j1,j2;
Scanner in = new Scanner(System.in);
w=in.next().toCharArray();

for( i=0; i< w.length-1; i+= 2 ){
x[i/2] = w[i] - 'A';
y[i/2] = w[i+1] - '1';
}

n = i / 2;

for( i1=0; i1< 8; i1++ )
for( j1=0; j1< 8; j1++ )
for( i2=0; i2< 8; i2++ )
for( j2=0; j2< 8; j2++ )
if( ( Math.abs( i1 - i2 ) == 2 && Math.abs( j1 - j2 ) == 1  ) || ( Math.abs( i1 - i2 ) == 1 && Math.abs( j1 - j2 ) == 2 ) )
dis[i1][j1][i2][j2] = 1;
else if( i1 == i2 && j1 == j2 ) dis[i1][j1][i2][j2] = 0;
else dis[i1][j1][i2][j2] = 999;

floyd();
}

static void doit()
{
int i1, i2, j1, j2, k, ans, t, temp, h;

ans = 99999;
for( i1=0; i1< 8; i1++ )
for( j1=0; j1< 8; j1++ )
{
temp = 0;
for( k=1; k< n; k++ )
temp += dis[x[k]][y[k]][i1][j1];

for( k=1; k< n; k++ )
{
t = temp - dis[x[k]][y[k]][i1][j1];

for( i2=0; i2< 8; i2++ )
for( j2=0; j2< 8; j2++ )
{
h = t + dis[x[k]][y[k]][i2][j2] + dis[i2][j2][i1][j1] +
( Math.abs( x[0]-i2 ) > Math.abs( y[0]-j2 ) ? Math.abs( x[0]-i2 ) : Math.abs( y[0]-j2 ) );

if( h < ans ) ans = h;
}

}
}

System.out.printf( "%d\n", ans );
}

public static void main(String[] args){

init();
doit();
}
}

1. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法

2. a是根先忽略掉，递归子树。剩下前缀bejkcfghid和后缀jkebfghicd，分拆的原则的是每个子树前缀和后缀的节点个数是一样的，根节点出现在前缀的第一个，后缀的最后一个。根节点b出现后缀的第四个位置，则第一部分为四个节点，前缀bejk，后缀jkeb，剩下的c出现在后缀的倒数第2个，就划分为cfghi和 fghic，第3部分就为c、c