poj 1178 Camelot(floyed+动规)

Camelot

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Problem 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

题意:在一个8*8的棋盘上面,让国王和骑士都到达同一个点,问最少几步?
国王可以上下左右斜面八个方向走和骑士走八个‘日’方向,当骑士遇到国王时,国王按骑士的方式一起走。

枚举所有的点,求出国王和骑士到达每个点的步数,然后取最小步数点。

用Floyd拉伸,求出每个点的最短路,先枚举i个点,先求出所有骑士到达i点花费,然后假设国王和骑士在j点相遇,那么就要减去国王congj到达i点的距离,(因为这段路是骑士带着走的),然后加上骑士从k点到j点再到i点的距离。这样求出的最短距离点就是我们的最终结果。

#include<cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#define INF 0x3f3f3f3f
#define N 64
using namespace std;

int dp[110][110];
int map[110][110];
int dir_king[8][2]= {1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,1,-1,-1};
int dir_knight[8][2]= {1,2,2,1,1,-2,2,-1,-1,2,-2,1,-1,-2,-2,-1};

int check(int x,int y)
{
    if(x<0||x>=8||y<0||y>=8)
    {
        return 0;
    }
    return 1;
}
int main()
{
    int king[110][110];
    int knight[110][110];
    memset(king,INF,sizeof(king));
    memset(knight,INF,sizeof(knight));
    for(int i=0; i<N; i++)
        king[i][i]=knight[i][i]=0;
    for(int i=0; i<8; i++)
    {
        for(int j=0; j<8; j++)
        {
            for(int k=0; k<8; k++)
            {
                int x1=i+dir_king[k][0];
                int y1=j+dir_king[k][1];
                int x2=i+dir_knight[k][0];
                int y2=j+dir_knight[k][1];
                if(check(x1,y1))
                {
                    king[i*8+j][x1*8+y1]=1;
                }
                if(check(x2,y2))
                {
                    knight[i*8+j][x2*8+y2]=1;
                }
            }
        }
    }
    for(int k=0; k<N; k++)
    {
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<N; j++)
            {
                king[i][j]=min(king[i][j],king[i][k]+king[k][j]);
                knight[i][j]=min(knight[i][j],knight[i][k]+knight[k][j]);
            }
        }
    }
    char str[1000];
    int len;
    int din_knight[110];
    while(~scanf("%s",str))
    {
        len=strlen(str);
        //cout<<len<<endl;
        int l=0;
        int ans=INF;
        int sum=0;
        int din_king=(str[0]-'A')*8+(str[1]-'1');
        for(int i=2; i<len; i+=2)
        {
            din_knight[l++]=(str[i]-'A')*8+(str[i+1]-'1');
        }
        int temp;
        //cout<<temp<<endl;
        for(int i=0; i<N; i++)///枚举所有的点
        {
            for(int j=0; j<N; j++)
            {
                sum=king[din_king][j];///国王在j位置遇到骑士
                for(int k=0; k<l; k++)          
                {
                    sum+=knight[din_knight[k]][i];///所有骑士在i集合
                }
                temp=INF;
                for(int k=0; k<l; k++)
                {
                    temp=min(temp,sum-knight[din_knight[k]][i]+knight[din_knight[k]][j]+knight[j][i]);
                }
                ans=min(ans,temp);
                //cout<<ans<<endl;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值