cf Educational Codeforces Round 52 D. Three Pieces

2024-03-24 07:48

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/840916

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

cf 164 C 费用流

给你n个任务,k个机器,n个任务的起始时间,持续时间,完成任务的获利 每个机器可以完成任何一项任务,但是同一时刻只能完成一项任务,一旦某台机器在完成某项任务时,直到任务结束,这台机器都不能去做其他任务 最后问你当获利最大时,应该安排那些机器工作,即输出方案 具体建图方法: 新建源汇S T‘ 对任务按照起始时间s按升序排序 拆点: u 向 u'连一条边 容量为 1 费用为 -c,

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

CF 508C

点击打开链接 import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String [] args){new Solve().run() ;} }class Solve{int bit[] = new int[608] ;int l

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

认知杂谈52

今天分享 有人说的一段争议性的话 I I 1拓展人脉很重要** 咱们活在这世上啊,得明白一件事儿,知识、逻辑能力和实战经验虽然重要,但确实都不是最关键的。真正关键的是要懂得怎么和那些手里有资源的人打交道。人脉那可真是一笔无形的大财富呢。你想想看,有时候一个有影响力的人帮你一把,那效果可比你累死累活干一年都强得多。 I I 就比如说,你要是认识个行业里的大牛,他可能给你介绍个特别好的工

代码随想录训练营day37|52. 携带研究材料,518.零钱兑换II,377. 组合总和 Ⅳ,70. 爬楼梯

52. 携带研究材料 这是一个完全背包问题,就是每个物品可以无限放。 在一维滚动数组的时候规定了遍历顺序是要从后往前的,就是因为不能多次放物体。 所以这里能多次放物体只需要把遍历顺序改改就好了 # include<iostream># include<vector>using namespace std;int main(){int n,m;cin>>n>>m;std::vector<i

Three 渲染器(二)

WebGL1Renderer 构造函数 WebGL1Renderer( parameters : Object ) Creates a new WebGL1Renderer. 属性 See the base WebGLRenderer class for common properties. 方法 See the base WebGLRenderer class for common