题目链接:http://lightoj.com/volume_showproblem.php?problem=1046
Time Limit: 1 second(s) | Memory Limit: 32 MB |
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.
Sample Input | Output for Sample Input |
4
3 2 .. 2. ..
3 3 1.1 ... ..1
10 10 .......... .2....2... ......2... 1......... ...2.1.... ...1...... .......... .......21. .......... ..........
1 4 1..1 | Case 1: 0 Case 2: 4 Case 3: 14 Case 4: -1 |
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 16
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
typedef pair<int, int> P; //或者用结构体
int sept[N][N], rec[N][N], m, n, num;
char mp[N][N];
int nx[8] = {1, 1, 2, 2, -1, -1, -2, -2};
int ny[8] = {2, -2, 1, -1, 2, -2, 1, -1};
void bfs(int x, int y)
{
int i, j, used[N][N];
memset(used, 0, sizeof(used));
memset(sept, -1, sizeof(used));
int s = mp[x][y] - '0';
queue< P > q;
q.push(make_pair(x, y));
used[x][y] = 1;
sept[x][y] = 0;
while(!q.empty())
{
P now = q.front();
q.pop();
for(int k = 0; k < 8; k++) // 搜索符合条件的进入队列
{
i = now.first + nx[k];
j = now.second + ny[k];
if(i < 1 || i > m || j < 1 || j > n) continue;
if(used[i][j]) continue;
used[i][j] = 1;
sept[i][j] = sept[now.first][now.second] + 1;
q.push(make_pair(i, j));
}
}
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
{
if(sept[i][j] == -1) continue;
int k = (int)ceil(1.0 * sept[i][j] / s); // 几步能到,ceil()函数返回一个大于等于该浮点数的第一个整数值,向上取整
rec[i][j] = rec[i][j] == -1 ? k : (k + rec[i][j]);
}
}
int solve()
{
int ans = inf, i, j;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++)
ans = rec[i][j] == -1 ? ans : min(ans, rec[i][j]);
return ans;
}
int main()
{
int t, cnt = 0;
cin >> t;
while(t--)
{
memset(rec, -1, sizeof(rec));
scanf("%d%d", &m,&n);
for(int i = 1; i <= m; i++)
scanf(" %s", &mp[i][1]);
num = 0;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
if(mp[i][j] != '.') bfs(i, j), num++;
int ans = solve();
if(ans == inf || (num != 1 && ans == 0))
ans = -1;
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}