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. IQueryable与IQEnumberable的区别

    IEnumberable接口: 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用froeach遍历此object; IQueryable接口 ...

  2. 藏地传奇js

    http://zd.163.com/m/zhenyan/ js很厉害,有很多值得学习的地方,记录下来. http://res.nie.netease.com/zdcq/qt/13/0625_zheny ...

  3. OpenGl绘制螺旋线

    /** * 缓冲区工具类 */public class BufferUtil { /**  * 将浮点数组转换成字节缓冲区  */ public static ByteBuffer arr2ByteB ...

  4. 高并发非自增ID如何设计?

    博友们一起来讨论下高并发非自增ID如何设计? 底层是很重要的,我最近设计底层,通用底层. 我想跟大家谈论下这个话题: 如何在高并发环境下设计出一套好用的非自增ID的添加操作的解决方案?更新的操作我随机 ...

  5. 有关sort函数的用法

    最近碰到这个sort函数,网上查了一些资料,感觉还是直接扔给我代码比较好理解些 要是像我一样的童鞋,建议看这里:http://www.cplusplus.me/265.html 个人认为很好理解..这 ...

  6. VMware上安装ubuntu 13.04

    作者:viczzx 出处:http://www.cnblogs.com/zixuan-zhang 欢迎转载,也请保留这段声明.谢谢! 这两天打算在Linux环境下学Python语言,想换个高点的ubu ...

  7. [转]iOS: About diagnostic capabilities

    Source:http://support.apple.com/kb/HT6331 Each of these diagnostic capabilities requires the user to ...

  8. java遍历Set集合

    class Person{ private String name; private int age; public Person(String name,int age){ this.name = ...

  9. Scut游戏服务器免费开源框架-3

    Scut游戏服务器免费开源框架--快速开发(3) Scut快速开发(3) 1        开发环境 需要安装的软件 a)        消息队列 b)        数据库,Sql2005以上版本 ...

  10. storm源码之巧用java反射反序列化clojure的defrecord获取属性值

    [原创]storm源码之巧用java反射反序列化clojure的defrecord获取属性值 [原创]storm源码之巧用java反射反序列化clojure的defrecord获取属性值 storm源 ...