蓝桥杯(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);
	}
}
相关推荐
范什么特西9 分钟前
配置Maven准备工作
java·maven
海兰41 分钟前
Elasticsearch Java 客户端(9.x)
java·elasticsearch·jenkins
重生之后端学习1 小时前
17. 电话号码的字母组合
java·开发语言·数据结构·算法·深度优先
仰泳的熊猫1 小时前
题目1545:蓝桥杯算法提高VIP-现代诗如蚯蚓
数据结构·c++·算法·蓝桥杯
TracyCoder1231 小时前
LeetCode Hot100(57/100)——5. 最长回文子串
算法·leetcode·职场和发展
蜜獾云1 小时前
JAVA面试题速记-redis知识点
java·开发语言·redis
Drifter_yh1 小时前
「JVM」 深入理解 StringTable:从底层编译优化到 intern 核心解密
java·jvm
JavaLearnerZGQ1 小时前
Spring Boot 流式响应接口核心组件解析
java·spring boot·后端
cur1es1 小时前
【TCP 协议的相关特性】
java·网络·网络协议·tcp/ip·tcp·滑动窗口·连接管理
我命由我123451 小时前
Photoshop - Photoshop 工具栏(68)内容感知移动工具
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop