蓝桥杯(3.11)

1233. 全球变暖

java 复制代码
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

public class Main{
	static int n;
	static final int N = 1010;
	static char[][] g = new char[N][N];
	static boolean[][] st = new boolean[N][N];
	
	public static boolean bfs(int sx,int sy) {
		Deque<int[]> dq = new LinkedList<>();
		dq.addLast(new int[] {sx,sy});
		st[sx][sy] = true;
		int total = 0,bound = 0;
		int[] dx = {1,-1,0,0};
		int[] dy = {0,0,1,-1};		
		while(!dq.isEmpty()) {
			int[] t = dq.pollFirst();
			total++;
			boolean is_bound = false;
			for(int i=0;i<4;i++) {
				int x = t[0]+dx[i],y = t[1]+dy[i];
				if(x < 0 ||x > (n-1) || y < 0 || y > (n-1))
					continue;
				if(st[x][y])
					continue;
				if(g[x][y] == '.') {
					is_bound = true;
					continue;
				}
				dq.addLast(new int[] {x,y});
				st[x][y] = true;
			}
			if(is_bound) bound++;
		}
		return total == bound;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    n = sc.nextInt();
        for(int i = 0;i < n;i++)
        {
            char[] charArray = sc.next().toCharArray();
            for(int j = 0;j < n;j++)
            {
                g[i][j] = charArray[j];
            }
        }
        
        int cnt = 0;
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < n;j++)
            {
                if(!st[i][j] && g[i][j] == '#')
                {
                    if(bfs(i,j)) cnt ++;
                }
            }
        }
        System.out.println(cnt);
	}
}
相关推荐
ideal-cs12 小时前
总结:Nginx配置文件案例说明
java·运维·nginx·nginx配置文件
无尽的沉默13 小时前
Thymeleaf 基本语法和表达式
java·开发语言
yzx99101313 小时前
蓝桥杯备考智能体:构建高并发、智能化编程竞赛助手的深度实践
职场和发展·蓝桥杯
Coder_Boy_13 小时前
Java后端核心技术体系全解析(个人总结)
java·开发语言·spring boot·分布式·spring cloud·中间件
南部余额13 小时前
函数式接口 Lambda 表达式好搭档:Predicate、Function、Consumer、Supplier
java·开发语言·consumer·lambda·function·predicate·supplier
Java后端的Ai之路13 小时前
【JDK】-JDK 17 新特性整理(比较全)
java·开发语言·后端·jdk17
小小小米粒13 小时前
Spring Boot Starter ,不止是 “打包好配置的工具类包”
java·开发语言
期末考复习中,蓝桥杯都没时间学了14 小时前
力扣刷题23
算法·leetcode·职场和发展
用户83071968408214 小时前
告别冗余!Spring Boot Web 入参转换 6 种玩法,@InitBinder 可以退休了
java·spring boot
Desirediscipline14 小时前
#include<limits>#include <string>#include <sstream>#include <iomanip>
java·开发语言·前端·javascript·算法