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 ...
随机推荐
- Java 字典排序
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import org.ju ...
- UI-popup
做CRM有些时间了,可是对于UI却接触的不是很多,不过感觉UI和DYNPRO差不多...很类似的,...呵呵.加油吧! LV_TITLE = TEXT-T07.”POPUP TITLE LV_PO ...
- yii框架
Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一.Yii是创始人 ...
- 《Play for Java》学习笔记(三)template+Message
说明: 这是本书的第八章内容,由于项目需要,提到前面来看啦~~~O(∩_∩)O 一.模板template的定义 Play中的模板是html代码和Scala代码的混合而成的,其中Scala代码以@开头, ...
- ArangoDB介绍——未知架构和底层原理
ArangoDB介绍 ArangoDB是一个开源NoSQL数据库,官网:https://www.ArangoDB.org/ArangoDB支持灵活的数据模型,比如文档Document.图Graph以及 ...
- Cookie实例,理解cookie
一.一句话了解cookie是什么 cookie是服务端发送给客户端的.用来记录一些信息(如用户名),定制主页,聚焦广告的.最终以文件形式存在于客户端电脑磁盘下的小型文档. 二.用实例来认清cookie ...
- jQuery滚动条回到顶部或指定位置
jQuery滚动条回到顶部或指定位置 在很多网站,为了增强用户体验,我们会看到回到顶部的按钮,不用手动拖拽滚动条就能回到顶部,非常方便.下面就介绍用jquery实现的滚动到顶部的代码 $(functi ...
- mybatis使用
mybatis网站:http://mybatis.github.io/spring/zh/ mybatis spring下载网址:https://github.com/mybatis/spring/r ...
- bzoj 2730: [HNOI2012]矿场搭建
#include<cstdio> #include<cstring> #include<iostream> #define M 508 using namespac ...
- android程序打包成APK
1.下载ant(从官网上下载没有bin目录,可以直接在百度上搜APACHE-ANT-1.9.4-BIN.ZIP) 2.解压到C盘根目录(也可以是其他盘) 3.修改环境变量 ANT_HOME C: ...