USACO Section 3.3 Camelot(搜索)

Camelot
IOI 98

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 chesspiece king and several knight pieces are placed on squares, no two knights on the same square.

This example board is the standard 8x8 array of squares:

The King can move to any adjacent square from  to  as long as it does not fall off the board:

A Knight can jump from  to , 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 any 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 minimal 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 from that point on, 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. The pieces can gather on any square, of course.

PROGRAM NAME: camelot

INPUT FORMAT

Line 1: Two space-separated integers: R,C, the number of rows and columns on the board. There will be no more than 26 columns and no more than 30 rows.
Line 2..end: The input file contains a sequence of space-separated letter/digit pairs, 1 or more per line. The first pair represents the board position of the king; subsequent pairs represent positions of knights. There might be 0 knights or the knights might fill the board. Rows are numbered starting at 1; columns are specified as upper case characters starting with `A'.

SAMPLE INPUT (file camelot.in)

8 8
D 4
A 3 A 8
H 1 H 8

The king is positioned at D4. There are four knights, positioned at A3, A8, H1, and H8.

OUTPUT FORMAT

A single line with the number of moves to aggregate the pieces.

SAMPLE OUTPUT (file camelot.out)

10

SAMPLE OUTPUT ELABORATION

They gather at B5. 
Knight 1: A3 - B5 (1 move) 
Knight 2: A8 - C7 - B5 (2 moves) 
Knight 3: H1 - G3 - F5 - D4 (picking up king) - B5 (4 moves) 
Knight 4: H8 - F7 - D6 - B5 (3 moves) 
1 + 2 + 4 + 3 = 10 moves. 

题意:在一个 n x m 的棋盘中,有一个国王和很多个骑士,国王和骑士在棋盘上的行走规则如上图,现在国王和所有的骑士要去一个格子了集合,国王和其中一个骑士在一个格子里相遇时,只算骑士的运动步数,现在问国王和所有的骑士的步数和最小值是多少。

分析:先BFS求两两间的最短路存在d[x1][y1][x2][y2],(x1,y1) 到(x2,y2)的最短路,然后枚举骑士和国王结合,枚举在国王的附件的+-2集合。

View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: camelot
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#define MAXN 31
#define INF 0xfffffff

using namespace std;

struct point
{
    int x,y,step;
};
struct pos
{
    int x,y;
};
int pp1[8][2]=
{
    {-2,1},{-1,2},{1,2},{2,1},
    {2,-1},{1,-2},{-2,-1},{-1,-2},
};
int pp2[25][2]=
{
    {0,0},
    {1,1},{1,0},{1,-1},{0,-1},
    {-1,-1},{-1,0},{-1,1},{0,1},
    {2,2},{2,1},{2,0},{2,-1},
    {2,-2},{1,-2},{0,-2},{-1,-2},
    {-2,-2},{-2,-1},{-2,0},{-2,1},
    {-2,2},{-1,2},{0,2},{1,2},
};
pos knight[MAXN*MAXN],king;
int d[MAXN][MAXN][MAXN][MAXN];
bool visited[MAXN][MAXN];
int n,m,cnt;

inline int ABS(int a)
{
    if(a<0) return -a;
    return a;
}

void BFS(point s)
{
    int i;
    point t,tt;
    queue<point>Q;
    memset(visited,false,sizeof(visited));
    Q.push(s);
    visited[s.x][s.y]=true;
    while(!Q.empty())
    {
        t=Q.front();
        Q.pop();
        d[s.x][s.y][t.x][t.y]=t.step;
        tt.step=t.step+1;
        for(i=0; i<8; i++)
        {
            tt.x=t.x+pp1[i][0];
            tt.y=t.y+pp1[i][1];
            if(tt.x<1||tt.x>n||tt.y<1||tt.y>m)  continue;
            if(!visited[tt.x][tt.y])
            {
                Q.push(tt);
                visited[tt.x][tt.y]=true;
            }
        }
    }
}

int count_ans(int p,int q)
{
    pos t;
    int sum,tsum,ans,i,j;
    ans=max(ABS(king.x-p),ABS(king.y-q));
    tsum=0;
    for(i=0;i<cnt;i++)
        tsum+=d[p][q][knight[i].x][knight[i].y];
    ans+=tsum;
    for(i=0;i<cnt;i++)
    {
        for(j=0;j<25;j++)
        {
            sum=tsum;
            t.x=king.x+pp2[j][0];
            t.y=king.y+pp2[j][1];
            if(t.x<1||t.x>n||t.y<1||t.y>m)  continue;
            sum-=d[p][q][knight[i].x][knight[i].y];
            sum+=d[knight[i].x][knight[i].y][t.x][t.y];
            sum+=d[p][q][t.x][t.y];
            sum+=max(ABS(pp2[j][0]),ABS(pp2[j][1]));
            if(ans>sum) ans=sum;
        }
    }
    return ans;
}

void work()
{
    int i,j,t,ans=INF;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            t=count_ans(i,j);
            if(ans>t) ans=t;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    freopen("camelot.in","r",stdin);
    freopen("camelot.out","w",stdout);
    int i,j;
    char ch;
    point s;
    scanf("%d %d",&n,&m);
    scanf("%s%d",&ch,&king.x);
    king.y=ch-'A'+1;
    cnt=0;
    while(~scanf("%s%d",&ch,&knight[cnt].x)) knight[cnt++].y=ch-'A'+1;
    int a1,b1,a2,b2;
    for(a1=1;a1<=n;a1++) for(b1=1;b1<=m;b1++)
    for(a2=1;a2<=n;a2++) for(b2=1;b2<=m;b2++)
        d[a1][b1][a2][b2]=INF;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            s.x=i;s.y=j;s.step=0;
            BFS(s);
        }
    }
    work();
    return 0;
}
posted @ 2012-09-17 14:09  mtry  阅读(632)  评论(0编辑  收藏  举报