Light oj 1046 - Rider (BFS)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1046


1046 - Rider
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

 

题目大意:数字是能走的步数,‘ .  ’是空位,一次只能走一个棋子,求所有棋子都到达某一格中用的最小步数
解析:题中给出的8个方向上广搜,找出最优解

代码如下:

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值