USACO Section 3.3 Camlot(BFS)

时间:2022-09-25 00:16:57

USACO Section 3.3 Camlot(BFS)

BFS.先算出棋盘上每个点到各个点knight需要的步数;然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个结论?还是usaco数据太弱了?不过看跑出来的时间,全部枚举或许也可以))。一开始觉得挺麻烦的,不过只要思路清晰写起来应该也没多大问题。大概就是这样了.

---------------------------------------------------------------------------------------

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define dow(i,l,r) for(int i=l;i>=r;i--)
#define clr(x,c) memset(x,c,sizeof x)
using namespace std;
const int inf=0x3f3f3f3f,maxr=30+5,maxc=26+5;
const int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int d[maxr][maxc][maxr][maxc];
int r,c,num=0;
struct coor { int x,y; };
queue<coor> q;
coor king,knight[maxr*maxc];
void init() {
    cin>>r>>c;
    char p;
    int t;
    cin>>p>>t;
    king={t,p-'A'+1};
    while(cin>>p>>t) knight[++num]={t,p-'A'+1};
    
    clr(d,inf);
    rep(i,1,r) rep(j,1,c) {
        d[i][j][i][j]=0;
        q.push((coor){i,j});
        while(!q.empty()) {
            coor e=q.front(); q.pop();
            rep(k,0,7) {
                int x=e.x+dir[k][0];
                int y=e.y+dir[k][1];
                if(x<=0 || x>r || y<=0 || y>c) continue;
                if(d[i][j][x][y]==inf) {
                    d[i][j][x][y]=d[i][j][e.x][e.y]+1;
                    q.push((coor){x,y});
                }
            }
        }
    }
}
int s() {
    int ans=inf;
    int i=5,j=2;
    rep(i,1,r) rep(j,1,c) {
        int w=0,t=inf;
        rep(k,1,num) {
            coor e=knight[k];
            w+=d[i][j][e.x][e.y];
        }
        
        rep(a,max(king.x-2,1),min(king.x+2,r))
            rep(b,max(king.y-2,1),min(king.y+2,c)) 
                rep(k,1,num) {
                    coor e=knight[k];
                    int x=abs(king.x-a);
                    int y=abs(king.y-b);
                    t=min(t,d[i][j][a][b]+d[a][b][e.x][e.y]+x+y-min(x,y)-d[i][j][e.x][e.y]);
                }
        
        int x=abs(king.x-i);
        int y=abs(king.y-j);
        int h=min(x+y-min(x,y),t)+w;
        ans= h>=0 && h<ans ? h:ans;
    }
    return ans;
}
int main() {
    freopen("camelot.in","r",stdin);
    freopen("camelot.out","w",stdout);
    
    init();
    cout<<s()<<endl;
    
    return 0;
}

-------------------------------------------------------------------------------------------

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 Camlot(BFS)

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

USACO Section 3.3 Camlot(BFS)

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

USACO Section 3.3 Camlot(BFS)

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.

USACO Section 3.3 Camlot(BFS)的更多相关文章

  1. 【USACO 2&period;4】Overfencing(bfs最短路)

    H行W列的迷宫,用2*H+1行的字符串表示,每行最多有2*W+1个字符,省略每行后面的空格.迷宫的边界上有且仅有两个出口,求每个点出发到出口的最短路. +-+-+-+-+-+ | | +-+ +-+ ...

  2. USACO Section 1&period;3 题解 (洛谷OJ P1209 P1444 P3650 P2693&rpar;

    usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...

  3. nyoj 21三个水杯(BFS &plus; 栈)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...

  4. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 【BZOJ5492】&lbrack;HNOI2019&rsqb;校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

随机推荐

  1. JiaThis分享插件的使用

    jia This的下载地址:http://www.jiathis.com/ 只需要在页面上加上以下代码即可 <span class="jiathis_style"> & ...

  2. Tomcat 系统架构与设计模式,第 2 部分&colon; 设计模式分析

    门面设计模式 门面设计模式在 Tomcat 中有多处使用,在 Request 和 Response 对象封装中.Standard Wrapper 到 ServletConfig 封装中.Applica ...

  3. IOS 学习笔记&lpar;5&rpar; 控件 文本视图&lpar;UITextView&rpar;的使用方法

    相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...

  4. QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址

    获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...

  5. sqlite数据库的char&comma;varchar&comma;text&comma;nchar&comma;nvarchar&comma;ntext的区别

    1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间,不足的自动用空格填充. 2.VAR ...

  6. 关于串session

    关于session: 最近在用IE浏览器调试不同用户所拥有的权限功能,出现了串session.串session,主要的流程结构如下: 解决方法: 在IE快捷方式上点击鼠标右键>属性>快捷方 ...

  7. 系统间通信——RPC架构设计

    架构设计:系统间通信(10)——RPC的基本概念 1.概述经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的 ...

  8. Emgucv&lpar;二&rpar;Emgucv和Aforge录制视频

    一.Emgucv录制视频 Emgucv中的Capture类可以完成视频文件的读取,利用EmguCV播放视频的原理是:将视频看作图片,用capture获取抓取通道,通过不断的调用{frame = cap ...

  9. 初窥Linux之我最常用的20条命令

    1.cd命令   这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切 ...

  10. kibana5&period;6源码分析3--目录结构

    kibana5.6的项目目录结构: bin:系统启动脚本目录 config:kibana配置文件目录 data:估计是缓存一些系统数据的,uuid放在这里面 docs: maps:此目录包含TileM ...