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. Superset 官方入门教程中文翻译

    本文翻译自 Superset 的官方文档:Toturial - Creating your first dashboard 最新版本的 Superset 界面与功能上与文档中提到的会有些许出入,以实际 ...

  2. 前端本地proxy跨域代理配置

    等了好久的接口,总算拿到了,结果却发现用本地localhost:9712去请求接口的时候,出现了跨域错误,而这个时候我们就需要进行下跨域配置了. 首先,找到项目中名为webpack.config.js ...

  3. 利用MAVEN打包可运行jar包,包括依赖的第三方包

    转载自:http://bglmmz.iteye.com/blog/2058914 背景: 另一篇文章说了如何利用IDEA来打包,现在来说说如何利用MAVEN打包 目标:应用本身打成一个jar包,依赖的 ...

  4. C#的委托事件总结

    什么是委托?1.委托是C#中由用户自定义的一个类型.2.类表示的是数据和方法的集合,而委托实际上是一个能持有对某个或某些方法的引用的类.3.与其他的类不同,委托类能拥有一个签名,并且他只能持有与他的签 ...

  5. springboot-jsp打jar问题

    [**前情提要**]最近做了一个项目,项目是springboot+jsp结构的,但是在发布生产环境的时候又需要用maven打成jar包,但是一开始的默认配置都不成功.下面的文章就是具体的解决过程. - ...

  6. 转载 | float 清除浮动的7种方法

    什么叫浮动:浮动会使当前标签脱离文档流,产生上浮的效果,同时还会影响周边元素(前后标签)及父级元素的位置和width,height属性.下面用一个小例子来看一看浮动的全过程:1.首先我们新建一个网页, ...

  7. mongoDB的CRUD的总结

    今天开始接触非关系型数据库的mongoDB,现在将自己做的笔记发出来,供大家参考,也便于自己以后忘记了可以查看. 首先,mongoDB,是一种数据库,但是又区别与mysql,sqlserver.orc ...

  8. (三十三)c#Winform自定义控件-日期控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. EXP查询合集提权后渗透必备

    0x00 整理的一些后渗透提权需要用到的一些漏洞,后渗透提权的时候可以看一下目标机那些补丁没打,再进行下一步渗透提权. 0x01 CVE-2019-0803 [An elevation of priv ...

  10. CGI getenv 参数

    变量名 描述 CONTENT_TYPE 这个环境变量的值指示所传递来的信息的MIME类型.目前,环境变量CONTENT_TYPE一般都是:application/x-www-form-urlencod ...