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

最新文章

  1. Spring中AOP(通知)的使用
  2. spring task 配置
  3. AndroidStudio 混淆打包
  4. [codeforces 55]D. Beautiful numbers
  5. 调试技巧--Windows端口号是否被占用
  6. viewpager+fragment+HorizontalScrollView详细版
  7. c 函数及指针学习 3
  8. eclipse:failed to create the java virtual machine
  9. 数值的整数次方(剑指offer面试题11)
  10. apache重写字段详细说明
  11. Java虚拟机内存优化实践
  12. USACO maze1 BFS
  13. [ An Ac a Day ^_^ ] CodeForces 339A Helpful Maths
  14. mysql一些函数的记录
  15. FreeMarker的用法
  16. 关键字(7):属性的增删改add,drop,modify
  17. C#.net mysql There is already an open datareader associated with this command引发的问题
  18. Java 身份证判断性别获取年龄
  19. HTML head 头标签(转)
  20. PHPUnit 组织测试

热门文章

  1. 整理github总结
  2. WIN10安装VC6.0无法使用的解决办法
  3. Superset 官方入门教程中文翻译
  4. Netty源码解析—客户端启动
  5. JavaFX 集成 Sqlite 和 Hibernate 开发爬虫应用
  6. Unity场景和代码合并以及UnityYAMLMerge的使用
  7. Go orm框架gorm学习
  8. Netty学习(一)-为什么选择Netty
  9. IDEA+maven搭建scala开发环境(spark)(半转载)
  10. JVM 内存模型概述