#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. web项目路径如何更改

  2. 2018.5.31 nRF905 test

    1 试电机:自动控制测试流程(Labview程序,加载扫描仪,自动测试夹具,测试数据保存) 2 USB RF收发器: 含S/N码发送读取功能(S/N:) The specific use please ...

  3. BEC listen and translation exercise 45

    So the Counselling Services we offer deal with any problems arising from your studies or in your lif ...

  4. python模块inspect.py

    inspect模块用来检查对象的类型(函数,属性,类,抽象基类,方法,模块等等) 是一个封装好的非常有用的模块. ]) ]: cls = :]: content = ] = lines[].lstri ...

  5. [冬令营模拟]wzj的题目#1

    T1 少膜一个,T3 暴力写挂 强势 rank1 -> rank2 一场比赛两道线段树分治,给力 T1 password 给你 m 个禁止字符串,求长度为 n 的所有字符串中至少包含这些禁止字符 ...

  6. loj515贪心只能过样例

    bitset练习题... 位运算真的是玄学... 一开始真的“只能过样例” 后来发现把左移写成了小于号 鬼知道我在想什么/手动微笑 loj第一题 #include<iostream> #i ...

  7. CodeForces - 1017 C. The Phone Number(数学)

    Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...

  8. bzoj 1670 Building the Moat护城河的挖掘 —— 凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 单调栈维护凸包即可,用叉积判断: 维护上凸壳,然后把所有点的纵坐标翻转再求上凸壳即可, ...

  9. git常见使用情境整理

    一.版本回退 回退到某个commit版本的方法如下: 1. 查看commit历史 git log 找到想要回退的版本的号码,eg:f765889 2. 回退到该版本 git reset f765889 ...

  10. Poj 2503 Babelfish(Map操作)

    一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...