蓝桥杯(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);
	}
}
相关推荐
消失的旧时光-19437 小时前
第二十二课:领域建模实战——订单系统最小闭环(实战篇)
java·开发语言·spring boot·后端
J_liaty7 小时前
Java Stream流常用方法归纳整理
java·stream
Y001112367 小时前
Day19—集合进阶-3
java·开发语言
!停7 小时前
数据结构二叉树—链式结构(中)
java·数据结构·算法
dcmfxvr7 小时前
【无标题】
java·linux·前端
llz_1127 小时前
蓝桥杯备赛-搜索(DFS/BFS)
c++·算法·蓝桥杯·深度优先·宽度优先
康小庄7 小时前
Java读写锁降级
java·开发语言·spring boot·python·spring·java-ee
流云细水8 小时前
Spec(规格说明书)与Skill(技能库)实操指南
java·人工智能
毕设源码-钟学长8 小时前
【开题答辩全过程】以 基于Java的停车场信息管理系统设计与实现为例,包含答辩的问题和答案
java·开发语言
rannn_1118 小时前
【苍穹外卖|Day7】缓存菜品、缓存套餐、添加购物车、查看购物车、清空购物车
java·spring boot·redis·后端·缓存·项目