Educational Codeforces Round 52D(ARRAY,模拟最短路)
#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,模拟最短路)的更多相关文章
- [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 ...
- Educational Codeforces Round 117 (Rated for Div. 2)
Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 35 A. Nearest Minimums【预处理】
[题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...
- Educational Codeforces Round 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...
随机推荐
- cocos2d-x 3.9 android studio项目命令行打包
进入创建的项目的 proj.android-studio目录 cocos run/compile -p android --android-studio 这样就可以打包了
- java:Properties属性文件概念
java:Properties属性文件概念 在java之前的国际化程序中提出了一个属性文件的概念,属性文件的后缀是:*.properties,那么在java中提供了意个属性文件的专门操作类,Prope ...
- STL list链表的用法详解
本文以List容器为例子,介绍了STL的基本内容,从容器到迭代器,再到普通函数,而且例子丰富,通俗易懂.不失为STL的入门文章,新手不容错过! 0 前言 1 定义一个list 2 使用list的成员函 ...
- Python基础-读取excel
import xlrdbook = xlrd.open_workbook('lanxia.xls')sheet = book.sheet_by_name('sheet1')rows = sheet.n ...
- Tomcat_异常_01_Setting property 'source' to 'org.eclipse.jst.jee.server:AWeiXin_QiYe_Demo' did not find a matching property
关于解决方法: 1.解决Setting property 'source' to 'org.eclipse.jst.jee.server的问题 2.eclipse中server location为灰色 ...
- Hibernate - POJO 类和数据库的映射文件*.hbm.xml
POJO 类和关系数据库之间的映射可以用一个XML文档来定义. 通过 POJO 类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对 ...
- Agc011_C Squared Graph
传送门 题目大意 给定$n$个点$m$条边的简单图(无重边无自环),将有序点对$\{a,b\}$作为新的点,新产生的$n^2$个点中对于两个点,$\{a,b\},\{x,y\}$,当且仅当原图中存在边 ...
- YNOI2016 这是我自己的发明
看到这个标题立刻想到:. “绝地科学家,八倍不屏息啊,八百里外把头打啊...” 首先我们发现如果只考虑第二个操作,这棵树就是假的,我们可以直接莫队解决 如果考虑换根的话...可以把一个操作换成小于等于 ...
- 判断iOS系统的Model
获取iOS系统的Model (参考网址:https://www.theiphonewiki.com/wiki/Models) + (NSString *)getModel{ struct utsn ...
- 使用Rancher搭建K8S测试环境
使用Rancher搭建K8S测试环境 http://blog.csdn.net/csdn_duomaomao/article/details/75316926 环境准备(4台主机,Ubuntu16.0 ...