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

  1. D4A3A8H1H8

Sample Output

  1. 10
  1. /**
  2. poj 1178 floyd+枚举
  3. 题目大意:在一个8*8的棋盘里有一个国王和一些骑士,我们须要把他们送到同一顶点上去,骑士和国王的行动方式如图所看到的。国王能够选择一名骑士作为坐骑。上马后相当和该骑士
  4. 一起行动(相当于一个骑士),同一位置能够同一时候有多个骑士和国王。问最少走的步数
  5. 解题思路:把8*8棋盘变成0~63的数,Floyd求出随意两点之间的最短路径。8*8枚举就可以。枚举终点,骑士上马点,国王上哪个骑士,终于负责度O(64^4)。
  6. */
  7. #include <string.h>
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <stdio.h>
  11. using namespace std;
  12.  
  13. char s[105];
  14. int cx[8][2]= {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
  15. int dx[8][2]= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
  16. int a[65][65],b[65][65],rking[65],king;
  17.  
  18. bool judge(int i,int j)
  19. {
  20. if(i>=0&&i<8&&j>=0&&j<8)
  21. return true;
  22. return false;
  23. }
  24.  
  25. void init()
  26. {
  27. for(int i=0; i<64; i++)
  28. {
  29. for(int j=0; j<64; j++)
  30. {
  31. if(i==j)
  32. a[i][j]=b[i][j]=0;
  33. else
  34. a[i][j]=b[i][j]=999;
  35. }
  36. }
  37. for(int i=0; i<8; i++)
  38. {
  39. for(int j=0; j<8; j++)
  40. {
  41. for(int k=0; k<8; k++)
  42. {
  43. int x=i+cx[k][0];
  44. int y=j+cx[k][1];
  45. int xx=i+dx[k][0];
  46. int yy=j+dx[k][1];
  47. if(judge(x,y))
  48. {
  49. a[i+j*8][x+y*8]=1;
  50. }
  51. if(judge(xx,yy))
  52. {
  53. b[i+j*8][xx+yy*8]=1;
  54. }
  55. }
  56. }
  57. }
  58. for(int k=0; k<64; k++)
  59. {
  60. for(int i=0; i<64; i++)
  61. {
  62. for(int j=0; j<64; j++)
  63. {
  64. a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
  65. b[i][j]=min(b[i][j],b[i][k]+b[k][j]);
  66. }
  67. }
  68. }
  69. }
  70. int main()
  71. {
  72. init();
  73. while(~scanf("%s",s))
  74. {
  75. int n=strlen(s);
  76. king=s[0]-'A'+(s[1]-'1')*8;
  77. int cnt=0;
  78. for(int i=2; i<n; i+=2)
  79. {
  80. int x=s[i+1]-'1';
  81. int y=s[i]-'A';
  82. rking[cnt++]=x*8+y;
  83. }
  84. int ans=9999999;
  85. for(int i=0;i<64;i++)///终点
  86. {
  87. for(int j=0;j<64;j++)///国王上马点
  88. {
  89. for(int k=0;k<cnt;k++)///国王所上的骑士
  90. {
  91. int sum=0;
  92. for(int l=0;l<cnt;l++)
  93. {
  94. if(l==k)continue;
  95. sum+=a[rking[l]][i];
  96. }
  97. sum+=b[king][j]+a[rking[k]][j]+a[j][i];
  98. ans=min(ans,sum);
  99. }
  100. }
  101. }
  102. printf("%d\n",ans);
  103. }
  104. return 0;
  105. }

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. 如何通过 Redis 实现分布式锁

    分布式锁需要解决的问题: 互斥性:任意时刻只能有一个客户端获取锁 安全性:锁只能被持有该锁的客户端删除 死锁:获取锁的客户端因为意外宕机未能释放锁,其他客户端再也无法获取到该锁导致死锁 容错:宕机后客 ...

  2. 04、Unity 5--全局光照技术

    本文整理自Unity全球官方网站,原文:UNITY 5 - LIGHTING AND RENDERING 简介全局光照,简称GI,是一个用来模拟光的互动和反弹等复杂行为的算法,要精确的仿真全局光照非常 ...

  3. 【JAVA】在线程里使用线程外的变量为什么一定要是final类型

    这个情况真的碰到很多,开始的时候也很难理解,但是既然IDE提示要final那我就final咯,跑通就行管那么多呢.然而这并不是科学的学习方法,万一面试问你呢那不是倒了大霉. OK,看了一些

  4. 谜题54:Null与Void

    下面仍然是经典的Hello World程序的另一个变种.那么,这个变种将打印什么呢? public class Null { public static void greet() { System.o ...

  5. Beaglebone Black教程Beaglebone Black的引脚分配

    Beaglebone Black教程Beaglebone Black的引脚分配 Beaglebone Black的引脚分配 绝大多数的微型开发平台都提供了一些称为GPIO的输入输出端口.这些端口可以让 ...

  6. Sass和Compass的安装

    Sass和Compass都是基于Ruby编程语言的命令行工具.要使用它们,你首先需要在电脑中安装Ruby,并对电脑的命令行操作有一个基本的理解.Sass和Compass可以安装在Windows.Mac ...

  7. [BZOJ 1901] Dynamic Rankings

    Link: BZOJ 1901 传送门 Solution: 带修改主席树的模板题 对于静态区间第$k$大直接上主席树就行了 但加上修改后会发现修改时复杂度不满足要求了: 去掉/增加第$i$位上的值时要 ...

  8. [NOIP2015] D1T2 信息传递

    洛谷题目链接:https://www.luogu.org/problemnew/show/2661 一道有很多种解法的题目 通过划归,发现就是求最小环 那么立即能想到的算法:1.Tarjan求强连通分 ...

  9. [BZOJ1003](ZJOI 2006) 物流运输trans

    [题目描述] 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟 ...

  10. ORACLE启动 切换实例命令

    启动服务器的其他实例 export ORACLE_SID=数据库实例名 sqlplus /nolog conn /as sysdba select name from v$database; !lsn ...