【第十四届蓝桥杯三月真题刷题训练——第 25 天 (3.28)& 全球变暖 & 调手表 & 铺设道路 & 搬砖】

第一题:全球变暖

// 1:无需package
// 2: 类名必须Main, 不可修改

import java.util.Scanner;

public class Main {

    static int N = 1010;
    static char[][] g = new char[N][N];
    static boolean[][] st = new boolean[N][N];
    static int[] dx = {1, 0, -1, 0};
    static int[] dy = {0, -1, 0, 1};
    static long cnt1, cnt2;

    static int n;

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

    public static void dfs(int x, int y){
        cnt1++;
        boolean flag = true;
        for(int i = 0; i < 4; i++){
            int xx = x + dx[i], yy = y + dy[i];
            if(check(xx, yy)){
                if(g[xx][yy] == '.' && flag){
                    flag = false;
                }else if(g[xx][yy] == '#' && !st[xx][yy]){
                    st[xx][yy] = true;
                    dfs(xx, yy);
                }
            }
        }
        if(!flag)   cnt2++;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        s.nextLine();
        for(int i = 1; i <= n; i++){
            String str = s.nextLine();
            for(int j = 1; j <= n; j++){
                g[i][j] = str.charAt(j-1);
            }
        }

//        for (int i = 1; i <= n; i++) {
//            for (int j = 1; j <= n; j++) {
//                System.out.print(g[i][j]);
//            }
//            System.out.println();
//        }

        long res = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(g[i][j] == '#' && !st[i][j]){
                    st[i][j] = true;
                    cnt1 = 0; cnt2 = 0;
                    dfs(i, j);
                    if(cnt1 == cnt2){
                        res++;
                    }
                }
            }
        }
        System.out.println(res);
    }
}

第二题:调手表

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

public class Main {
    static int N = (int)1e5 + 10;
    static int[] dist = new int[N];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int k = Integer.parseInt(s[1]);
        Arrays.fill(dist, 0x3f3f3f3f);
        Queue<Integer> q = new LinkedList<>();
        dist[0] = 0;
        int res = 0;
        q.add(0);

        while(!q.isEmpty()){
            int t = q.poll();
            res = Math.max(res, dist[t]);
            if(dist[(t+1)%n] == 0x3f3f3f3f){
                dist[(t+1)%n] = dist[t]+1;
                q.add((t+1)%n);
            }
            if(dist[(t+k)%n] == 0x3f3f3f3f){
                dist[(t+k)%n] = dist[t] + 1;
                q.add((t+k)%n);
            }
        }

        System.out.println(res);
    }
}

第三题:铺设道路

补题----------------------------------------

第四题:搬砖

补题-----------------------------------------------

补了发评论区

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值