#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. SSAS——基础

    一.Analysis Services Analysis Services是用于决策支持和BI解决方案的数据引擎.它提供报表和客户端中使用的分析数据. 它可在多用途数据模型中创建高性能查询结构,业务逻 ...

  2. 分享知识-快乐自己:Spring切入点的表达式和通知类型

    1.切入点的表达式 表达式格式: execution([修饰符] 返回值类型 包名.类名.方法名(参数)) 其他的代替: <!-- 完全指定一个方法 --> <!-- <aop ...

  3. STL memory.cpp

    memory.cpp # // Filename: memory # # // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: ht ...

  4. Core Data存储数据出错(This NSPersistentStoreCoordinator has no persistent stores (unknown))

    Core Data存储数据的时候崩溃,崩溃信息: reason: 'This NSPersistentStoreCoordinator has no persistent stores (unknow ...

  5. IronPython for ASP.NET 部署注意事项

    用 IronPython for ASP.NET 开发的网站,在部署时,除了发布 bin 目录下的 IronPython.dll, IronMath.dll, Microsoft.Web.IronPy ...

  6. JS性能之滚动条之外的其他部分

    问题: 如果一个页面宽高比较大,也就是页面需要滚动条来查看其他页面内容,这时候,在滚动条之外的其他部分,是依然处于运行状态. 比如那部分有视频播放,则那些视频虽然在当前窗口看不到,但它们还是会处于播放 ...

  7. 人物-IT-马云:马云

    ylbtech-人物-IT-马云:马云 马云 (阿里巴巴集团创始人) 马云,男,汉族,中共党员,1964年9月10日生于浙江省杭州市,祖籍浙江省嵊州市谷来镇, 阿里巴巴集团主要创始人,现担任阿里巴巴集 ...

  8. 在Golang中使用C语言代码实例

    转自:http://www.jb51.net/article/56720.htm cgo 使得在 Golang 中可以使用 C 代码. Hello World 为了有一个较为直观的了解,我们来看一个简 ...

  9. 使用root直接安装python3.5.2

    操作系统:Centos7.4 不使用pyenv管理器直接进行编译安装3.5.2版本. 下载tgz的包,如果没有安装wget,请yum -y install wget 解压python包: 将解压后的包 ...

  10. TS学习之解构与展开

    一.解构 1.解构数组 let input = [1, 2]; let [first, second] = input; console.log(first); // outputs 1 consol ...