http://poj.org/problem?id=1178

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
/**
poj 1178 floyd+枚举
题目大意:在一个8*8的棋盘里有一个国王和一些骑士,我们须要把他们送到同一顶点上去,骑士和国王的行动方式如图所看到的。国王能够选择一名骑士作为坐骑。上马后相当和该骑士
一起行动(相当于一个骑士),同一位置能够同一时候有多个骑士和国王。问最少走的步数
解题思路:把8*8棋盘变成0~63的数,Floyd求出随意两点之间的最短路径。8*8枚举就可以。枚举终点,骑士上马点,国王上哪个骑士,终于负责度O(64^4)。
*/
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std; char s[105];
int cx[8][2]= {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int dx[8][2]= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int a[65][65],b[65][65],rking[65],king; bool judge(int i,int j)
{
if(i>=0&&i<8&&j>=0&&j<8)
return true;
return false;
} void init()
{
for(int i=0; i<64; i++)
{
for(int j=0; j<64; j++)
{
if(i==j)
a[i][j]=b[i][j]=0;
else
a[i][j]=b[i][j]=999;
}
}
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
for(int k=0; k<8; k++)
{
int x=i+cx[k][0];
int y=j+cx[k][1];
int xx=i+dx[k][0];
int yy=j+dx[k][1];
if(judge(x,y))
{
a[i+j*8][x+y*8]=1;
}
if(judge(xx,yy))
{
b[i+j*8][xx+yy*8]=1;
}
}
}
}
for(int k=0; k<64; k++)
{
for(int i=0; i<64; i++)
{
for(int j=0; j<64; j++)
{
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
b[i][j]=min(b[i][j],b[i][k]+b[k][j]);
}
}
}
}
int main()
{
init();
while(~scanf("%s",s))
{
int n=strlen(s);
king=s[0]-'A'+(s[1]-'1')*8;
int cnt=0;
for(int i=2; i<n; i+=2)
{
int x=s[i+1]-'1';
int y=s[i]-'A';
rking[cnt++]=x*8+y;
}
int ans=9999999;
for(int i=0;i<64;i++)///终点
{
for(int j=0;j<64;j++)///国王上马点
{
for(int k=0;k<cnt;k++)///国王所上的骑士
{
int sum=0;
for(int l=0;l<cnt;l++)
{
if(l==k)continue;
sum+=a[rking[l]][i];
}
sum+=b[king][j]+a[rking[k]][j]+a[j][i];
ans=min(ans,sum);
}
}
}
printf("%d\n",ans);
}
return 0;
}

poj1178 floyd+枚举的更多相关文章

  1. poj 1161 Floyd+枚举

    题意是: 给出n个点,围成m个区域.从区域到另一个区域间需穿过至少一条边(若两区域相邻)——边连接着两点. 给出这么一幅图,并给出一些点,问从这些点到同一个区域的穿过边数最小值. 解题思路如下: 将区 ...

  2. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  3. floyd最短路

    floyd可以在O(n^3)的时间复杂度,O(n^2)的空间复杂度下求解正权图中任意两点间的最短路长度. 本质是动态规划. 定义f[k][i][j]表示从i出发,途中只允许经过编号小于等于k的点时的最 ...

  4. ZOJ 1232 【灵活运用FLOYD】 【图DP】

    题意: copy自http://blog.csdn.net/monkey_little/article/details/6637805 有A个村子和B个城堡,村子标号是1~A,城堡标号是A+1~B.马 ...

  5. 简单的floyd——初学

     前言: (摘自https://www.cnblogs.com/aininot260/p/9388103.html): 在最短路问题中,如果我们面对的是稠密图(十分稠密的那种,比如说全连接图),计算多 ...

  6. 套题T3

    秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  7. bzoj千题计划123:bzoj1027: [JSOI2007]合金

    http://www.lydsy.com/JudgeOnline/problem.php?id=1027 因为x+y+z=1,所以z=1-x-y 第三维可以忽略 将x,y 看做 平面上的点 简化问题: ...

  8. code1167 树网的核

    floyd+枚举 看点: 1.floyd同时用数组p记录转移节点k,这样知道线段的端点u v就可以得到整条线段 2.任意一点c到线段a b的距离=(d[a][c]+d[c][b]-d[a][b])/2 ...

  9. APIO2017

    商旅 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号,集市之间通过M条 ...

随机推荐

  1. django-BBS(1)

    1.首先分析BBS的设计需要,然后设计相应的数据库.填写在models.py 中 2.修改setting.py中的内容: a.将appname加入INSTALLED_APP中 b.修改DATABASE ...

  2. FZU 2297 Number theory【线段树/单点更新/思维】

    Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide. Input First l ...

  3. LibreOJ #139 树链剖分 [树链剖分,线段树]

    题目传送门 树链剖分 题目描述 这是一道模板题. 给定一棵 n 个节点的树,初始时该树的根为 1 号节点,每个节点有一个给定的权值.下面依次进行 m 个操作,操作分为如下五种类型: 换根:将一个指定的 ...

  4. 【Floyd】文化之旅

    [NOIP2012]文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一 种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不 ...

  5. 倒置数组 Exercise07_12

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:倒置数组 * */ public class Exercise07_12 ...

  6. Java NIO:Buffer、Channel 和 Selector

    Buffer 一个 Buffer 本质上是内存中的一块,我们可以将数据写入这块内存,之后从这块内存获取数据. java.nio 定义了以下几个 Buffer 的实现,这个图读者应该也在不少地方见过了吧 ...

  7. Problem C: 零起点学算法82——数组中查找数

    #include<stdio.h> int main(void) { ],m; while(scanf("%d",&n)!=EOF) { ;i<n;i++ ...

  8. Java源代码编译过程

      编译其本质是将一种语言规范转换成另一种语言规范,即将Java语言规范转换为JVM虚拟机语言规范.结果就是.java文件到.class文件. 对于C/C++编译直接将高级语言转换为机器语言,Java ...

  9. python的dict和set

    dict dict是dictionary的缩写,python内置了字典,在其他语言中也称为map,使用键值对储存,具有极快的查找速度. 如果是只用list来实现,就需要两个list,先在第一个list ...

  10. [转]115个Java面试题和答案——终极列表(上)

    本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的章节分为上下两篇,第一篇将要讨论面向对象编程和它的特点,关于Java和它的功能 ...