Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3119   Accepted: 1455

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

【题意】有一个国王和n个骑士在一个8*8的棋盘里,国王可以往邻近的8个点走,骑士走日字姓,问最后都走到同一个格子需要几步,其中国王一旦与一个骑士相遇之后,他们就是一个整体,按骑士的方法来移动。

【思路】先求出棋盘上任意一格到另一格的国王走法和骑士走法的最少步数存储在两个二维数组中,每格的表示方法i*8+j;

再假设国王与一个骑士在j格相遇,所有人集合在i,tmp=min(tmp,sum-knight[hh[k]][i]+knight[hh[k]][j]+knight[j][i]);

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
const int di1[][] = {,,,,-,,,-,,,,-,-,,-,-};
const int di2[][] = {,,,,,-,,-,-,,-,,-,-,-,-};
int king[][],knight[][];
void floyd()
{
for(int k=;k<;k++)
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
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]);
}
}
}
}
void init()//求出棋盘上任意一格到另一格的最短路径
{
memset(king,inf,sizeof(king));
memset(knight,inf,sizeof(knight));
for(int i=;i<;i++)
king[i][i]=knight[i][i]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
int x1=i+di1[k][];
int y1=j+di1[k][];
int x2=i+di2[k][];
int y2=j+di2[k][];
if(x1>=&&x1<&&y1>=&&y1<)
king[i*+j][x1*+y1]=;
if(x2>=&&x2<&&y2>=&&y2<)
knight[i*+j][x2*+y2]=;
}
}
}
floyd();
}
int main()
{
char str[];
int hh[],kk,ans;
init();
while(~scanf("%s",str))
{
int len=strlen(str);
int cnt=;
ans=inf;
kk=(str[]-'A')*+(str[]-'');//坐标用这种方法记录,这样开一个二维数组就可以搞定了
for(int i=;i<len;i+=)
hh[cnt++]=(str[i]-'A')*+(str[i+]-'');
int sum,tmp;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
sum=king[kk][j];//king与一个骑士在j这一格相遇了
for(int k=;k<cnt;k++)
{
sum+=knight[hh[k]][i];//所有骑士集合在i;
}
tmp=inf;
for(int k=;k<cnt;k++)
{
tmp=min(tmp,sum-knight[hh[k]][i]+knight[hh[k]][j]+knight[j][i]);//找出最初与国王相遇的骑士,使终点为i时的步数最小
ans=min(tmp,ans);
}
}
}
cout<<ans<<endl;
}
return ;
}

Camelot_floyd&&DP的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

随机推荐

  1. Visual Studio 2012出现“无法访问T-SQL组件和安装了不兼容伯 DacFx版本”的解决办法

    参考:Visual Studio 2012出现“无法访问T-SQL组件和安装了不兼容伯 DacFx版本”的解决办法 Vs2012的下载地址: https://msdn.microsoft.com/en ...

  2. think in java 读书笔记 2 —— 套接字

    目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 套接字基本知识 2 ...

  3. jQuery学习小结2——动画

    一.基础动画 方法名 说明 show([speed,[easing],[fn]])hide([speed,[easing],[fn]]) speed:三种预定速度之一的字符串("slow&q ...

  4. 使用Lucene.Net管理索引实现搜索

    之前使用一直是没有问题的,只到今天发现删除的时候无法删除,增加的时候却一直在增加,导致搜索的时候可以搜出来很多相同的结果. 小猪决定趁今天这个机会好好的把这个问题给解决了. private void ...

  5. Mysqldump参数大全

    Mysqldump参数大全(参数来源于mysql5.5.19源码)   参数 参数说明 --all-databases  , -A 导出全部数据库. mysqldump  -uroot -p --al ...

  6. Swift - 自动布局库SnapKit的使用详解1(配置、使用方法、样例)

    为了适应各种屏幕尺寸,iOS 6后引入了自动布局(Auto Layout)的概念,通过使用各种 Constraint(约束)来实现页面自适应弹性布局. 在 StoryBoard 中使用约束实现自动布局 ...

  7. S1 : 传递参数

    ECMAScript 中所有函数的参数都是按值传递的.也就是说,把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另一个变量一样.基本类型值的传递如同基本类型变量的复制一样,而引用类型值的传 ...

  8. windbg调试C#代码(一)

    用windbg调试C#代码是比较麻烦的,因为windbg是针对OS层级的,而C#被CLR隔了一层,很多原生的命令如查看局部变量dv.查看变量类型dt等在CLR的环境中都不能用了.必须使用针对CLR的扩 ...

  9. 获取Android系统的版本号

    int currentVersion = android.os.Build.VERSION.SDK_INT;

  10. 使用OCI向Oracle插入Geometry数据

    使用C/C++操作Oracle数据库,使用OCI可谓是最强大,当然也是最难的方式.Oracle是一个功能复杂而强大的数据库,它可以很好的支持空间数据(Oracle spatial).如何使用OCI向O ...