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.

————————————————————————

看着这道题第一眼觉得我见过,好像是枚举,然后n3挂得毫无疑问

嗯其实是最短路问题,网上说的枚举都是简化题面了,和华容道有点类似【华容道多加了一维方向做状态】,就是要多加一维带王或者不带王的状态做spfa

第一步处理王到棋盘的所有最短路

第二步处理某个骑士到棋盘各处的最短路的同时,加一维状态0/1记录骑士是否带王,使用堆优,每次更新0的时候顺带就更新1【也就是设某个点p,我们初始把所有最短路刷成inf后,第一次更新1就相当于王到p的最短路和骑士到p的最短路之和,也就是shortest_path_of_knight[p][0]+shortest_path_of_king[p]】

超级快,最慢不到0.3s

两行老泪……终于AC了……

 /*
ID: ivorysi
PROG: camelot
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
#define MAXN 400005
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int sp[][];
int knivis[][];
int vis[][];
int ksp[];
int kingcost[];
int r,c;
int ky[]={,,,,-,-,-,-};
int kx[]={,,-,-,-,-,,};
int kj[]={,,,,-,-,-,};
int ki[]={,,-,-,-,,,};
struct node {
int rr,cc,step;
};
queue<node> q;
void pre1(int u) {
memset(vis,,sizeof(vis));
int cx=(u-)%c+,rx=(u-)/c+;
while(!q.empty()) q.pop();
q.push((node){rx,cx,});
vis[rx][cx]=;
while(!q.empty()) {
node nw=q.front();q.pop();
kingcost[(nw.rr-)*c+nw.cc]=ksp[(nw.rr-)*c+nw.cc]=nw.step;
siji(i,,) {
int z=nw.rr+ki[i],w=nw.cc+kj[i];
if(z<=r&&z>=&&w<=c&&w>=&& vis[z][w]==) {
vis[z][w]=;
q.push((node){z,w,nw.step+});
}
}
}
}
struct data{
int rr,cc,cking,step;
bool operator < (const data &rhs) const{
return step>rhs.step;
}
};
priority_queue<data> q1;
void spfa(int u) {
memset(sp,inf,sizeof(sp));
memset(knivis,,sizeof(vis));
while(!q1.empty()) q1.pop();
int cx=(u-)%c+,rx=(u-)/c+;
q1.push((data){rx,cx,,});
sp[u][]=;
knivis[u][]=;
while(!q1.empty()) {
data nw=q1.top();q1.pop();
siji(i,,) {
int z=nw.rr+kx[i],w=nw.cc+ky[i];
if(z<=r&&z>=&&w<=c&&w>=) {
if(sp[(z-)*c+w][nw.cking]>nw.step+) {
sp[(z-)*c+w][nw.cking]=nw.step+;
if(!knivis[(z-)*c+w][nw.cking]){
knivis[(z-)*c+w][nw.cking]=;
q1.push((data){z,w,nw.cking,nw.step+});
}
}
}
}
if(nw.cking== && sp[(nw.rr-)*c+nw.cc][]>nw.step+ksp[(nw.rr-)*c+nw.cc]) {
sp[(nw.rr-)*c+nw.cc][]=nw.step+ksp[(nw.rr-)*c+nw.cc];
if(!knivis[(nw.rr-)*c+nw.cc][]){
knivis[(nw.rr-)*c+nw.cc][]=;
q1.push((data){nw.rr,nw.cc,,sp[(nw.rr-)*c+nw.cc][]});
}
}
knivis[(nw.rr-)*c+nw.cc][nw.cking]=;
} }
int king,kni[],cnt;
int dist[];
void init(){
scanf("%d%d",&r,&c);
char str[];int b;
char s;
scanf("%s%d",str,&b);
siji(i,,) if(str[i] >='A'&&str[i]<='Z') {s=str[i];break;}
king=(b-)*c+s-'A'+;
while(){
scanf("%s %d",str,&b);
if(b==) break;
siji(j,,) if(str[j] >='A'&&str[j]<='Z') {s=str[j];break;}
kni[++cnt]=(b-)*c+s-'A'+;
b=;
}
pre1(king); }
void solve() {
init();
siji(i,,cnt) {
spfa(kni[i]);
siji(j,,r*c) {
if(sp[j][]==inf||dist[j]==-) {dist[j]=-;continue;}
dist[j]+=sp[j][];
kingcost[j]=min(kingcost[j],sp[j][]-sp[j][]);
}
}
int ans=inf;
siji(j,,r*c) {
if(dist[j]==-) continue;
ans=min(kingcost[j]+dist[j],ans);
}
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("camelot.in","r",stdin);
freopen("camelot.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}

USACO 3.3 Camelot的更多相关文章

  1. 洛谷P1930 亚瑟王的宫殿 Camelot

    P1930 亚瑟王的宫殿 Camelot 19通过 53提交 题目提供者JOHNKRAM 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 很久以前,亚瑟王和 ...

  2. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  3. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  4. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  5. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  6. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  7. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  8. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  9. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

随机推荐

  1. leetcode[90] Decode Ways

    题目:如下对应关系 'A' -> 1 'B' -> 2 ... ‘Z’ -> 26 现在给定一个字符串,返回有多少种解码可能.例如:Given encoded message &qu ...

  2. .NET MVC通过反射获取数据修

    .NET MVC通过反射获取数据修 折磨了我一个晚上的问题,奈何对物理的反射印象太深了,整天去想着物理的反射.折射怎么解.感谢少将哥哥给我的指点,经过一个晚上对反射的恶补,最终搞定了.纪念一下. 1. ...

  3. MVC框架的插件与拦截器基础

    自制MVC框架的插件与拦截器基础 上篇谈到我自己写的MVC框架,接下来讲讲插件及拦截器! 在处理一些通用的逻辑最好把它封装一个插件或者拦截器,以便日后可以直接拿过来直接使用.在我的框架中可以通过继承以 ...

  4. C#自带组件

    C#自带组件 在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 贯穿所有的日志系统 日志系统,往往是贯穿一个程序的所有代码的; ...

  5. c语言可变参函数探究

    一.什么是可变长参数 可变长参数:顾名思义,就是函数的参数长度(数量)是可变的.比如 C 语言的 printf 系列的(格式化输入输出等)函数,都是参数可变的.下面是 printf 函数的声明: in ...

  6. 网​址​U​R​L​中​特​殊​字​符​转​义​编​码

    网址URL中特殊字符转义编码 字符 - URL编码值 空格 - %20 " - %22 # - %23 % - %25 & - %26 ( - %28 ) - %29 + - %2B ...

  7. redis 中文存储乱码问题

    在redis 中存储中文,读取会出现乱码(其实不是乱码,只是不是我们存的中文显示) redis> set test "我们" OK redis> get test &q ...

  8. 记录下关于SQL Server的东西

    CTE之所以与其他表表达式不同,是因为它支持递归查询: 定义一个递归CTE,至少需要两个查询(或者更多),第一个查询称为定位点成员(anchor member),第二个查询称为递归成员(recursi ...

  9. Web开发框架对比

    Web开发框架能极大地提升开发效率,下面对一些常用的框作一些更深层次的对比. JSF JSF不是极好的快速开发原型,代码生成不是内置功能,并且原型开发应用需要开发完整应用程序那么多的配置.这真的不是J ...

  10. Dragon Balls(hdu3635带权并查集)

    题意:n个城市有n个龙珠,T  a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次. 注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回 ...