USACO 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.
————————————————————————
看着这道题第一眼觉得我见过,好像是枚举,然后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的更多相关文章
- 洛谷P1930 亚瑟王的宫殿 Camelot
P1930 亚瑟王的宫殿 Camelot 19通过 53提交 题目提供者JOHNKRAM 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 很久以前,亚瑟王和 ...
- USACO Section 3.3 Camlot(BFS)
BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...
- 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 ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
随机推荐
- CSS知识点:清除浮动
开场白 我们平时工作中,很容易遇到浮动效果.一个DIV中嵌套多个DIV,左边显示一个DIV,右边显示一个DIV,外层DIV的高度随着内层内容的高度变化.这就是最基本的浮动效果.下图的百度搜索结果就是一 ...
- 关于easyui的tab,layout,datagrid嵌套的问题
我的项目使用easyui作为前台的展示框架现在页面中是一个layout布局(分上,左,中)在左边是一些菜单,点击后,在中间部分增加一个tab显示内容而增加的tab里面是显示一些列表数据,列表上面是查询 ...
- Office文档在线编辑的实现之一
因为项目的关系,研究了一下Office的在线编辑功能,写出来共享一下. Office xp之后的版本支持通过webdav协议(http的扩展)直接编辑服务器上的文件. IIS(6.0)支持webdav ...
- 自己写CPU第九阶段(5)——实现负载存储指令2(改变运行阶段)
我们会继续上传新书<自己动手写CPU>.今天是第42篇.我尽量每周四篇,可是近期已经非常久没有实现这个目标了,一直都有事.不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9% ...
- 如何防范CC攻击
服务器如何防范CC攻击CC攻击是DDOS(分布式拒绝服务)的一种,相比其它的DDOS攻击CC似乎更有技术含量一些.这种攻击你见不到虚假IP,见不到特别大的异常流量,但造成服务器无法进行正常连接,听说一 ...
- JAVA 异常 throw 与 throws
最近一直throw和throw new …… 获取头部罢工,要彻底生气清楚这件事,他对这个思想精华收集了很多网友.这里摘录. throws全部异常信息throw则是指抛出的一个详细的异常类型.通常在一 ...
- asp.net mvc上传图片案例
1.放在ajax.BeginForm里,不好使,同asp.net 表单中 fileupload控件不支持ajax差不多吧,如果异步的话可以借助jquery.form.js,很方便 2. //上传文件 ...
- mvc3项目如何在IIS7.5上发布的
若项目拿到服务器上发布,必须要安装MVC3的安装包! 1.在vs中打开你要发布的项目,右键属性找到发布 2.弹出发布web对话框,选择<新建配置文件...> 在弹出的对话框中输入一个配置文 ...
- Step one : 熟悉HTML
//H1 1 <html> <head> <title>BeiJing</title> </head> <body> <h ...
- 大数据工具篇之Hive与HBase整合完整教程
大数据工具篇之Hive与HBase整合完整教程 一.引言 最近的一次培训,用户特意提到Hadoop环境下HDFS中存储的文件如何才能导入到HBase,关于这部分基于HBase Java API的写入方 ...