ACM: DP+floyd 动态规划题 poj 117…

Camelot
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.
ACM: <wbr>DP+floyd <wbr>动态规划题 <wbr>poj <wbr>1178

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

题意: 1个国王和多个骑士, 求出国王和全部骑士在同一个点的最少时间, 已知国王和骑士的移动模式.
      当国王和其中一个骑士再同一个点时, 骑士可以带着国王一起移动(国王变成和骑士一样).

解题思路:
        1. 可以对国王和骑士的移动模式枚举出全部可以移动到的点.
        2. 用floyd计算出每个点之间的最短距离.
        3. 先枚举终点, 再枚举骑士和国王相遇的点. 计算最小值.

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#define MAX 8
const int INF = (1<<20);
int dirKing[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}};
int dirKnight[8][2] = {{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};

char str[MAX*MAX*2+5];
int king[MAX*MAX][MAX*MAX], knight[MAX*MAX][MAX*MAX];
int step[MAX*MAX], num;

inline int min(int a,int b)
{
    return a < b ? a : b;
}

void init()
{
    for(int i = 0; i < MAX*MAX; ++i)
        for(int j = 0; j < MAX*MAX; ++j)
            king[i][j] = knight[i][j] = (i == j ? 0 : INF);
    for(int i = 0; i < MAX*MAX; ++i)
    {
        int x = i/8;
        int y = i%8;
        for(int j = 0; j < 8; ++j)
        {
            int xx = x+dirKing[j][0];
            int yy = y+dirKing[j][1];
            if(xx >= 0 && xx < MAX && yy >= 0 && yy < MAX)
                king[i][8*xx+yy] = 1;
        }
       
        for(int j = 0; j < 8; ++j)
        {
            int xx = x+dirKnight[j][0];
            int yy = y+dirKnight[j][1];
            if(xx >= 0 && xx < MAX && yy >= 0 && yy < MAX)
                knight[i][8*xx+yy] = 1;
        }
    }
}

void floyd(int a[][MAX*MAX])
{
    for(int k = 0; k < MAX*MAX; ++k)
    {
        for(int i = 0; i < MAX*MAX; ++i)
        {
            for(int j = 0; j < MAX*MAX; ++j)
            {
                if(a[i][j] > a[i][k]+a[k][j])
                    a[i][j] = a[i][k]+a[k][j];
            }
        }
    }
}

int main()
{
//    freopen("input.txt","r",stdin);
    init();
    floyd(king);
    floyd(knight);
   
    while(scanf("%s",str) != EOF)
    {
        int start = (str[0]-'A') + (str[1]-'1')*8;
        int num = (strlen(str)-2)/2;
        if(num == 0)
        {
            printf("0\n");
            continue;
        }
       
        for(int i = 0, j = 2; i < num; ++i, j+=2)
            step[i] = (str[j]-'A') + (str[j+1]-'1')*8;
        int result = INF;
        int t1, t2;
        int sum;
        for(int i = 0; i < MAX*MAX; ++i)
        {
            sum = 0;
            for(int k = 0; k < num; ++k)
                sum += knight[ step[k] ][i];
            for(int j = 0; j < MAX*MAX; ++j)
            {
                t1 = king[start][j];
                t2 = INF;
                for(int kk = 0; kk < num; ++kk)
                {
                    t2 = min(t2,knight[ step[kk] ][j] + knight[j][i] - knight[ step[kk] ][i]); // 骑士从起始点到达j点接国王, 骑士和国王一起到达枚举的终点, 其中要减去sum已经加上的即可.
                }
                result = min(result,sum+t1+t2);
            }
        }
        printf("%d\n",result);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值