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

来源:互联网 发布:厦门淘宝代运营 编辑:程序博客网 时间:2024/05/22 00:17
Camelot
Description
Centuries ago, KingArthur and the Knights of the Round Table used to meet every yearon New Year's Day to celebrate their fellowship. In remembrance ofthese events, we consider a board game for one player, on which oneking and several knight pieces are placed at random on distinctsquares.
The Board is an 8x8 array of squares. The King can move to anyadjacent square, as shown in Figure 2, as long as it does not falloff the board. A Knight can jump as shown in Figure 3, as long asit 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 thesame square. The board squares are assumed big enough so that apiece 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 inthe same square, in the smallest possible number of moves. Toachieve this, he must move the pieces as prescribed above.Additionally, whenever the king and one or more knights are placedin the same square, the player may choose to move the king and oneof the knights together henceforth, as a single knight, up to thefinal gathering point. Moving the knight together with the kingcounts as a single move.

Write a program to compute the minimum number of moves the playermust perform to produce the gathering.

Input

Your program is toread from standard input. The input contains the initial boardconfiguration, encoded as a character string. The string contains asequence of up to 64 distinct board positions, being the first onethe position of the king and the remaining ones those of theknights. Each position is a letter-digit pair. The letter indicatesthe horizontal board coordinate, the digit indicates the verticalboard coordinate.

0 <= number of knights <= 63

Output

Your program is towrite to standard output. The output must contain a single linewith an integer indicating the minimum number of moves the playermust 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
原创粉丝点击