第十四届蓝桥杯 三十天刷题 第二十五天

哈喽大家好啊,这几天比较忙,只是发了打卡帖子,没有写博客,刷题也质量不高,下周六就比赛了,好好调整一下,要好好刷题了,加油!

目录

全球变暖

 问题描述

输入输出样例

运行限制

 思路分享

 参考代码


1.全球变暖

 问题描述

 

输入输出样例

示例

输入

7
.......
.##....
.##....
....##.
..####.
...###.
.......

输出

1

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 256M

 思路分享

这是一道BFS模板题。对于这道题,我们要判断每个岛屿(连通块)当中是否存在一块陆地('#')的四周全都是陆地,如果存在这样的陆地,那么说明这座岛屿不会被淹没,否则他就会被淹没。

 参考代码

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName 全球变暖
 * @Author @浅夜
 * @Date 2023/3/28 15:31
 * @Version 1.0
 */

public class 全球变暖 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static int[] dx = {1, -1, 0, 0};
    static int[] dy = {0, 0, 1, -1};
    static int n = 1010;
    static boolean[][] st;
    static char[][] a;
    static int ans = 0;
    static boolean flag = false;

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(br.readLine());
        a = new char[n][n];
        st = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            a[i] = br.readLine().toCharArray();
        }
        Queue<int[]> q = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i][j] == '#' && !st[i][j]) {
                    q.offer(new int[]{i,j});
                    st[i][j] = true;
                    bfs(q);
                    if(!flag) ans++;
                    flag = false;
                }
            }
        }
        out.println(ans);
        out.flush();

    }
    static void bfs(Queue<int[]> q) {
        while (!q.isEmpty()) {
            int size = q.size();
            while (size-- > 0) {
                int[] tmp = q.poll();
                int x = tmp[0];
                int y = tmp[1];
               if(check(x + dx[0],y + dy[0])&& check(x + dx[1],y + dy[1]) && check(x + dx[2],y + dy[2]) && check(x + dx[3],y + dy[3])){
                   flag = true;
               }
                for (int i = 0; i < 4; i++) {
                    int newX = x + dx[i];
                    int newY = y + dy[i];
                    if (!st[newX][newY] && a[newX][newY] == '#') {
                        st[newX][newY] = true;
                        q.offer(new int[]{newX, newY});
                    }
                }
            }
        }
    }
    static boolean check(int x,int y){
        return a[x][y] == '#';
    }
}

 

2.调手表

 问题描述

 

输入描述

一行两个整数 �,� (0<�<�≤105)n,k (0<k<n≤105),意义如题。

输出描述

输出一行一个整数,表示按照最优策略按键,从一个时间调到另一个时间最多要按多少次。

输入输出样例

示例

输入

5 3

输出

2

样例解释

如果时间正确则按 0 次。否则要按的次数和操作系列之间的关系如下:

1:+1

2:+1, +1

3:+3

4:+3, +1

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 256M

参考代码

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName 调手表
 * @Author @浅夜
 * @Date 2023/3/28 22:18
 * @Version 1.0
 */

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int n = 100010;
    static int[] a = new int[n];

    public static void main(String[] args) throws IOException {
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        int k = Integer.parseInt(s[1]);
        Arrays.fill(a,Integer.MAX_VALUE);
        Queue<Integer> q = new LinkedList<>();
        a[0] = 0;
        int ans = 0;
        q.add(0);
        while(!q.isEmpty()){
            int t = q.poll();
            ans = Math.max(ans,a[t]);
            if(a[(t + 1)%n]==Integer.MAX_VALUE){
                a[(t + 1)%n] = a[t] + 1;
                q.add((t + 1)%n);
            }
            if(a[(t + k)%n] == Integer.MAX_VALUE){
                a[(t+k)%n] = a[t] + 1;
                q.add(a[(t+k)%n]);
            }
        }
        System.out.println(ans);
    }
}



加油加油

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值