D. Three Pieces
下象棋 题意还可以 就和我们的象棋差不多
多状态的最短路问题
状态:当前的位置x,y,用的棋子类型
需要记录:dist,改变次数
#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ll long long
const ll MOD = 1e9+7;
#define FORP(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
#define horse 0
#define che 1
#define xiang 2
ll G[20][20];
struct node{
int x,y;
}p[120];
bool vis[109][20][20][20];
int n;
int ans1,ans2;
struct qnode{
int to,x,y,dist,type,change;
bool operator<(const qnode &r)const{
return dist!=r.dist?dist>r.dist:change>r.change;
}
};
priority_queue<qnode> PQ;
inline bool check(int x1,int y1,int x2,int y2,int now){
if(x1<1||x1>n||y1<1||y2>n) return false;//点不合法
if(x1==x2&&y1==y2) return false;//同一个点
if(now==horse) return abs(x1-x2)*abs(y1-y2)==2;//马可以一步跳
else if(now==che) return x1==x2||y1==y2;//车可以一步跳
else return abs(x1-x2)==abs(y1-y2);//象可以一步跳
}
void dijkstra(int x){
for(int i=0;i<3;++i){
PQ.push(qnode{1,p[1].x,p[1].y,0,i,0});
}
while(!PQ.empty()){
qnode u=PQ.top();
PQ.pop();
if(vis[u.to][u.x][u.y][u.type])continue;
vis[u.to][u.x][u.y][u.type]=1;
if(u.to==n*n){
ans1=u.dist;
ans2=u.change;
break;
}
for(int i=1;i<=n;++i){//暴力枚举所有点
for(int j=1;j<=n;++j){
if(!check(u.x,u.y,i,j,u.type)) continue;//如果不可以一步跳或者某点不合法 continue
int to=(G[i][j]==u.to+1)?u.to+1:u.to;//如果是下一个点 那就状态改变 否则还是当前这个点在前往下一个点的路上
if(!vis[to][i][j][u.type]) PQ.push(qnode{to,i,j,u.dist+1,u.type,u.change});//确保状态不重复
}
}
for(int i=0;i<3;++i){
if(!vis[u.to][u.x][u.y][i]){//暴力添加其他情况
PQ.push(qnode{u.to,u.x,u.y,u.dist+1,i,u.change+1});
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;j++){
cin>>G[i][j];
p[G[i][j]].x=i;
p[G[i][j]].y=j;
}
}
dijkstra(1);
printf("%d %d\n",ans1,ans2);
return 0;
}