蓝桥杯(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);
	}
}
相关推荐
步行cgn2 小时前
carList.forEach(System.out::println)
java·开发语言·mybatis
plainGeekDev3 小时前
JUnit 4 → JUnit 5 + Kotlin
android·java·kotlin
plainGeekDev3 小时前
Mockito → MockK
android·java·kotlin
Zane19943 小时前
ThreadLocal 与 BlockingQueue:线程本地变量的内存泄漏原理,以及阻塞队列怎么选?
java·后端
用户3126874877203 小时前
Spring Bean 生命周期到底经历了什么?从实例化到销毁的全链路拆解
java·spring boot
Flittly3 小时前
【我的手搓轮子日记】(2)handmade-ioc 手搓 IOC 容器
java·spring boot·spring
阿昌喜欢吃黄桃3 小时前
Dubbo服务重启时注册中心下线不及时问题排查与修复记录
java·rpc·dubbo·问题排查
程序员钟霖3 小时前
【实战版】Spring IOC 控制反转详解
java·后端·spring·maven·intellij-idea
confiself4 小时前
树形思考研究进展
java·大数据·微服务
还是鼠鼠4 小时前
【Spring AI智能体实战 01】Java开发者接入大模型:国内平台、Token与API Key入门
java·大模型·spring ai·deepseek·阿里云百炼