题解:P16292 [蓝桥杯 2026 省 Java A 组] 两栖作战

· · 题解

注意到:

}; priority_queue<cla>q;

## Step 2
正常广搜,特判是否变换类型,累加变换次数:
```cpp line-numbers
ll bfs()
{
    q.push({1,1,0});
    vis[1][1]=1;
    while(!q.empty())
    {
        cla k=q.top();
        q.pop();
        if(k.x==n&&k.y==n)
        {
            return k.ans;
        }
        for(int i=0;i<4;i++)
        {
            ll xx=k.x+dx[i];
            ll yy=k.y+dy[i];
            ll ans=k.ans;
            if(a[xx][yy]!=a[k.x][k.y])//特判
            {
                ans++;
            }
            if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&!vis[xx][yy])
            {
                q.push({xx,yy,ans});
                vis[xx][yy]=1;
            }
        }
    }
}

:::success[AC Code]{open}

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
const ll dx[4]={-1,0,1,0};
const ll dy[4]={0,1,0,-1};
ll n;
char a[5010][5010];
bool vis[5010][5010];
class cla
{
    private:

    public:
        ll x;
        ll y;
        ll ans;
        bool operator<(const cla&k)const
        {
            return ans>k.ans;
        }
    protected:

};
priority_queue<cla>q;
struct str
{

};
namespace 
{

};
ll bfs()
{
    q.push({1,1,0});
    vis[1][1]=1;
    while(!q.empty())
    {
        cla k=q.top();
        q.pop();
        if(k.x==n&&k.y==n)
        {
            return k.ans;
        }
        for(int i=0;i<4;i++)
        {
            ll xx=k.x+dx[i];
            ll yy=k.y+dy[i];
            ll ans=k.ans;
            if(a[xx][yy]!=a[k.x][k.y])
            {
                ans++;
            }
            if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&!vis[xx][yy])
            {
                q.push({xx,yy,ans});
                vis[xx][yy]=1;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<bfs();
    return 0;
}

::: :::info[JAVA 题解]

import java.io.*;
import java.util.*;

public class Main {
    // 四个方向:上下左右
    static int[] dx = {-1, 0, 1, 0};
    static int[] dy = {0, 1, 0, -1};

    static class Node implements Comparable<Node> {
        int x, y, cost;

        public Node(int x, int y, int cost) {
            this.x = x;
            this.y = y;
            this.cost = cost;
        }

        // 小根堆:代价小的优先出队
        @Override
        public int compareTo(Node o) {
            return this.cost - o.cost;
        }
    }

    public static void main(String[] args) throws IOException {
        // 快读(N=5000必须用快读,否则超时)
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        char[][] grid = new char[n + 2][n + 2]; // 1-based

        for (int i = 1; i <= n; i++) {
            String line = br.readLine();
            for (int j = 1; j <= n; j++) {
                grid[i][j] = line.charAt(j - 1);
            }
        }

        // dist[x][y] = 到达(x,y)的最小切换次数
        int[][] dist = new int[n + 2][n + 2];
        for (int i = 0; i <= n; i++)
            Arrays.fill(dist[i], 0x3f3f3f3f);

        PriorityQueue<Node> pq = new PriorityQueue<>();
        dist[1][1] = 0;
        pq.add(new Node(1, 1, 0));

        while (!pq.isEmpty()) {
            Node now = pq.poll();
            int x = now.x;
            int y = now.y;
            int cost = now.cost;

            // 到达终点直接输出
            if (x == n && y == n) {
                System.out.println(cost);
                return;
            }

            // 这个点已经有更优解了,跳过
            if (cost > dist[x][y]) continue;

            // 四个方向扩展
            for (int d = 0; d < 4; d++) {
                int nx = x + dx[d];
                int ny = y + dy[d];

                if (nx < 1 || nx > n || ny < 1 || ny > n) continue;

                // 计算新代价:地形不同则+1
                int newCost = cost;
                if (grid[nx][ny] != grid[x][y]) newCost++;

                // 更优才更新
                if (newCost < dist[nx][ny]) {
                    dist[nx][ny] = newCost;
                    pq.add(new Node(nx, ny, newCost));
                }
            }
        }
    }
}
此代码由豆包依 C++ 编写而成。

:::