LeightOJ 1046 - Rider

本文介绍了一种算法,用于解决不同类型的骑士(棋子)如何在棋盘上移动到同一位置的问题。考虑到骑士的不同跳跃能力,算法通过遍历所有可能的目的地并计算所需最小移动次数来寻找解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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\n", cases, ans);
}

int main()
{
    int t;
    scanf("%d", &t);
    for(int i=1; i<=t; ++ i)
        solve(i);
    return 0;
}

转载于:https://www.cnblogs.com/aiterator/p/6730421.html

资源下载链接为: https://pan.quark.cn/s/f989b9092fc5 在 Android 应用开发中,开发一款仿 OPPO 手机计算器的应用是极具实践价值的任务,它融合了 UI 设计、事件处理以及数学逻辑等多方面的技术要点。当前的“最新版仿 OPPO 手机计算器--android.rar”压缩包中,提供了该计算器应用的源代码,这为开发者深入学习 Android 编程提供了宝贵的资源。 UI 设计是构建此类计算器应用的基石。OPPO 手机的计算器界面以清晰的布局和良好的用户交互体验著称,其中包括数字键、运算符键以及用于显示结果的区域等关键元素。开发者需借助 Android Studio 中的 XML 布局文件来定义这些界面元素,可选用 LinearLayout、GridLayout 或 ConstraintLayout 等布局管理器,并搭配 Button 控件来实现各个按键功能。同时,还需考虑不同分辨率屏幕和设备尺寸的适配问题,这通常涉及 Density Independent Pixel(dp)单位的应用以及 Android 尺寸资源的合理配置。 事件处理构成了计算器的核心功能。开发者要在每个按钮的点击事件中编写相应的处理代码,通常通过实现 OnClickListener 接口来完成。例如,当用户点击数字键时,相应的值会被添加到显示区域;点击运算符键时,则会保存当前操作数并设定运算类型。而对于等号(=)按钮,需要执行计算操作,这往往需要借助栈数据结构来存储操作数和运算符,并运用算法解析表达式以完成计算。 数学逻辑的实现则是计算器功能的关键体现。在 Android 应用中,开发者可以利用 Java 内置的 Math 类,或者自行设计算法来完成计算任务。基本的加减乘除运算可通过简单的算术操作实现,而像求幂、开方等复杂运算则需调用 Math 类的相关方法。此外
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值