如果不喜欢过长代码的看官,请移步其他题解...
这题其实思想极其简单:
棋盘问题常见的算法都比较暴力,常用的有搜索和状压dp
而这道题显然没啥能状压的,所以我们考虑搜索
但是仅仅搜索是不够的,因为有极大的可能搜到死...
所以我们引入记忆化
设状态f[i][j][k][0/1/2]代表目前在点(i,j)处,上一个到达的点(注意是达成要求而非经过)的编号为k,目前的棋子种类为1/2/3时,所需要的最小代价
当然这还不够,我们还要保证在代价最小时更换棋子次数最小,所以我们再用状态g[i][j][k][0/1/2]更新,下标含义与上述相同,表示对应状态所需的最小棋子更换次数
注意优先更新f,在保证f合法的情况下维护g
然后用bfs更新即可
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <vector>
#include <ctime>
#include <set>
#include <deque>
#include <bitset>
using namespace std;
int dp[][][][];
int g[][][][];
int xp[],yp[];
int maps[][];
int dir[][]={{,},{,},{,-},{-,},{-,-},{-,-},{,-},{-,}};
int n;
bool check(int x,int y)
{
if(x>&&x<=n&&y>&&y<=n)
{
return ;
}
return ;
}
void bfs()
{
memset(dp,0x3f,sizeof(dp));
memset(g,0x3f,sizeof(g));
dp[xp[]][yp[]][][]=;//车
dp[xp[]][yp[]][][]=;//马
dp[xp[]][yp[]][][]=;//象
g[xp[]][yp[]][][]=;
g[xp[]][yp[]][][]=;
g[xp[]][yp[]][][]=;
queue <int> Qx,Qy,Qz,Qv;
for(int i=;i<=;i++)
{
Qx.push(xp[]);
Qy.push(yp[]);
Qz.push(i);
Qv.push();
}
while(!Qx.empty())
{
int ux=Qx.front();
int uy=Qy.front();
int uz=Qz.front();
int uv=Qv.front();
Qx.pop();
Qy.pop();
Qz.pop();
Qv.pop();
if(uv==n*n)
{
continue;
}
if(uz==)
{
for(int i=;i<=n;i++)
{
if(i==uy)
{
continue;
}
if(maps[ux][i]==uv+)
{
if(dp[ux][i][uv+][uz]>dp[ux][uy][uv][uz]+)
{
dp[ux][i][uv+][uz]=dp[ux][uy][uv][uz]+;
g[ux][i][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(ux);
Qy.push(i);
Qz.push(uz);
Qv.push(uv+);
}else if(dp[ux][i][uv+][uz]==dp[ux][uy][uv][uz]+&&g[ux][i][uv+][uz]>g[ux][uy][uv][uz])
{
g[ux][i][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(ux);
Qy.push(i);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[ux][i][uv][uz]>dp[ux][uy][uv][uz]+)
{
dp[ux][i][uv][uz]=dp[ux][uy][uv][uz]+;
g[ux][i][uv][uz]=g[ux][uy][uv][uz];
Qx.push(ux);
Qy.push(i);
Qz.push(uz);
Qv.push(uv);
}else if(dp[ux][i][uv][uz]==dp[ux][uy][uv][uz]+&&g[ux][i][uv][uz]>g[ux][uy][uv][uz])
{
g[ux][i][uv][uz]=g[ux][uy][uv][uz];
Qx.push(ux);
Qy.push(i);
Qz.push(uz);
Qv.push(uv);
}
}
}
for(int i=;i<=n;i++)
{
if(i==ux)
{
continue;
}
if(maps[i][uy]==uv+)
{
if(dp[i][uy][uv+][uz]>dp[ux][uy][uv][uz]+)
{
dp[i][uy][uv+][uz]=dp[ux][uy][uv][uz]+;
g[i][uy][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(i);
Qy.push(uy);
Qz.push(uz);
Qv.push(uv+);
}else if(dp[i][uy][uv+][uz]==dp[ux][uy][uv][uz]+&&g[i][uy][uv+][uz]>g[ux][uy][uv][uz])
{
g[i][uy][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(i);
Qy.push(uy);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[i][uy][uv][uz]>dp[ux][uy][uv][uz]+)
{
dp[i][uy][uv][uz]=dp[ux][uy][uv][uz]+;
g[i][uy][uv][uz]=g[ux][uy][uv][uz];
Qx.push(i);
Qy.push(uy);
Qz.push(uz);
Qv.push(uv);
}else if(dp[i][uy][uv][uz]==dp[ux][uy][uv][uz]+&&g[i][uy][uv][uz]>g[ux][uy][uv][uz])
{
g[i][uy][uv][uz]=g[ux][uy][uv][uz];
Qx.push(i);
Qy.push(uy);
Qz.push(uz);
Qv.push(uv);
}
}
}
if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+)
{
dp[ux][uy][uv][]=dp[ux][uy][uv][uz]+;
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}else if(dp[ux][uy][uv][]==dp[ux][uy][uv][uz]+&&g[ux][uy][uv][]>g[ux][uy][uv][uz]+)
{
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}
if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+)
{
dp[ux][uy][uv][]=dp[ux][uy][uv][uz]+;
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}else if(dp[ux][uy][uv][]==dp[ux][uy][uv][uz]+&&g[ux][uy][uv][]>g[ux][uy][uv][uz]+)
{
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}
}else if(uz==)//马
{
for(int i=;i<;i++)
{
int tx=ux+dir[i][];
int ty=uy+dir[i][];
if(tx<=||tx>n||ty<=||ty>n)
{
continue;
}
if(maps[tx][ty]==uv+)
{
if(dp[tx][ty][uv+][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv+][]=dp[ux][uy][uv][]+;
g[tx][ty][uv+][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv+);
Qz.push();
}else if(dp[tx][ty][uv+][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv+][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[tx][ty][uv][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv][]=dp[ux][uy][uv][]+;
g[tx][ty][uv][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv);
Qz.push();
}else if(dp[tx][ty][uv][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv);
}
}
}
if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+)
{
dp[ux][uy][uv][]=dp[ux][uy][uv][uz]+;
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}else if(dp[ux][uy][uv][]==dp[ux][uy][uv][uz]+&&g[ux][uy][uv][]>g[ux][uy][uv][uz]+)
{
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}
if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+)
{
dp[ux][uy][uv][]=dp[ux][uy][uv][uz]+;
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}else if(dp[ux][uy][uv][]==dp[ux][uy][uv][uz]+&&g[ux][uy][uv][]>g[ux][uy][uv][uz]+)
{
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}
}else if(uz==)
{
for(int i=;i<=n;i++)
{
int tx=ux+i;
int ty=uy+i;
if(check(tx,ty))
{
if(maps[tx][ty]==uv+)
{
if(dp[tx][ty][uv+][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv+][]=dp[ux][uy][uv][]+;
g[tx][ty][uv+][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv+);
Qz.push();
}else if(dp[tx][ty][uv+][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv+][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[tx][ty][uv][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv][]=dp[ux][uy][uv][]+;
g[tx][ty][uv][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv);
Qz.push();
}else if(dp[tx][ty][uv][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv+);
}
}
}
tx=ux+i;
ty=uy-i;
if(check(tx,ty))
{
if(maps[tx][ty]==uv+)
{
if(dp[tx][ty][uv+][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv+][]=dp[ux][uy][uv][]+;
g[tx][ty][uv+][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv+);
Qz.push();
}else if(dp[tx][ty][uv+][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv+][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[tx][ty][uv][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv][]=dp[ux][uy][uv][]+;
g[tx][ty][uv][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv);
Qz.push();
}else if(dp[tx][ty][uv][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv);
}
}
}
tx=ux-i;
ty=uy+i;
if(check(tx,ty))
{
if(maps[tx][ty]==uv+)
{
if(dp[tx][ty][uv+][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv+][]=dp[ux][uy][uv][]+;
g[tx][ty][uv+][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv+);
Qz.push();
}else if(dp[tx][ty][uv+][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv+][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[tx][ty][uv][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv][]=dp[ux][uy][uv][]+;
g[tx][ty][uv][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv);
Qz.push();
}else if(dp[tx][ty][uv][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv);
}
}
}
tx=ux-i;
ty=uy-i;
if(check(tx,ty))
{
if(maps[tx][ty]==uv+)
{
if(dp[tx][ty][uv+][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv+][]=dp[ux][uy][uv][]+;
g[tx][ty][uv+][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv+);
Qz.push();
}else if(dp[tx][ty][uv+][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv+][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv+][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv+);
}
}else
{
if(dp[tx][ty][uv][]>dp[ux][uy][uv][]+)
{
dp[tx][ty][uv][]=dp[ux][uy][uv][]+;
g[tx][ty][uv][]=g[ux][uy][uv][];
Qx.push(tx);
Qy.push(ty);
Qv.push(uv);
Qz.push();
}else if(dp[tx][ty][uv][uz]==dp[ux][uy][uv][uz]+&&g[tx][ty][uv][uz]>g[ux][uy][uv][uz])
{
g[tx][ty][uv][uz]=g[ux][uy][uv][uz];
Qx.push(tx);
Qy.push(ty);
Qz.push(uz);
Qv.push(uv);
}
}
}
}
if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+)
{
dp[ux][uy][uv][]=dp[ux][uy][uv][uz]+;
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}else if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+&&g[ux][uy][uv][]>g[ux][uy][uv][uz]+)
{
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}
if(dp[ux][uy][uv][]>dp[ux][uy][uv][uz]+)
{
dp[ux][uy][uv][]=dp[ux][uy][uv][uz]+;
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}else if(dp[ux][uy][uv][]==dp[ux][uy][uv][uz]+&&g[ux][uy][uv][]>g[ux][uy][uv][uz]+)
{
g[ux][uy][uv][]=g[ux][uy][uv][uz]+;
Qx.push(ux);
Qy.push(uy);
Qz.push();
Qv.push(uv);
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&maps[i][j]);
xp[maps[i][j]]=i;
yp[maps[i][j]]=j;
}
}
bfs();
int ans=0x3f3f3f3f;
int ret=0x3f3f3f3f;
for(int i=;i<=;i++)
{
if(ans>dp[xp[n*n]][yp[n*n]][n*n][i])
{
ans=dp[xp[n*n]][yp[n*n]][n*n][i];
ret=g[xp[n*n]][yp[n*n]][n*n][i];
}else if(ret>g[xp[n*n]][yp[n*n]][n*n][i]&&ans==dp[xp[n*n]][yp[n*n]][n*n][i])
{
ret=g[xp[n*n]][yp[n*n]][n*n][i];
}
}
printf("%d %d\n",ans,ret);
return ;
}