【第十四届蓝桥杯三月真题刷题训练——第 23 天 (3.26)& 长草 & 蓝肽子序列 & 迷宫与陷阱 & 送礼物】

第一题:长草

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
	
	static int N = 1010;
	static char[][] g = new char[N][N];
	static Queue<int[]> q = new LinkedList<>();
	static int[] dx = {0, 1, 0, -1};
	static int[] dy = {1, 0, -1, 0};
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		
		
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int m = Integer.parseInt(s[1]);
		
		for(int i = 1; i <= n; i++) {
			char[] c = br.readLine().toCharArray();
			for(int j = 1; j <= m; j++) {
				g[i][j] = c[j-1];
				if(g[i][j] == 'g') q.add(new int[] {i, j});
			}
		}
		
		int k = Integer.parseInt(br.readLine());
		while(!q.isEmpty() && k-->0) {
			int len = q.size();
			while(len-->0) {
				int[] t = q.poll();
				int x = t[0], y = t[1];
				
				for(int i = 0; i < 4; i++) {
					int tX = x + dx[i];
					int tY = y + dy[i];
					if(tX >= 1 && tX <= n && tY >= 1 && tY <= m && g[tX][tY] == '.') {
						g[tX][tY] = 'g';
						q.add(new int[] {tX, tY});
					}
				}
			}
			
		}
		
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j++) {
				out.print(g[i][j]);
			}
			out.println();
		}
		out.flush();
	}
}

第二题:蓝肽子序列

import java.util.Scanner;

public class Main {
    static int N = 1010;
    static int[][] dp = new int[N][N];

    public static String[] getStringArray(String s){
        char[] c = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < c.length; i++){
            if(c[i] >= 'A' && c[i] <= 'Z'){
                sb.append(" ").append(c[i]);
            }else{
                sb.append(c[i]);
            }
        }
        return sb.toString().trim().split(" ");
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String[] s1 = getStringArray(s.next());
        String[] s2 = getStringArray(s.next());
        int len1 = s1.length;
        int len2 = s2.length;
        int res = 0;
//        System.out.println(len1 + "  " + len2);
        for(int i = 1; i <= len1; i++){
            for(int j = 1; j <= len2; j++){
                if(s1[i-1].equals(s2[j-1]))  dp[i][j] = dp[i-1][j-1]+1;
                else{
                    dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);
                }
            }
        }
        System.out.println(dp[len1][len2]);
    }
}
/*
WowqWowqGzYsnYsnSzdwkWowqYsnBiwucTdiCehBkxaBiwucYsnCehBiwucTdiCehWowqBkxa CehCehKdCehBiwucYsnCehGzGzWowqYsnWowqTdiSzdwkBiwucGzWowqWowqCeh
 */

第三题:迷宫与陷阱

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class 迷宫与陷阱bfs {
    static int N = 1010;
    static char[][] g = new char[N][N];
    static int k;
    static int n;
    static boolean[][][] st = new boolean[N][N][11];
    static Queue<Node> q = new LinkedList<>();
    static int[] dx = {1, 0, -1, 0};
    static int[] dy = {0, -1, 0, 1};

    public static boolean check(int x, int y){
        if(x >= 1 && x <= n && y >= 1 && y <= n)    return true;
        return false;
    }

    static class Node{
        int x, y, k, s;
        public Node(int x, int y, int k, int s){
            this.x = x;
            this.y = y;
            this.k = k;
            this.s = s;
        }
    }

    public static int bfs(){
        st[1][1][0] = true;

        q.add(new Node(1, 1, 0, 0));
        while(!q.isEmpty()){
            Node t = q.poll();
            int x = t.x;
            int y = t.y;
//            System.out.println(x + " " + y + " " + t.s);
            if(x == n && y == n)   {
                return t.s;
            }
            for(int i = 0; i < 4; i++){
                int xx = x + dx[i];
                int yy = y + dy[i];
                if(!check(xx, yy) || g[xx][yy] == '#')  continue;
                if(g[xx][yy] == '%' && !st[xx][yy][k]){
                    st[xx][yy][k] = true;
                    q.add(new Node(xx, yy, k, t.s+1));
                }else{
                    if(t.k > 0 && !st[xx][yy][t.k-1]){
                        st[xx][yy][t.k-1] = true;
                        q.add(new Node(xx, yy, t.k-1, t.s + 1));
                    }else if(g[xx][yy] == '.' && !st[xx][yy][0] && t.k == 0){
                        st[xx][yy][0] = true;
                        q.add(new Node(xx, yy, 0, t.s + 1));
                    }
                }
            }
        }

        return -1;
    }


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" +");
        n = Integer.parseInt(s[0]);
        k = Integer.parseInt(s[1]);
        for(int i = 1; i <= n; i++){
            char[] c = br.readLine().toCharArray();
            for(int j = 1; j <= n; j++){
                g[i][j] = c[j-1];
            }
        }
        System.out.println(bfs());
    }
}
/*
5 3
...XX
##%#.
XX.#X
.###X
.X...
 */

第四题:送礼物

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值