#include<bits/stdc++.h>
using namespace std;
int n,x;
int chess[17*17];//记录棋盘上的number
array<int,2>pace[17*17*3][17*17*3],dp[17*17][3];//first记录root,second记录change
array<int,2>operator+(const array<int,2>a,const array<int,2> b){
    return {a[0]+b[0],a[1]+b[1]};
}
int main(){
    memset(pace,1,sizeof(pace));//最大为1
    memset(dp,1,sizeof(dp));//最大为1
    scanf("%d",&n);
    for(int i=0;i<n*n;i++){
        scanf("%d",&x);
        x--;
        chess[x]=i;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int k=0;k<3;k++){
                for(int l=0;l<3;l++){
                    pace[(i*n+j)*3+k][(i*n+j)*3+l]={1,1};//i*n后+j可以表示棋盘上n*n的所有位置,*3后可以用一维表示位置和用哪一种棋子走到这里的,不*3多开一维也可以
                }
            }
            for(int k=0;k<n;k++){
                for(int l=0;l<n;l++){
                    if(i==k||j==l)//车一步可以走到
                        pace[(i*n+j)*3][(k*n+l)*3]={1,0};
                    else if(abs(i-k)+abs(j-l)==3)//骑士一步可以走到
                        pace[(i*n+j)*3+2][(k*n+l)*3+2]={1,0};//+1用来区分是谁走的
                    if(i+j==k+l||i-j==k-l)//皇后一步可以走到
                    pace[(i*n+j)*3+1][(k*n+l)*3+1]={1,0};//+2用于区分
                }
            }
        }
    }
    for(int k=0;k<n*n*3;k++){
        for(int i=0;i<n*n*3;i++){
            for(int j=0;j<n*n*3;j++){
                pace[i][j]=min(pace[i][k]+pace[k][j],pace[i][j]);//最短路
            }
        }
    }
    dp[0][0]=dp[0][1]=dp[0][2]={};
    for(int i=1;i<n*n;i++){
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++){
                dp[i][j]=min(dp[i-1][k]+pace[chess[i-1]*3+k][chess[i]*3+j],dp[i][j]);
            }
        }
    }
    array<int,2>a=min({dp[n*n-1][0],dp[n*n-1][1],dp[n*n-1][2]});//分别为初始的三种棋子,首先比较第一个元素选取最小,依次比较后续元素选取最小
    printf("%d %d",a[0],a[1]);
    return 0;
}

Educational Codeforces Round 52D(ARRAY,模拟最短路)的更多相关文章

  1. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  2. Educational Codeforces Round 117 (Rated for Div. 2)

    Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...

  3. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  4. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  6. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  7. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  8. Educational Codeforces Round 35 A. Nearest Minimums【预处理】

    [题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...

  9. Educational Codeforces Round 26

    Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...

随机推荐

  1. 从HTTP请求中获取客户IP地址

    /**     * 从HTTP请求中获取客户IP地址     *     * @param request http请求     * @return 客户IP地址     */    public s ...

  2. Abp模块分析

    1.什么是模块? 模块化是一种处理复杂系统分解为更好的可管理模块的方式.模块化用来分割,组织和打包软件.每个模块完成一个特定的子功能,所有的模块按某种方法组装起来,成为一个整体,完成整个系统所要求的功 ...

  3. IpIImage -> CvMat 转换方法

    Ipl转为CvMat 一般为这两种方法: 1: /*cvGetMat*/ CvMat matheader; CvMat * mat = cvGetMat(img, &matheader); 2 ...

  4. 制作SD卡img文件,并扩容

    /********************************************************************************** * raspi-config E ...

  5. uimsbf和 bslbf的含义

    bslbf代表位串,即“Bit string, left bit first ”, uimsbf代表无符号整数,即”unsinged integer, most significant bit fir ...

  6. ACM学习历程—Hihocoder 1289 403 Forbidden(字典树 || (离线 && 排序 && 染色))

    http://hihocoder.com/problemset/problem/1289 这题是这次微软笔试的第二题,过的人比第三题少一点,这题一眼看过去就是字符串匹配问题,应该可以使用字典树解决.不 ...

  7. bzoj 3091: 城市旅行 LCT

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=3091 题解: 首先前三个操作就是裸的LCT模板 只考虑第四个操作. 要求我们计算期望,所以我 ...

  8. bzoj 3307: 雨天的尾巴 线段树合并

    题目大意: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.问完成所有发放后,每个点存放最多的是哪种物品. 题解: 首先我们为每一个节 ...

  9. NOIp2018集训test-10-18 (bike day4)

    这是一套简单题,这几天的考试让bike老爷感觉很绝望,说实话我也确实不知道还能怎么更简单了. 这几天的题换做llj.sxy应该都能轻松AK吧,至少随便考个250+应该不是问题吧,我越来越觉得觉得我跟他 ...

  10. 分立元件封装尺寸及PCB板材工艺与设计实例

    分立元件封装尺寸 inch mm (L)mm (w)mm (t)mm (a)mm (b)mm 0201 0603 0.6±0.05 0.30±0.05 0.23±0.05 0.10±0.05 0.60 ...