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();
}

最新文章

  1. curl 或 file_get_contents 获取需要授权页面的方法
  2. 手动给控制器添加xib
  3. Cisco 防止SYN Flood 攻击原理
  4. C#学习笔记(第1周作业)
  5. 如何编译libcurl
  6. python的方法总结:
  7. iOS子线程操作UI问题检查
  8. HDOJ 1056 HangOver(水题)
  9. 【原创】mdk5宏定义的使用小结
  10. windbg指定SOS版本,执行扩展命令报错
  11. 使用window.performance分析web前端性能
  12. 具体CAS操作实现(无锁算法)
  13. 用html+css+js实现选项卡切换效果
  14. 关于*** WARNING L15: MULTIPLE CALL TO SEGMENT
  15. 微软正式开源Blazor,将.NET带回到浏览器
  16. Chapter3 (字符串,向量,数组) --C++Prime笔记
  17. 温故vue对vue计算属性computed的分析
  18. mysql的索引和执行计划
  19. Shellinabox on centos6.9
  20. 关于JS中判断是数字和小数的正则表达式用法

热门文章

  1. Machine Learning - XI. Machine Learning System Design机器学习系统的设计(Week 6)
  2. 基于.NET Socket Tcp的发布-订阅框架
  3. Oracle入门4-REF Cursor
  4. asp.net mvc请求响应模型原理回顾
  5. BitMap画图
  6. 看AngularJS
  7. html+js+ashx+easyui+ado.net权限管理系统
  8. c# in deep 之使用匿名方法的内联委托操作
  9. Redis for Windows(C#缓存)配置文件详解
  10. 苹果APNs’ device token特性和过期更新