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

探讨在特殊棋盘上,利用骑士、主教和车三种棋子遍历所有标记点的最优策略,目标是最小化步数和更换棋子次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
  }
}

 

### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值