本文主要是介绍cf Educational Codeforces Round 52 D. Three Pieces,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原题:
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 secondusing 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);}}}//使用floyedforn(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)])
这篇关于cf Educational Codeforces Round 52 D. Three Pieces的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!