蓝桥杯(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);
	}
}
相关推荐
IT-ZXT8882 分钟前
Spring 框架之IOC容器加载重要组件
java·后端·spring
xlsw_22 分钟前
MyBatis之测试添加功能
java·开发语言·mybatis
保持学习ing43 分钟前
黑马Java面试笔记之 消息中间件篇(RabbitMQ)
java·微服务·面试·java-rabbitmq
---wzy---1 小时前
docker生命周期
java·docker·容器
可可,天上人间1 小时前
path环境变量
java·环境·环境变量·path
何中应1 小时前
【设计模式-4.11】行为型——解释器模式
java·设计模式·解释器模式
程序员葵安1 小时前
【Java Web】9.Maven高级
java·数据库·后端·maven
界面开发小八哥1 小时前
「Java EE开发指南」如何使用MyEclipse在Web项目中用Web Fragments?
java·前端·ide·java-ee·eclipse·myeclipse
不想头秃a1 小时前
JavaEE初阶-网络编程
java·运维·服务器·网络
饮长安千年月2 小时前
JavaSec-SSTI - 模板引擎注入
java·windows·安全·web安全·网络安全·系统安全·安全架构