Camelot_floyd&&DP
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3119 | Accepted: 1455 |
Description
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
0 <= number of knights <= 63
Output
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的更多相关文章
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 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 ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- px、dp和sp,这些单位有什么区别?
DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...
- android px转换为dip/dp
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...
随机推荐
- Word Search [LeetCode]
Problem Description: http://oj.leetcode.com/problems/word-search/ Basic idea: recursively go forward ...
- 集成 WeChat SDK - 支付篇
作者感言 惯例, 开头都是要说些东西的了, 随着现在的App越来越商业化, 很多公司的App都会集成第三方的支付SDK, 这样子的成本就比较低, 但是呢, 有很多朋友还是不太会集成, 也不太爱看集成文 ...
- 读书笔记3 Socket
Socket被称为网络插座.用于两个网络应用程序之间的通信. 通信地址:URI 通过协议,地址,端口号可以确定网络上的一个程序.地址和端口号组合称之为端点. 通常会有发信人通信地址,收信人通信地址这两 ...
- Vim经典讲解
http://blog.csdn.net/niushuai666/article/details/7275406
- 多线程相关Interlocked.Increment问题
今天群里有人问到如下代码打印出来的东西为什么不是连续得,所以有大神解释了原因.在这过程中遇到了些奇怪的情况 static void Main(string[] args) { for (int i = ...
- 采用SHELL,通过SQL LOAD导入一定格式的txt文件至数据库中
1. 准备工作,window中可直接通过sqlload直接导入文件,linux下,需要有sqlload的相关软件. 2. SQL脚本(MID_DFDZ.ctl) LOAD DATA INTO TABL ...
- lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法
参考: http://www.slideshare.net/lucenerevolution/what-is-inaluceneagrandfinal http://www.slideshare.ne ...
- c++实现之 -- 文章TF-IDF值的计算
首先,是关键词的选取: 好吧这个我这模型实在是太简单了,但还是讲一讲比较好呢... 我们现在手头有的是一堆百度百科词条w的DF(w, c)值,c是整个百科词条...原因是...方便嘛~(而且人家现成的 ...
- BZOJ2490 Zombie’s Treasure Chest
如果n = lcm(s1, s2),那么就可以直接得到maxV = (v / s1 * v1, v / s2 *v2) 然后还剩下一点体积我们暴力枚举用s1的量,让s1为max(s1, s2)可以减少 ...
- 闲聊CSS之关于clearfix--清除浮动[转]
.clearfix:after { content: " "; display: block; clear: both; height:; } .clearfix { zoom:; ...