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. javascript图片延迟加载(转载)

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  2. RPC服务的发布订阅实现Thrift

    Thrift 个人实战--RPC服务的发布订阅实现(基于Zookeeper服务) 前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的 ...

  3. Ant 随想

    Ant是一种基于Java的build工具 面向任务构建,属性与于shell脚本中命令功能类型. <?xml version="1.0"?> <project na ...

  4. VS2012下systemC配置

    一.编译System库 1.下载SystemC library source code               到http://www.systemc.org注册会员账号后,即可下载SystemC ...

  5. Vim插件之插件管理器Vundle

    Vim插件之插件管理器Vundle 1.介绍下载 相比Sublime.Text2等现代编辑器,Vim缺乏默认的插件管理器,所有插件的文件都散布在~/.vim下的几个文件夹中,配置Vim的过程, 就是在 ...

  6. 分享一个c#写的开源分布式消息队列equeue

    分享一个c#写的开源分布式消息队列equeue 前言 equeue消息队列中的专业术语 Topic Queue Producer Consumer Consumer Group Broker 集群消费 ...

  7. java使用maven创建springmvc web项目

    创建maven项目,使用maven-archetype-webapp 创建完成后首先是在pom.xml里增加maven的依赖 <dependencies> <dependency&g ...

  8. WPF界面按钮美化

    在App.xaml里加入全局按钮样式 <Application x:Class="WpfButton.App" xmlns="http://schemas.micr ...

  9. MVC与EasyUI结合增删改查

    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查   在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的 ...

  10. JavaScript修改Canvas图片

    用JavaScript修改Canvas图片的分辨率(DPI)   应用场景: 仓库每次发货需要打印标签, Canvas根据从数据库读取的产品信息可以生成标签JPG, 但是这个JPG图片的默认分辨率(D ...