cf Educational Codeforces Round 52 D. Three Pieces

本文探讨了一种优化算法,用于解决在N×N棋盘上,利用knight、bishop和rook三种棋子,遍历所有格子的最少步数问题。通过状态压缩和动态规划,结合Floyd算法计算最短路径,实现遍历路径优化。

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

原题:
D. Three Pieces
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You stumbled upon a new kind of chess puzzles. The chessboard you are given is not necesserily 8×8, but it still is N×N
. Each square has some number written on it, all the numbers are from 1 to N2 and all the numbers are pairwise distinct. The j-th square in the i-th row has a number Aij written on it.

In your chess set you have only three pieces: a knight, a bishop and a rook. At first, you put one of them on the square with the number 1(you can choose which one). Then you want to reach square 2 (possibly passing through some other squares in process), then square 3and so on until you reach square N2. In one step you are allowed to either make a valid move with the current piece or replace it with some other piece. Each square can be visited arbitrary number of times.

A knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. A bishop moves diagonally. A rook moves horizontally or vertically. The move should be performed to a different square from the one a piece is currently standing on.

You want to minimize the number of steps of the whole traversal. Among all the paths to have the same number of steps you want to choose the one with the lowest number of piece replacements.

What is the path you should take to satisfy all conditions?

Input
The first line contains a single integer N (3≤N≤10) — the size of the chessboard.

Each of the next N lines contains N integers Ai1,Ai2,…,AiN (1≤Aij≤N2) — the numbers written on the squares of the i-th row of the board.It is guaranteed that all Aij are pairwise distinct.

Output
The only line should contain two integers — the number of steps in the best answer and the number of replacement moves in it.

Example
input
3
1 9 3
8 6 7
4 2 5
output
12 1
Note
Here are the steps for the first example (the starting piece is a knight):

Move to (3,2)
Move to (1,3)
Move to (3,2)
Replace the knight with a rook
Move to (3,1)
Move to (3,3)
Move to (3,2)
Move to (2,2)
Move to (2,3)
Move to (2,1)
Move to (1,1)
Move to (1,2)

中文:

给你一个N×N的棋盘,棋盘上从1到N×N有标号,现在给你三个棋子,分别是knight、bishop、rook,其中knight走“日”字,bishop走对角线(任意远),rook走直线(任意远),每次使用一个棋子进行移动,可以将当前棋子替换为其他棋子,现在问你最少走多少步能够把棋盘上的每个格子遍历完,最少步数的前提下,替换的棋子次数最少。

官方代码:

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef pair<int, int> pt;//first表示最少步数,second表示最少换棋子

const int N = 12;
const int M = 305;
const int INF = 1e9;

int n;
int a[N][N];
pt pos[N * N];
pt dist[M][M];

pt operator +(const pt &a, const pt &b){
    return mp(a.x + b.x, a.y + b.y);
}

int dx[] = {-2, -1, 1, 2,  2,  1, -1, -2};
int dy[] = { 1,  2, 2, 1, -1, -2, -2, -1};

bool in(int x, int y){
    return (0 <= x && x < n && 0 <= y && y < n);
}
//x,y,p表示在位置(x,y)使用棋子p,这里使用一维数组表示三维状态
int get(int x, int y, int p){
    return x * n * 3 + y * 3 + p;
}

pt dp[N * N][3];

int main() {
    scanf("%d", &n);
    forn(i, n) forn(j, n){
        scanf("%d", &a[i][j]);
        --a[i][j];
        pos[a[i][j]] = mp(i, j);
    }
    
    forn(i, M) forn(j, M) dist[i][j] = mp(INF, INF);
    forn(i, M) dist[i][i] = mp(0, 0);
    //初始化从位置(x,y)使用3种棋子走一步后对应状态的距离
    forn(x, n) forn(y, n){
        forn(i, 8){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (in(nx, ny))
                dist[get(x, y, 0)][get(nx, ny, 0)] = mp(1, 0);
        }
        
        for (int i = -n + 1; i <= n - 1; ++i){
            int nx = x + i;
            int ny = y + i;
            if (in(nx, ny))
                dist[get(x, y, 1)][get(nx, ny, 1)] = mp(1, 0);
            ny = y - i;
            if (in(nx, ny))
                dist[get(x, y, 1)][get(nx, ny, 1)] = mp(1, 0);
        }
        
        forn(i, n){
            int nx = x;
            int ny = i;
            dist[get(x, y, 2)][get(nx, ny, 2)] = mp(1, 0);
            nx = i;
            ny = y;
            dist[get(x, y, 2)][get(nx, ny, 2)] = mp(1, 0);
        }
        //换棋子
        forn(i, 3) forn(j, 3){
            if (i != j){
                dist[get(x, y, i)][get(x, y, j)] = mp(1, 1);
            }
        }
    }
    //使用floyed
    forn(k, M) forn(i, M) forn(j, M)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
    //动态规划计算最优遍历
    forn(i, N * N) forn(j, 3) dp[i][j] = mp(INF, INF);
    dp[0][0] = dp[0][1] = dp[0][2] = mp(0, 0);
    forn(i, n * n - 1) forn(j, 3) forn(k, 3)
        dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dist[get(pos[i].x, pos[i].y, j)][get(pos[i + 1].x, pos[i + 1].y, k)]);
    
    pt ans = mp(INF, INF);
    ans = min(ans, dp[n * n - 1][0]);
    ans = min(ans, dp[n * n - 1][1]);
    ans = min(ans, dp[n * n - 1][2]);
    
    printf("%d %d\n", ans.x, ans.y);
    return 0;
}

思路:

比赛的时候写的广搜,没来及弄完,弃疗了,其实是非常好的一道题目。

说说官方题解当中给出的思路,首先可以用设置状态dist[X][Y],表示从状态X到状态Y所移动的最小步数和对应的最少换棋子数,这里的X和Y是表示的状态是所在位置与当前使用的棋子,可以用get函数将三个状态压缩成一个整数。

按照每一个棋子的行走规则,初始化dist[X][Y]

然后使用floyed算法计算dist[X][Y]之间的最短路径

设置状态dp[i][k]表示走到棋盘第i个格子,当前棋子是第k种棋子时得到的最少步数和对应的最少换棋子次数(存储的是pair),那么状态转移方程表示为(很好理解)
d p [ i + 1 ] [ k ] = m i n ( d p [ i + 1 ] [ k ] , d p [ i ] [ j ] + d i s t [ g e t ( p o s [ i ] . x , p o s [ i ] . y , j ) ] [ g e t ( p o s [ i + 1 ] . x , p o s [ i + 1 ] . y , k ) ] ) dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dist[get(pos[i].x, pos[i].y, j)][get(pos[i + 1].x, pos[i + 1].y, k)]) dp[i+1][k]=min(dp[i+1][k],dp[i][j]+dist[get(pos[i].x,pos[i].y,j)][get(pos[i+1].x,pos[i+1].y,k)])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值