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各个骑士到所有点的最小值然后再一一比较即可

  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. const int inf = 0X3f3f3f3f;
  6. struct TnT {
  7. int x , y , num;
  8. };
  9. 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};
  10. char map[30][30];
  11. void bfs(int i , int j , int total , int count) {
  12. memset(vis , 0 , sizeof(vis));
  13. queue<TnT>q;
  14. TnT p;
  15. p.x = i , p.y = j , p.num = 0;
  16. vis[p.x][p.y] = 1;
  17. va[count][i][j] = 0;
  18. q.push(p);
  19. while(!q.empty()) {
  20. TnT gg = q.front();
  21. for(int i = 0 ; i < 8 ; i++) {
  22. TnT gl = gg;
  23. gl.x += dr[i][0];
  24. gl.y += dr[i][1];
  25. if(gl.x >= 0 && gl.x < n && gl.y >= 0 && gl.y < m) {
  26. gl.num++;
  27. int temp;
  28. if(gl.num > total) {
  29. if(gl.num % total == 0) {
  30. temp = gl.num / total;
  31. }
  32. else {
  33. temp = gl.num / total + 1;
  34. }
  35. }
  36. else {
  37. temp = 1;
  38. }
  39. if(vis[gl.x][gl.y] == 0) {
  40. va[count][gl.x][gl.y] = temp;
  41. q.push(gl);
  42. }
  43. if(vis[gl.x][gl.y] == 0) {
  44. vis[gl.x][gl.y] = 1;
  45. }
  46. }
  47. }
  48. q.pop();
  49. }
  50. }
  51. int main()
  52. {
  53. int t;
  54. cin >> t;
  55. int ans = 0;
  56. while(t--) {
  57. memset(va , -1 , sizeof(va));
  58. ans++;
  59. cin >> n >> m;
  60. int cnt = inf , count = 0;
  61. for(int i = 0 ; i < n ; i++) {
  62. cin >> map[i];
  63. }
  64. for(int i = 0 ; i < n ; i++) {
  65. for(int j = 0 ; j < m ; j++) {
  66. if(map[i][j] != '.') {
  67. count++;
  68. bfs(i , j , map[i][j] - '0' , count);
  69. }
  70. }
  71. }
  72. for(int i = 0 ; i < n ; i++) {
  73. for(int j = 0 ; j < m ; j++) {
  74. int sum = 0;
  75. for(int l = 1 ; l <= count ; l++) {
  76. sum += va[l][i][j];
  77. if(va[l][i][j] == -1) {
  78. sum = inf;
  79. break;
  80. }
  81. }
  82. cnt = min(cnt , sum);
  83. }
  84. }
  85. cout << "Case " << ans << ": ";
  86. if(cnt == inf) {
  87. cout << -1 << endl;
  88. }
  89. else {
  90. cout << cnt << endl;
  91. }
  92. }
  93. return 0;
  94. }

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. Confluence未授权模板注入/代码执行(CVE-2019-3396)

    --- title: Confluence未授权模板注入/代码执行(CVE-2019-3396) tags: [poc,cve] num :g7y12 --- # 简介 --- Confluence是 ...

  2. 基于TP5.1实用案例及教程

    推荐<基于TP5.1实用案例及教程>书 目录: 通用封装 Export通用封装Import通用封装配合Import通用封装的ImportBaseVerify类Files通用封装Direct ...

  3. c#小灶——注释和代码规范

    为什么要写注释? 早上我写完了代码,我和我的朋友们都能看懂,到了晚上,我还能看懂,一周后,就只有上帝能看懂了…… 将来我们写的代码量是很大的,代码又不像我们自然语言这么好理解,可能过段时间我们就不认识 ...

  4. 【数据结构学习】关于HashMap的那些事儿

    涉及数据结构 红黑树 链表 哈希 从CRUD说起 预热知识: DEFAULT_INITIAL_CAPACITY = 1 << 4, HashMap默认容量为16(n << m意 ...

  5. 信息收集框架——recon-ng

    背景:在渗透测试前期做攻击面发现(信息收集)时候往往需要用到很多工具,最后再将搜集到的信息汇总到一块.          现在有这样一个现成的框架,里面集成了许多信息收集模块.信息存储数据库.以及报告 ...

  6. 鲜为人知的maven标签解说

    目录 localRepository interactiveMode offline pluginGroups proxies servers mirrors profiles 使用场景 出现位置 激 ...

  7. Android删除指定路径下指定前缀或后缀的文件

    微信公众号:CodingAndroid CSDN:http://blog.csdn.net/xinpengfei521声明:本文由CodingAndroid原创,未经授权,不可随意转载! 需求 我们在 ...

  8. cs231n---卷积网络可视化,deepdream和风格迁移

    本课介绍了近年来人们对理解卷积网络这个“黑盒子”所做的一些可视化工作,以及deepdream和风格迁移. 1 卷积网络可视化 1.1 可视化第一层的滤波器 我们把卷积网络的第一层滤波器权重进行可视化( ...

  9. spring boot 打 war包

    spring boot .spring cloud打 war包,并发布到tomcat中运行 1.pom文件修改 <packaging>war</packaging> 2.< ...

  10. poi使用随笔

    HSSFworkbook,XSSFworkbook,SXSSFworkbook区别简述 HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls: XSSFWor ...