zoukankan      html  css  js  c++  java
  • lightoj 1046

    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;
    }
    
    
  • 相关阅读:
    QT5编程入门教程
    bstr_t与BSTR
    Android Studio 更改APP图标
    Indy服务器关闭所有客户端连接
    使用高德地图API
    内网渗透——Hadoop未授权访问getshell
    内网渗透——struts2远程任意代码执行(s2-046)
    工具使用——cobalt strike使用
    工具使用——docker使用
    漏洞复现——weblogic任意文件上传(cve-2018-2894)
  • 原文地址:https://www.cnblogs.com/TnT2333333/p/6075268.html
Copyright © 2011-2022 走看看