LightOJ - 1046 Rider

Description

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

4

 

3 2

..

2.

..

 

3 3

1.1

...

..1

 

10 10

..........

.2....2...

......2...

1.........

...2.1....

...1......

..........

.......21.

..........

..........

 

1 4

1..1

Sample Output

Case 1: 0

Case 2: 4

Case 3: 14

Case 4: -1


题解:这道题的意思是把所有的骑士放在同一个格子最少多少步,K骑士一次可以走1~K步。此题可以利用反向求解,就是求每个格子到达相应的虚拟骑士的步数,然后再利用K个来求出次骑士所需要的步数.

distance(k,from,to) = ceil(distance(1,from,to)/K)


#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <set>
using namespace std;

char pic[11][11];
int vis[11][11];
int m,n,minlen;
typedef pair<int,int> pii;
set<pii> S;
queue<pii> Q;
int direct[8][2] = {{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}};

void bfs(int x,int y){
    memset(vis,-1,sizeof(vis));
    vis[x][y] = 0;
    while(!Q.empty())Q.pop();
    Q.push(make_pair(x,y));
    while(!Q.empty()){
        pii u = Q.front();Q.pop();
        for(int i = 0; i < 8; i++){
            int x1 = u.first+direct[i][0];
            int y1 = u.second+direct[i][1];
            if(x1<0||x1>=m||y1<0||y1>=n) continue;
            if(vis[x1][y1] != -1) continue;
            vis[x1][y1] = vis[u.first][u.second] + 1;
            Q.push(make_pair(x1,y1));
        }
    }
    int sumlen = 0;bool flag = true;
    for(set<pii>::const_iterator it = S.begin();it!=S.end();it++){
        pii temp = *it;
        double K = (pic[temp.first][temp.second]-'0')*1.0;
        int len = vis[temp.first][temp.second];
        if(len<=-1){flag = false;break;}
        sumlen+=(len+K-1)/K;
    }//cout<<sumlen<<" ";
    if(flag)minlen = min(minlen,sumlen);
}

int main(){
    int T;
    scanf("%d",&T);
    for(int i = 1; i <= T; i++){
        scanf("%d%d",&m,&n);
        minlen = 99999;
        S.clear();
        for(int row = 0; row < m; row++){
            scanf("%s",pic[row]);
            for(int column = 0; column < n; column++){
                if('1'<=pic[row][column] && pic[row][column]<='9'){
                    S.insert(make_pair(row,column));
                }
            }
        }
        for(int row = 0; row < m; row++)
            for(int column = 0; column < n; column++)bfs(row,column);
        minlen == 99999?printf("Case %d: %d\n",i,-1):printf("Case %d: %d\n",i,minlen);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值