蓝桥杯(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);
	}
}
相关推荐
羑悻的小杀马特1 分钟前
【动态规划篇】欣赏概率论与镜像法融合下,别出心裁探索解答括号序列问题
c++·算法·蓝桥杯·动态规划·镜像·洛谷·空隙法
JaJian.2 分钟前
Java后端服务假死问题排查与解决
java·开发语言
救赎小恶魔4 分钟前
C++算法(5)
java·c++·算法
重生之后端学习44 分钟前
236. 二叉树的最近公共祖先
java·数据结构·算法·职场和发展·深度优先
Sun_gentle1 小时前
java.lang.RuntimeException: Could not load wrapper properties from ‘C:\Users\
java·开发语言·安卓
2501_901147831 小时前
有序数组单一元素查找:从通用解法到算法极致优化——兼谈高性能计算基础思路
算法·面试·职场和发展
笨蛋不要掉眼泪1 小时前
Nacos配置中心详解:核心用法、动态刷新与经典面试题解析
java·数据库·后端
橙露1 小时前
面向对象编程思想:Java 与 Python 的封装、继承与多态对比分析
java·开发语言·python
上海合宙LuatOS1 小时前
LuatOS核心库API——【io】 io操作(扩展)
java·服务器·前端·网络·单片机·嵌入式硬件·物联网
追随者永远是胜利者1 小时前
(LeetCode-Hot100)42. 接雨水
java·算法·leetcode·职场和发展·go