Codeforces 1065D Three Pieces (多状态限制 BFS 搜索)

D. Three Pieces

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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的位置出发 每次可以有三种走法:

车 走直线,四个方向。一次可以走多个单位
象 走斜线,四个方向。一次可以走多个单位
马 走日字,八个方向,一次只能走一个单位
每走一次,需要花费一个精力值。如果切换了一次走法,则需要额外花费一个精力值(限制条件+1维)。而且要求,必须先访问1,再访问2,然后是3,以此类推,一直到n^2(限制条件+1维),但是,在从 i 走到 i+1的过程中,可以经过其它的点(但这个点必须时已经走过了的)

比如在3--->4中途可以在 2 or 1 点停一下,换走法,继续走。
问最少花费多少精力值可以走完n^2个点。精力最小的时候要求切换走法次数也最少(筛选条件+1维)。
输出最小精力和最少切换走法次数。

分析:
bfs搜索,状态可以这么设定dp[i][j][a][b][c]表示到达mp[i][j]这个点,用的是第a种走法,已经换了b次走法,已经按顺序访问了c个点,此时的最小精力值。
转移分为两种:

1.原地换一下方式

2.不换方式,按照当前方式继续走下去。

代码:

#include<bits/stdc++.h>
using namespace std;
int dp[12][12][3][200][102];//i j 当前方式 换次数 当前状态历史走过的最大值
///到达g[i][j]这个点,用的是第a种走法,已经换了b次走法,已经按顺序访问了c个点,此时的最小精力值。
int mp[12][12],sx,sy,ex,ey,n,m;
//0是日,1是直,2是斜线
int d1[8][2]= {1,2, -1,2, 1,-2, -1,-2, 2,1, -2,1, 2,-1, -2,-1}; //马
int d2[4][2]= {0,1, 0, -1, 1,0,-1,0}; //车
int d3[4][2]= {1,1, -1,1, 1,-1, -1,-1}; //象
struct node
{
  int x,y,a,b,c;
};
void dfs(int x,int y)
{
  memset(dp,-1,sizeof dp);
  dp[x][y][0][0][1]=dp[x][y][1][0][1]=dp[x][y][2][0][1]=0;//开始时的三种状态
  queue<node>q;
  while(!q.empty()) q.pop();
  q.push(node{x,y,0,0,1});
  q.push(node{x,y,1,0,1});
  q.push(node{x,y,2,0,1});
  while(!q.empty())
  {
    node tm=q.front();
    q.pop();
    int tx=tm.x,ty=tm.y,ta=tm.a,tb=tm.b,tc=tm.c,k=dp[tx][ty][ta][tb][tc];
    for(int i=0; i<3; i++) // 原地换状态
    {
      if(i==ta) continue;
      if(dp[tx][ty][i][tb+1][tc]!=-1) continue;
      dp[tx][ty][i][tb+1][tc]=k+1;
      q.push(node{tx,ty,i,tb+1,tc});
    }
    if(ta==0)//限制条件
      for(int i=0; i<8; i++) //车
      {
        int ti=tx+d1[i][0],tj=d1[i][1]+ty,cc=tc;
        if(ti>n||ti<1||tj>n||tj<1) continue;//越界
        if(mp[ti][tj]==tc+1) cc++;
        if(dp[ti][tj][ta][tb][cc]!=-1) continue;//该状态已走过
        dp[ti][tj][ta][tb][cc]=k+1;
        q.push(node{ti,tj,ta,tb,cc});
      }
    if(ta==1)
      for(int j=1; j<=10; j++) //走多个格
        for(int i=0; i<4; i++)
        {
          int ti=tx+d2[i][0]*j,tj=d2[i][1]*j+ty,cc=tc;
          if(ti>n||ti<1||tj>n||tj<1) continue;
          if(mp[ti][tj]==tc+1) cc++;
          if(dp[ti][tj][ta][tb][cc]!=-1) continue;
          dp[ti][tj][ta][tb][cc]=k+1;
          q.push(node{ti,tj,ta,tb,cc});

        }
    if(ta==2)
      for(int j=1; j<=10; j++)
        for(int i=0; i<4; i++)
        {
          int ti=tx+d3[i][0]*j,tj=d3[i][1]*j+ty,cc=tc;
          if(ti>n||ti<1||tj>n||tj<1) continue;
          if(mp[ti][tj]==tc+1) cc++;
          if(dp[ti][tj][ta][tb][cc]!=-1) continue;
          dp[ti][tj][ta][tb][cc]=k+1;
          q.push(node{ti,tj,ta,tb,cc});
        }
  }
}
int main()
{
  while(~scanf("%d",&n))
  {
    for(int i=1; i<=n; i++)
      for(int j=1; j<=n; j++)
      {
        scanf("%d",&mp[i][j]);
        if(mp[i][j]==1) sx=i,sy=j;
        if(mp[i][j]==n*n) ex=i,ey=j;

      }
    dfs(sx,sy);
    int mi=0x3f3f3f3f,ti=1;
    for(int j=0; j<=201; j++) //两个最小条件
      for(int i=0; i<3; i++)
      {
        if(dp[ex][ey][i][j][n*n]!=-1)
          mi=min(dp[ex][ey][i][j][n*n],mi);
      }
    for(int i=0; i<=201; i++)
    {
      int f=0;
      for(int j=0; j<3; j++)
      {
        if(dp[ex][ey][j][i][n*n]==mi)
        {
          ti=i;
          f=1;
          break;
        }
      }
      if(f) break;
    }
    printf("%d %d\n",mi,ti);
  }
}

猜你喜欢

转载自blog.csdn.net/qq_41668093/article/details/83042978