[蓝桥杯]真题讲解: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;
    }
}
相关推荐
好心的小明2 小时前
【蓝桥杯】CB组国二攻略(省赛地点:广东)
职场和发展·蓝桥杯
ღ 金龍戲水 ღ3 小时前
蓝桥杯竞赛单片机组备赛【经验帖】
经验分享·单片机·蓝桥杯
_OLi_4 小时前
力扣 LeetCode 459. 重复的子字符串(Day4:字符串)
算法·leetcode·职场和发展·kmp
ahadee4 小时前
蓝桥杯每日真题 - 第12天
c++·vscode·算法·蓝桥杯
Xiao Fei Xiangζั͡ޓއއ5 小时前
一觉睡醒,全世界计算机水平下降100倍,而我却精通C语言——scanf函数
c语言·开发语言·笔记·程序人生·面试·蓝桥杯·学习方法
_OLi_5 小时前
力扣 LeetCode 150. 逆波兰表达式求值(Day5:栈与队列)
算法·leetcode·职场和发展
糊涂君-Q6 小时前
Python小白学习教程从入门到入坑------第三十一课 迭代器(语法进阶)
python·学习·程序人生·考研·职场和发展·学习方法·改行学it
路遇晚风8 小时前
力扣=Mysql-3322- 英超积分榜排名 III(中等)
mysql·算法·leetcode·职场和发展
ahadee12 小时前
蓝桥杯每日真题 - 第10天
c语言·vscode·算法·蓝桥杯
吾店云建站19 小时前
WordPress 6.7 “Rollins”发布
科技·程序人生·职场和发展·创业创新·程序员创富