zoukankan      html  css  js  c++  java
  • LeightOJ 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.

    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
    

    Output for Sample Input

    Case 1: 0
    Case 2: 4
    Case 3: 14
    Case 4: -1
    

    枚举最后所有骑士落脚的地点,然后暴搜每个骑士最少到达该点的步数,用一个变量res存储所有骑士花费的步数。

    用一个变量更新res中的最小值就可以了。

    #include<bits/stdc++.h>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    
    bool mk[17][17];
    int dir[8][2] = { {1,2}, {1,-2}, {2, 1}, {2, -1}, {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1} };
    char mp[17][17];
    int m, n;
    
    struct node
    {
        int x, y, step;
        node(){};
        node(int x, int y, int step): x(x), y(y), step(step){}
    };
    
    int dfs(int ed_x, int ed_y, int bg_x, int bg_y)
    {
        if(ed_x == bg_x && ed_y == bg_y)
            return 0;
    
        queue<node>que;
        que.push(node(bg_x, bg_y, 0));
        memset(mk, false, sizeof(mk));
        mk[bg_x][bg_y] = true;
    
        while(!que.empty())
        {
            node f = que.front();
            que.pop();
    
            for(int i=0; i<8; ++ i)
            {
                int x = f.x + dir[i][0], y = f.y + dir[i][1];
                if(x >= 0 && x < m && y >= 0 && y < n && mk[x][y] == false)
                {
                    if(x == ed_x && y == ed_y)
                        return f.step + 1;
    
                    mk[x][y] = true;
                    que.push(node(x, y, f.step + 1));
                }
            }
        }
        return -1;
    }
    
    int cou(int x, int y)
    {
        int res = 0;
        for(int i=0; i<m; ++ i)
        {
            for(int j=0; j<n; ++ j)
            {
                if(mp[i][j] != '.')
                {
                    int t = dfs(x, y, i, j);
    
                    if(t == -1)
                        return -1;
                    t = t/(mp[i][j] - '0') + (t%(mp[i][j] - '0') != 0);
                    res += t;
                }
            }
        }
        return res;
    }
    
    void solve(int cases)
    {
        scanf("%d%d", &m, &n);
    
        for(int i=0; i<m; ++ i)
            for(int j=0; j<n; ++ j)
                scanf(" %c", &mp[i][j]);
    
        int ans = INF;
        bool have = false;
    
        for(int i=0; i<m; ++ i)
        {
            for(int j=0; j<n; ++ j)
            {
                int t = cou(i, j);
                if(t != -1)
                {
                    ans = min(ans, t);
                    have = true;
                }
            }
        }
        if(have == false)
            ans = -1;
    
        printf("Case %d: %d
    ", cases, ans);
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        for(int i=1; i<=t; ++ i)
            solve(i);
        return 0;
    }
    
    
  • 相关阅读:
    Flash/Flex学习笔记(14):制作涂鸦板
    Flash/Flex学习笔记(9):ActionScript3.0与Javascript的相互调用
    Flash/Flex学习笔记(15):FMS 3.5之远程共享对象(Remote Shared Object)
    vs2008 自动属性
    上传图片 水印位置计算 记录一下
    产生一个int数组,长度为100,并向其中随机插入1100,并且不能重复 的另一种写法
    关于存储过程 select top n 参数问题
    datagrid 和 gridview 与checkbox 获取所选中的id的方法
    如何在webservice中取得sesssionid
    asp.net 将新添加的文件直接加上版权信息
  • 原文地址:https://www.cnblogs.com/aiterator/p/6730421.html
Copyright © 2011-2022 走看看