[蓝桥杯]真题讲解:AB路线(BFS+分层图)

[蓝桥杯]真题讲解:AB路线(BFS+分层图)

一、视频讲解

[蓝桥杯]真题讲解:AB路线(BFS+分层图)

二、正解代码

1、C++

cpp 复制代码
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e3 + 10;
int g[N][N];
bool st[N][N][20];
int dis[N][N][20];

int n, m, k;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

bool check(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

int bfs() {
	for(int i = 0; i < n; i ++) {
		for(int j = 0; j < m; j ++) {
			for(int p = 0; p < k; p ++) {
				dis[i][j][p] = INF;
			}
		}
	}
	queue<array<int,3>>q;
	q.push({0, 0, 1});
	dis[0][0][1] = 1;
	st[0][0][1] = true;
	while(q.size()){
		auto t = q.front();
		q.pop();
		int x = t[0], y = t[1], cnt = t[2];
		int d = dis[x][y][cnt];

		for(int i = 0; i < 4; i ++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			int nc = (d / k) % 2;
			if(check(nx, ny) && g[nx][ny] == nc && !st[nx][ny][(d + 1) % k]) {
				st[nx][ny][(d + 1) % k] = true;
				q.push({nx, ny, (d + 1) % k});

				dis[nx][ny][(d + 1) % k] = d + 1;
			}
		}
	}
	
	int minv = INF;
	for(int i = 0; i < k; i ++) {
		minv = min(minv, dis[n - 1][m - 1][i]);
	}
	return minv;
}

void solve(){
	cin >> n >> m >> k;
	for(int i = 0; i < n; i ++) {
		string s; cin >> s;
		for(int j = 0; j < m; j ++) {
			g[i][j] = (s[j] != 'A');
		}
	}

	int res = bfs();
	if(res == INF){
		cout << -1 << endl;
	}else{
		cout << res - 1 << endl;
	}

}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	while(t--){
		solve();
	}
	return 0;
}

2、python3

python 复制代码
from collections import deque
INF = 0x3f3f3f3f
N = 1010
n, m, k = map(int, input().split())
st = [[[False] * 20 for _ in range(N)] for _ in range(N)]
dis = [[[INF] * 20 for _ in range(N)] for _ in range(N)]
g = [[0] * N for _ in range(N)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def check(x, y):
    return x >= 0 and x < n and y >= 0 and y < m

def bfs():
    q = deque([(0, 0, 1)])
    dis[0][0][1] = 1
    st[0][0][1] = True
    while q:
        x, y, cnt = q[0]
        q.popleft()
        d = dis[x][y][cnt]
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            nc = (d // k) % 2
            if check(nx, ny) and g[nx][ny] == nc and st[nx][ny][(d + 1) % k] == False:
                st[nx][ny][(d + 1) % k] = True
                q.append((nx, ny, (d + 1) % k))
                dis[nx][ny][(d + 1) % k] = d + 1
    minv = INF
    for i in range(k):
        minv = min(minv, dis[n - 1][m - 1][i])
    return minv


for i in range(n):
    s = input()
    for j in range(m):
        if s[j] != 'A':
            g[i][j] = 1
        else:
            g[i][j] = 0
res = bfs()
if res == INF:
    print(-1)
else:
    print(res - 1)

3、Java

java 复制代码
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class ABRoad {

    public static final int INF = 0x3f3f3f3f;
    public static int[] dx = new int[]{-1, 0, 1, 0};
    public static int[] dy = new int[]{0, 1, 0, -1};
    public static int n, m, k;
    public static int[][] g;
    public static boolean[][][] st;
    public static int[][][] dis;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        g = new int[n + 10][m + 10];
        st = new boolean[n + 10][m + 10][k + 10];
        dis = new int[n + 10][m + 10][k + 10];
        for(int i = 0; i < n; i ++ ) {
            String s;
            s = sc.next();
            for(int j = 0; j < m; j ++) {
                if(s.charAt(j) != 'A')
                    g[i][j] = 1;
                else
                    g[i][j] = 0;
            }
        }
        int res = bfs();
        if(res == INF)
            System.out.println(-1);
        else
            System.out.println(res - 1);
    }

    private static int bfs() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (int p = 0; p < k; p++) {
                    dis[i][j][p] = INF;
                }
            }
        }
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{0, 0, 1});
        dis[0][0][1] = 1;
        st[0][0][1] = true;
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int x = t[0], y = t[1], cnt = t[2];
            int d = dis[x][y][cnt];
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                int nc = (d / k) % 2;
                if (check(nx, ny) && g[nx][ny] == nc && !st[nx][ny][(d + 1) % k]) {
                    st[nx][ny][(d + 1) % k] = true;
                    q.add(new int[]{nx, ny, (d + 1) % k});
                    dis[nx][ny][(d + 1) % k] = d + 1;
                }
            }
        }
        int minv = INF;
        for(int i = 0; i < k; i ++) {
            minv = Math.min(minv, dis[n - 1][m - 1][i]);
        }
        return  minv;
    }
    private static boolean check(int x, int y) {
        return x >= 0 && x < n && y >= 0 && y < m;
    }
}
相关推荐
YYJ333_3333 小时前
蓝桥杯 r格式(高精度*低精度)
c++·算法·蓝桥杯
hello_simon11 小时前
【Word转PDF】在线Doc/Docx转换为PDF格式 免费在线转换 功能强大好用
职场和发展·pdf·word·学习方法·word转pdf·石墨文档·word转换
武乐乐~13 小时前
欢乐力扣:赎金信
算法·leetcode·职场和发展
夏天的阳光吖14 小时前
C++蓝桥杯基础篇(四)
开发语言·c++·蓝桥杯
测试199815 小时前
接口测试工具:Postman
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
StickToForever16 小时前
第4章 信息系统架构(二)
经验分享·笔记·学习·职场和发展
查理零世17 小时前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
Joyner201820 小时前
python-leetcode-找到字符串中所有字母异位词
算法·leetcode·职场和发展
ll77881120 小时前
LeetCode每日精进:225.用队列实现栈
c语言·开发语言·数据结构·算法·leetcode·职场和发展
Helene19001 天前
Leetcode 224-基本计算器
算法·leetcode·职场和发展