A rider is a fantasy chess piece that can jump like a knight several times in a single move. A rider that can perform a maximum of K jumps during a single move is denoted as a K-rider. For example, a 2-rider can jump once or twice during a single move, and a 1-rider is a traditional knight.

There are some riders of different types on a chessboard. You are given a 2D board representing the layout of the pieces. The jth character of the ith element of board is the content of the square at row i, column j. If the character is a digit K between '1' and '9', the square contains a K-rider. Otherwise, if the character is a '.', the square is empty. Find the minimal total number of moves necessary to move all the riders to the same square. Only one piece can move during each move. Multiple riders can share the same squares all times during the process. Print -1 if it is impossible.

A traditional knight has up to 8 moves from a square with coordinates (x, y) to squares (x+1, y+2), (x+1, y-2), (x+2, y+1), (x+2, y-1), (x-1, y+2), (x-1, y-2), (x-2, y+1), (x-2, y-1), and can't move outside the chessboard.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case begins with a blank line and two integers m, n (1 ≤ m, n ≤ 10) denoting the rows and the columns of the board respectively. Each of the next m lines will contain n integers each denoting the board.

Output

For each case of input you have to print the case number the desired result.

题意:矩阵中有一些骑士, 称为k-Rider, 一步跳k次, 问最少多少步,把所有骑士放到一个位置上。

由于这题数据比较小所以直接bfs各个骑士到所有点的最小值然后再一一比较即可

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int inf = 0X3f3f3f3f;
struct TnT {
int x , y , num;
};
int n , m , vis[30][30] , va[110][30][30] , dr[8][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1};
char map[30][30];
void bfs(int i , int j , int total , int count) {
memset(vis , 0 , sizeof(vis));
queue<TnT>q;
TnT p;
p.x = i , p.y = j , p.num = 0;
vis[p.x][p.y] = 1;
va[count][i][j] = 0;
q.push(p);
while(!q.empty()) {
TnT gg = q.front();
for(int i = 0 ; i < 8 ; i++) {
TnT gl = gg;
gl.x += dr[i][0];
gl.y += dr[i][1];
if(gl.x >= 0 && gl.x < n && gl.y >= 0 && gl.y < m) {
gl.num++;
int temp;
if(gl.num > total) {
if(gl.num % total == 0) {
temp = gl.num / total;
}
else {
temp = gl.num / total + 1;
}
}
else {
temp = 1;
}
if(vis[gl.x][gl.y] == 0) {
va[count][gl.x][gl.y] = temp;
q.push(gl);
}
if(vis[gl.x][gl.y] == 0) {
vis[gl.x][gl.y] = 1;
}
}
}
q.pop();
}
}
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--) {
memset(va , -1 , sizeof(va));
ans++;
cin >> n >> m;
int cnt = inf , count = 0;
for(int i = 0 ; i < n ; i++) {
cin >> map[i];
}
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(map[i][j] != '.') {
count++;
bfs(i , j , map[i][j] - '0' , count);
}
}
}
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
int sum = 0;
for(int l = 1 ; l <= count ; l++) {
sum += va[l][i][j];
if(va[l][i][j] == -1) {
sum = inf;
break;
}
}
cnt = min(cnt , sum);
}
}
cout << "Case " << ans << ": ";
if(cnt == inf) {
cout << -1 << endl;
}
else {
cout << cnt << endl;
}
}
return 0;
}

lightoj 1046 - Rider(bfs)的更多相关文章

  1. 【lightoj-1046】Rider(BFS)

    链接:http://www.lightoj.com/volume_showproblem.php?problem=1046 题意: 给m*n的棋盘,数字k代表这个位置上有棋子,并且一步可以连续跳1-k ...

  2. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  3. Lightoj 1174 - Commandos (bfs)

    题目链接: Lightoj  1174 - Commandos 题目描述: 有一军队秉承做就要做到最好的口号,准备去破坏敌人的军营.他们计划要在敌人的每一个军营里都放置一个炸弹.军营里有充足的士兵,每 ...

  4. 暑期训练狂刷系列——Lightoj 1084 - Winter bfs

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...

  5. loj 1046(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26766 思路:由于数据不是很大,我们可以枚举骑士最后聚集的位置,然 ...

  6. lightoj 1111 - Best Picnic Ever(dfs or bfs)

    题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111 题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚. 简单 ...

  7. LightOJ 1337 F - The Crystal Maze (bfs)

    Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...

  8. Lightoj 1066 Gathering Food (bfs)

    Description Winter is approaching! The weather is getting colder and days are becoming shorter. The ...

  9. LightOJ 1009 二分图染色+BFS/种类并查集

    题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...

随机推荐

  1. 【pycharm】pycharm远程连接服务器的Python解释器,远程编写代码!!!

    今天讲讲如何用pycharm连接远程服务器,使用远程服务器的Python解释器,比如说是你公司的服务器,在家里就可以编写或修改项目的代码! 第一步,先找到服务器上的ip地址 Linux查看IP命令:i ...

  2. HTML第六章 盒子模型

    什么是盒子模型: (1)边框: (2)内边距: (3)外边距: (4)元素内容·: (5)背景色·: 边框: 属性: 颜色(border-color),粗细(border-width),样式(bord ...

  3. 前端插件之Datatables使用--上篇

    工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,前边两篇分别介绍了Duallistbox插件和Select2插件的使用,这一篇开始Databases的征服之旅 D ...

  4. Java 通过反射改变私有变量的值

    直接上代码 import java.lang.reflect.Field; public class Main {      public static void main(String[] args ...

  5. elk系列教程:docker中安装配置elk

    elasticSearch Docker安装elasticsearch: docker pull docker.io/elasticsearch:7.2.0 启动: docker run -p 920 ...

  6. JAVA MQ API方式通信采用Binding MQ Server方式

    package com.mqapi;   /**  * @modified by actorai E-mail:actorai@163.com  * @version 创建时间:2010-9-15 * ...

  7. Redis集群与spring的整合

    上一篇详细的赘述了Redis的curd操作及集群的搭建.下面我们开始将他整合到我们实际的项目中去.我的项目采用的是标准的ssm框架,ssm框架这里不说,直接开始整合. 首先在maven管理中将我们的j ...

  8. Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别

    转载来源 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚. ...

  9. vue中的v-if和v-show的区别

    v-if和v-show的区别是前端面试中常问的基础知识点,v-if.v-show顾名思义就是用来判断视图层展示效果的.那么具体是怎么展示呢?v-if和v-show的区别又是什么呢? 首先我们可以来看一 ...

  10. js-获取屏幕的中各种宽高

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...