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

灌溉

原题链接

灌溉

问题描述

todo:这里复制题目描述

解题思路

BFS,把每一个水管都添加到队列中,在某一个时刻,遍历队列中所有的点,看它的四个方向是否被灌溉过,没有的话就添加到队列中。当时间为 0 时退出循环。

参考代码

import java.util.*;
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 = 110, n, m, t, ans;
    static int[][] a = new int[N][N];
    static boolean[][] vis = new boolean[N][N];
    static int[] dx = {0, 1, 0, -1};
    static int[] dy = {1, 0, -1, 0};
    static Deque<int[]> dq = new ArrayDeque<>();
    
    public static void bfs() {
    	while (!dq.isEmpty()) {
    		if (t == 0) break;
    		int sz = dq.size();
    		for (int i = 0; i < sz; i++) {
    			int[] tt = dq.pollFirst();
    			int x = tt[0], y = tt[1];
    			for (int j = 0; j < 4; j++) {
    				int nx = x + dx[j], ny = y + dy[j];
    				if (nx <= 0 || nx > n || ny <= 0 || ny > m || vis[nx][ny]) continue;
    				vis[nx][ny] = true;
    				dq.offerLast(new int[] {nx, ny});
    				ans++;
    			}
    		}
    		t--;
    	}
    }
    
    public static void main(String[] args) throws Exception {
    	String[] s = in.readLine().split(" ");
    	n = Integer.parseInt(s[0]);
    	m = Integer.parseInt(s[1]);
    	t = Integer.parseInt(in.readLine());
    	
    	for (int i = 0; i < t; i++) {
    		s = in.readLine().split(" ");
    		int x = Integer.parseInt(s[0]), y = Integer.parseInt(s[1]);
    		if (!vis[x][y]) {
    			dq.offerLast(new int[] {x, y});
    			vis[x][y] = true;
    			ans++;
    		}
    	}
    	
    	t = Integer.parseInt(in.readLine());
    	
    	bfs();
    	
    	out.println(ans);
  	
        out.flush();
        in.close();
    }  
}

小朋友崇拜圈

原题链接

小朋友崇拜圈

问题描述

todo:这里复制题目描述

解题思路

按题意模拟就行,判断当前的小学生的一个崇拜关系链,如果长度超过数组长度,说明有环,直接退出循环即可,如果在循环的过程中回到了原点,说明可以构成,维护最大的一个长度即可。

参考代码

import java.util.*;
import java.math.*;
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 = (int)1e5 + 10, n;
    static int[] a = new int[N], cnt = new int[N];
    
    public static void main(String[] args) throws Exception {
    	n = Integer.parseInt(in.readLine());
    	String[] s = in.readLine().split(" ");
    	for (int i = 1; i <= n; i++) {
    		a[i] = Integer.parseInt(s[i - 1]);
    	}
    	int ans = 0;
    	for (int i = 1; i <= n; i++) {
    		int t = i, cur = a[i], cnt = 1;
    		while (cur != t) {
    			cnt++;
    			if (cnt > n) {
    				cnt = 0;
    				break;
    			}
    			cur = a[cur];
    		}
    		ans = Math.max(ans, cnt);
    	}
    	out.println(ans);
    	
        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、付费专栏及课程。

余额充值