蓝桥杯(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);
	}
}
相关推荐
升鲜宝供应链及收银系统源代码服务3 分钟前
《IntelliJ + Claude Code + Gemini + ChatGPT 实战配置手册升鲜宝》
java·前端·数据库·chatgpt·供应链系统·生鲜配送
daidaidaiyu15 分钟前
Nacos实例一则及其源码环境搭建
java·spring
小江的记录本26 分钟前
【Redis】Redis全方位知识体系(附《Redis常用命令速查表(完整版)》)
java·数据库·redis·后端·python·spring·缓存
摇滚侠41 分钟前
Java 项目《谷粒商城-1》架构师级Java 项目实战,对标阿里 P6-P7,全网最强,实操版本
java·开发语言
zihao_tom1 小时前
Spring Boot(快速上手)
java·spring boot·后端
hua872222 小时前
SpringSecurity之跨域
java
小王不爱笑1322 小时前
G1 GC 的核心基础:Region 模型的补充细节
java·jvm·算法
salipopl2 小时前
Spring 中的 @ExceptionHandler 注解详解与应用
java·后端·spring
LJianK12 小时前
java封装
java·前端·数据库
海海不瞌睡(捏捏王子)2 小时前
C#知识点概要
java·开发语言·1024程序员节