array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 USACO Section 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:

USACO Section 3.3 Camelot(搜索)

The King can move to any adjacent square from USACO Section 3.3 Camelot(搜索) to USACO Section 3.3 Camelot(搜索) as long as it does not fall off the board:

USACO Section 3.3 Camelot(搜索)

A Knight can jump from USACO Section 3.3 Camelot(搜索) to USACO Section 3.3 Camelot(搜索), as long as it does not fall off the board:

USACO Section 3.3 Camelot(搜索)

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. 

题意:在一个 n x m 的棋盘中,有一个国王和很多个骑士,国王和骑士在棋盘上的行走规则如上图,现在国王和所有的骑士要去一个格子了集合,国王和其中一个骑士在一个格子里相遇时,只算骑士的运动步数,现在问国王和所有的骑士的步数和最小值是多少。

分析:先BFS求两两间的最短路存在d[x1][y1][x2][y2],(x1,y1) 到(x2,y2)的最短路,然后枚举骑士和国王结合,枚举在国王的附件的+-2集合。

View Code
/*
  ID: dizzy_l1
  LANG: C++
  TASK: camelot
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#define MAXN 31
#define INF 0xfffffff

using namespace std;

struct point
{
    int x,y,step;
};
struct pos
{
    int x,y;
};
int pp1[8][2]=
{
    {-2,1},{-1,2},{1,2},{2,1},
    {2,-1},{1,-2},{-2,-1},{-1,-2},
};
int pp2[25][2]=
{
    {0,0},
    {1,1},{1,0},{1,-1},{0,-1},
    {-1,-1},{-1,0},{-1,1},{0,1},
    {2,2},{2,1},{2,0},{2,-1},
    {2,-2},{1,-2},{0,-2},{-1,-2},
    {-2,-2},{-2,-1},{-2,0},{-2,1},
    {-2,2},{-1,2},{0,2},{1,2},
};
pos knight[MAXN*MAXN],king;
int d[MAXN][MAXN][MAXN][MAXN];
bool visited[MAXN][MAXN];
int n,m,cnt;

inline int ABS(int a)
{
    if(a<0) return -a;
    return a;
}

void BFS(point s)
{
    int i;
    point t,tt;
    queue<point>Q;
    memset(visited,false,sizeof(visited));
    Q.push(s);
    visited[s.x][s.y]=true;
    while(!Q.empty())
    {
        t=Q.front();
        Q.pop();
        d[s.x][s.y][t.x][t.y]=t.step;
        tt.step=t.step+1;
        for(i=0; i<8; i++)
        {
            tt.x=t.x+pp1[i][0];
            tt.y=t.y+pp1[i][1];
            if(tt.x<1||tt.x>n||tt.y<1||tt.y>m)  continue;
            if(!visited[tt.x][tt.y])
            {
                Q.push(tt);
                visited[tt.x][tt.y]=true;
            }
        }
    }
}

int count_ans(int p,int q)
{
    pos t;
    int sum,tsum,ans,i,j;
    ans=max(ABS(king.x-p),ABS(king.y-q));
    tsum=0;
    for(i=0;i<cnt;i++)
        tsum+=d[p][q][knight[i].x][knight[i].y];
    ans+=tsum;
    for(i=0;i<cnt;i++)
    {
        for(j=0;j<25;j++)
        {
            sum=tsum;
            t.x=king.x+pp2[j][0];
            t.y=king.y+pp2[j][1];
            if(t.x<1||t.x>n||t.y<1||t.y>m)  continue;
            sum-=d[p][q][knight[i].x][knight[i].y];
            sum+=d[knight[i].x][knight[i].y][t.x][t.y];
            sum+=d[p][q][t.x][t.y];
            sum+=max(ABS(pp2[j][0]),ABS(pp2[j][1]));
            if(ans>sum) ans=sum;
        }
    }
    return ans;
}

void work()
{
    int i,j,t,ans=INF;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            t=count_ans(i,j);
            if(ans>t) ans=t;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    freopen("camelot.in","r",stdin);
    freopen("camelot.out","w",stdout);
    int i,j;
    char ch;
    point s;
    scanf("%d %d",&n,&m);
    scanf("%s%d",&ch,&king.x);
    king.y=ch-'A'+1;
    cnt=0;
    while(~scanf("%s%d",&ch,&knight[cnt].x)) knight[cnt++].y=ch-'A'+1;
    int a1,b1,a2,b2;
    for(a1=1;a1<=n;a1++) for(b1=1;b1<=m;b1++)
    for(a2=1;a2<=n;a2++) for(b2=1;b2<=m;b2++)
        d[a1][b1][a2][b2]=INF;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            s.x=i;s.y=j;s.step=0;
            BFS(s);
        }
    }
    work();
    return 0;
}

相关文章: