博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1178 floyd+枚举
阅读量:5113 次
发布时间:2019-06-13

本文共 3824 字,大约阅读时间需要 12 分钟。

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 
#include
#include
#include
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

转载于:https://www.cnblogs.com/brucemengbm/p/6740831.html

你可能感兴趣的文章
sql server必知多种日期函数时间格式转换
查看>>
ListView如何获取点击单元格内容
查看>>
jQuery EasyUI 的下拉选择combobox后台动态赋值
查看>>
(转)游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)
查看>>
timeline时间轴进度“群英荟萃”
查看>>
python map函数用法
查看>>
ios之申请后台延时执行和做一个假后台的方法(系统进入长时间后台后,再进入前台部分功能不能实现)...
查看>>
编码命名规范
查看>>
耿丹16-1上半学期助教总结
查看>>
python if else elif statement
查看>>
网络编程
查看>>
文本隐藏(图片代替文字)
查看>>
three.map.control
查看>>
二叉树的深度
查看>>
java面试题
查看>>
提高码力专题(未完待续)
查看>>
IOS第17天(3,Quartz2D画板和画线)
查看>>
pair的例子
查看>>
前端框架性能对比
查看>>
@property中 retain 详解
查看>>