第十四届蓝桥杯三月真题刷题训练——第 23 天

长草

原题链接

长草

问题描述

todo:这里复制题目描述

解题思路

BFS 模板题

参考代码

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

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    // static StreamTokenizer st = new StreamTokenizer(in);
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    static int[] dx = {0, 1, 0, -1};
    static int[] dy = {1, 0, -1, 0};
    static int N = 1010, n, m, k;
    static char[][] g = new char[N][N];
    static boolean[][] vis = new boolean[N][N];
    static Deque<int[]> dq = new ArrayDeque<>();
    
    public static void main(String[] args) throws Exception {
    	
    	String[] s = in.readLine().split(" ");
    	n = Integer.parseInt(s[0]);
    	m = Integer.parseInt(s[1]);
    	for (int i = 0; i < n; i++) {
    		String row = in.readLine();
    		for (int j = 0; j < m; j++) {
    			g[i][j] = row.charAt(j);
    			if (g[i][j] == 'g') {
    				dq.offerLast(new int[] {i, j});
    				vis[i][j] = true;
    			}
    		}
    	}
    	k = Integer.parseInt(in.readLine());
    	while (!dq.isEmpty()) {
    		if (k == 0) break;
    		int sz = dq.size();
    		for (int i = 0; i < sz; i++) {
    			int[] t = dq.pollFirst();
    			int x = t[0], y = t[1];
    			for (int j = 0; j < 4; j++) {
    				int nx = x + dx[j], ny = y + dy[j];
    				if (nx < 0 || ny < 0 || nx >= n || ny >= m || vis[nx][ny]) continue;
    				vis[nx][ny] = true;
    				g[nx][ny] = 'g';
    				dq.offerLast(new int[] {nx, ny});
    			}
    		}
    		k--;
    	}
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < m; j++) {
    			out.print(g[i][j]);
    		}
    		out.println("");
    	}

        out.flush();
        in.close();
    }  
}

蓝肽子序列

原题链接

蓝肽子序列

问题描述

todo:这里复制题目描述

解题思路

最长公共子序列模板题

参考代码

import java.io.*;

public class Main {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    static int N = 1010, n, m;
    static String[] ss = new String[N], tt = new String[N];
    static int[][] f = new int[N][N];
    
    /** 预处理 */
    public static void init(String s, String[] ss, int type) {
    	int len = s.length(), st = -1, idx = 1;
    	for (int i = 0; i < len; i++) {
    		char c = s.charAt(i);
    		if (Character.isUpperCase(c)) {
    			if (st != -1) {
    				ss[idx++] = s.substring(st, i);
    			}
    			st = i;
    		}
    	}
    	if (st != n - 1) ss[idx++] = s.substring(st, len);
    	if (type == 0) n = idx;
    	if (type == 1) m = idx;
    }
    
    public static void main(String[] args) throws Exception {
    	init(in.readLine(), ss, 0);
    	init(in.readLine(), tt, 1);
    	
    	for (int i = 1; i < n; i++) {
    		for (int j = 1; j < m; j++) {
    			if (ss[i].equals(tt[j])) {
    				f[i][j] = f[i - 1][j - 1] + 1;
    			} else {
    				f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
    			}
    		}
    	}
    	
    	out.println(f[n - 1][m - 1]);

        out.flush();
        in.close();
    }  
}

迷宫与陷阱

原题链接

迷宫与陷阱

问题描述

todo:这里复制题目描述

解题思路

todo:这里是解题思路

参考代码

todo:这里是参考代码

送礼物

原题链接

送礼物

问题描述

todo:这里复制题目描述

解题思路

todo:这里是解题思路

参考代码

todo:这里是参考代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值