USACO section Camelot(枚举+队列优化)

博客介绍了Camelot棋盘游戏,玩家需将国王和骑士聚集在同一格子上,用最少的步数。通过修改最短路径算法,考虑骑士是否携带国王移动,对所有可能的聚集点进行计算,找到总移动次数最小的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

思路:先求出王到所有点的最少时间,骑士到每个点的最少时间,然后可以骑士带着王走,骑士的时间假设其不变,只更新王所到点的时间。最后枚举所有点加和最小就是答案。

/*
ID:nealgav1
LANG:C++
PROG:camelot
*/
#include<fstream>
#include<cstring>
#include<queue>
using namespace std;
ifstream cin("camelot.in");
ofstream cout("camelot.out");
const int dx[]={1,1,-1,-1,2,2,-2,-2};
const int dy[]={2,-2,2,-2,1,-1,1,-1};
const int oo=1e9;
class node {
   public:
    int x, y;
   void data(int a,int b){x=a,y=b;}
};
int R, C;
int cost[26][30];
int kcost[26][30];
int dist[26][30];
int kdist[26][30];
int kkdist[26][30];    
bool vis[26][30];
bool isKnight[26][30];

void bfs(int sx, int sy)
{
    //int steps = 0;
    int x, y;
    node p;
    memset(vis, false, 30*26);
    memset(dist, 0, sizeof(int)*30*26);
    queue<node> Q;
    p.data(sx,sy);
    Q.push(p);
    //dist[sx][sy] = 1;
    vis[sx][sy] = true;

    //bfs alone
    while (!Q.empty()) {
        p = Q.front(); Q.pop();
        for (int i = 0; i < 8; i++) {
            x = p.x+dx[i];
            y = p.y+dy[i];
            // check if the next node is vaild
            if (x>=0 && x<C && y>=0 && y<R && !vis[x][y]) {
                vis[x][y] = true;
                node s;s.data(x,y);
                Q.push(s);
                dist[x][y] = dist[p.x]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值