【暴力BFS】CF 1065D Three Pieces

D. Three Pieces

time limit per test

2 seconds

memory limit per test

256 megabytes

You stumbled upon a new kind of chess puzzles. The chessboard you are given is not necesserily 8×88×8, but it still is N×NN×N. Each square has some number written on it, all the numbers are from 11 to N2N2 and all the numbers are pairwise distinct. The jj-th square in the ii-th row has a number AijAij written on it.

In your chess set you have only three pieces: a knight, a bishop and a rook. At first, you put one of them on the square with the number 11 (you can choose which one). Then you want to reach square 22 (possibly passing through some other squares in process), then square 33 and so on until you reach square N2N2. In one step you are allowed to either make a valid move with the current piece or replace it with some other piece. Each square can be visited arbitrary number of times.

A knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. A bishop moves diagonally. A rook moves horizontally or vertically. The move should be performed to a different square from the one a piece is currently standing on.

You want to minimize the number of steps of the whole traversal. Among all the paths to have the same number of steps you want to choose the one with the lowest number of piece replacements.

What is the path you should take to satisfy all conditions?

Input

The first line contains a single integer NN (3≤N≤103≤N≤10) — the size of the chessboard.

Each of the next NN lines contains NN integers Ai1,Ai2,…,AiNAi1,Ai2,…,AiN (1≤Aij≤N21≤Aij≤N2) — the numbers written on the squares of the ii-th row of the board.

It is guaranteed that all AijAij are pairwise distinct.

Output

The only line should contain two integers — the number of steps in the best answer and the number of replacement moves in it.

Example

input

Copy

3
1 9 3
8 6 7
4 2 5

output

Copy

12 1

Note

Here are the steps for the first example (the starting piece is a knight):

  1. Move to (3,2)(3,2)
  2. Move to (1,3)(1,3)
  3. Move to (3,2)(3,2)
  4. Replace the knight with a rook
  5. Move to (3,1)(3,1)
  6. Move to (3,3)(3,3)
  7. Move to (3,2)(3,2)
  8. Move to (2,2)(2,2)
  9. Move to (2,3)(2,3)
  10. Move to (2,1)(2,1)
  11. Move to (1,1)(1,1)
  12. Move to (1,2)

题意:给你一个n*n的矩阵,从1-n*n的数字放在里面,要求你从1号开始走,走完所有的棋子

你手头有三种棋子:一个是只能马字走,一个只能横竖走,一个只能斜着走

你任意选择一个棋子从一号开始走,你可以选择换棋子,这也要花费一步,问你最后最小走的步数是多少

以及步数最小是 换棋子最少的步数

思路

开一个dp[x][y][z][t][k]:你走到(x,y)这个位置用的是第z种走法,换了t次棋子,走完了k个点的最小步数

直接暴力BFS,反正走道的就是最小的

 

 

#include <bits/stdc++.h>
using namespace std;
int a[15][15];
int dir1[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
int dir2[4][2]={{1,1},{1,-1},{-1,1},{-1,-1}};
int dir3[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int dp[12][12][5][205][105];

struct node
{
    int x,y,z,t,k;
    node(int _x,int _y,int _z,int _t,int _k)
    {
        x=_x,y=_y,z=_z,t=_t,k=_k;
    }
};

int main()
{
    memset(dp,-1,sizeof(dp));
    int n,sx,sy,ex,ey;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&a[i][j]);
            if(a[i][j]==1)
            {
                sx=i,sy=j;
            }

            if(a[i][j]==n*n)
            {
                ex=i,ey=j;
            }
        }
    }

    queue <node> q;
    q.push(node{sx,sy,1,0,1});
    q.push(node{sx,sy,2,0,1});
    q.push(node{sx,sy,3,0,1});
    dp[sx][sy][1][0][1]=dp[sx][sy][2][0][1]=dp[sx][sy][3][0][1]=0;

    while(!q.empty())
    {
        node u=q.front();
        q.pop();
        //cout<<u.x<<" "<<u.y<<" "<<u.z<<" "<<u.t<<" "<<u.k<<endl;
        int x=u.x,y=u.y,z=u.z,t=u.t,k=u.k;
        for(int i=1;i<=3;i++)  //原地不动,改方向
        {
            if(i==z) continue;
            if(dp[x][y][i][t+1][k]!=-1) continue;
            dp[x][y][i][t+1][k]=dp[x][y][z][t][k]+1;
            q.push(node{x,y,i,t+1,k});
        }

        if(z==1) //一个方向走
        {
            for(int i=0;i<8;i++)
            {
                int kk=k;
                int xx=x+dir1[i][0],yy=y+dir1[i][1];
                if(xx<1||xx>n||yy<1||yy>n) continue;
                if(a[xx][yy]==k+1) kk++;
                if(dp[xx][yy][z][t][kk]!=-1) continue;
                dp[xx][yy][z][t][kk]=dp[x][y][z][t][k]+1;
                q.push(node{xx,yy,z,t,kk});
            }
        }

        else if(z==2)
        {
            for(int j=1;j<=n;j++)
            {
                for(int i=0;i<4;i++)
                {
                    int kk=k;
                    int xx=x+j*dir2[i][0],yy=y+j*dir2[i][1];
                    if(xx<1||xx>n||yy<1||yy>n) continue;
                    if(a[xx][yy]==k+1) kk++;
                    if(dp[xx][yy][z][t][kk]!=-1) continue;
                    dp[xx][yy][z][t][kk]=dp[x][y][z][t][k]+1;
                    q.push(node{xx,yy,z,t,kk});
                }
            }
        }

        else if(z==3)
        {
            for(int j=1;j<=n;j++)
            {
                for(int i=0;i<4;i++)
                {
                    int kk=k;
                    int xx=x+j*dir3[i][0],yy=y+j*dir3[i][1];
                    if(xx<1||xx>n||yy<1||yy>n) continue;
                    if(a[xx][yy]==k+1) kk++;
                    if(dp[xx][yy][z][t][kk]!=-1) continue;
                    dp[xx][yy][z][t][kk]=dp[x][y][z][t][k]+1;
                    q.push(node{xx,yy,z,t,kk});
                }
            }
        }
    }

    int minn=1e9;
    for(int i=1;i<=3;i++)
    {
        for(int j=0;j<=200;j++)
        {
            if(dp[ex][ey][i][j][n*n]!=-1)
            {
                minn=min(minn,dp[ex][ey][i][j][n*n]);
            }
        }
    }
    int flag=n*n;
    for(int i=1;i<=3;i++)
    {
        for(int j=200;j>=0;j--)
        {
            if(dp[ex][ey][i][j][n*n]==minn)
            {
                if(flag>j) flag=j;
            }
        }
    }

    cout<<minn<<" "<<flag<<endl;

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值