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 ...
随机推荐
- linq to xml学习
http://www.cnblogs.com/greatverve/archive/2010/07/09/linq-to-xml-add-delete-update-query.html 记录一下,别 ...
- json小例子
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 如何为Kafka集群选择合适的Partitions数量
转载:http://blog.csdn.net/odailidong/article/details/52571901 这是许多kafka使用者经常会问到的一个问题.本文的目的是介绍与本问题相关的一些 ...
- mysql 索引长度限制
MyISAM存储引擎引键的长度综合不能超过1000字节 InnoDB单列索引长度不能超过767bytes,联合索引还有一个限制是3072
- ORACLE 日期比较
oracle sql日期比较:在今天之前: select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm ...
- C++ STL pair
没有找到priority_queue里存放pair不用typedef的方法...大概第一次觉得这个有用吧... 优先队列里和sort函数对pair 的默认排序是first从小到大,second从小到大 ...
- NimBus一个好的开发框架
NimbusKit是一个非常适合有经验的开发人员使用的iOS开发框架,具备完整的文档,并且提供了模块化的方式来解决iOS开发中的各种不同需求.最重要的是,该框架会经常添加一些新的组件和功能. Nimb ...
- HookIAT的启动程序
// 启动程序.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...
- 关于http断点续传相关的RANGE这个header
<?php //1.txt内容"1234567890" socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=0-0\r\ ...
- LCD驱动 15-3
测试:1:make menuconfig去掉原来的驱动程序 Device Drivers ---> Graphics support ---> ...