java池塘_AcWing 1097. java池塘计数

import java.lang.*;

import java.io.*;

import java.util.*;

class Main{

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

static StringTokenizer stz = new StringTokenizer("");

static String nextLine() throws Exception {// 读取下一行字符串

return br.readLine();

}

static String next() throws Exception {// 读取下一个字符串

while (!stz.hasMoreTokens()) {

stz = new StringTokenizer(br.readLine());

}

return stz.nextToken();

}

static int nI() throws Exception {// 读取下一个int型数值

return Integer.parseInt(next());

}

static double nD() throws Exception {// 读取下一个double型数值

return Double.parseDouble(next());

}

static long nL() throws Exception {// 读取下一个double型数值

return Long.parseLong(next());

}

static void write(String str) throws Exception{

bw.write(str);

}

static String itoS(int i){

return Integer.toString(i);

}

static void wI(int i) throws Exception{

write(Integer.toString(i));

}

static void flush() throws Exception{

bw.flush();

}

public static void main(String[] args) throws Exception{

new Main().run();

}

class Point{

public int x, y;

Point(int x, int y){

this.x = x;

this.y = y;

}

}

Queue q = new LinkedList<>();

void bfs(Point p){

st[p.x][p.y] = true;

q.add(p);

while (!q.isEmpty()){

Point t = q.poll();

// 考虑八个方向

for (int i = -1; i <= 1 ; i++) {

for (int j = -1; j <= 1 ; j++) {

int a = t.x + i, b = t.y + j;

if (i == 0 && j == 0) continue;

// 合法性判断

if (a >= 1 && a <= n && b >= 1 && b <= m && g[a][b] == 'W' && !st[a][b]){

st[a][b] = true;

q.add(new Point(a, b));

}

}

}

}

}

int n, m;

final int N = 1010;

boolean[][] st = new boolean[N][N];

char[][] g = new char[N][N];

public void run() throws Exception{

n = nI(); m = nI();

for (int i = 1; i <= n; i++) {

String str = nextLine();

for (int j = 1; j <= m ; j++) {

g[i][j] = str.charAt(j-1);

}

}

int res = 0;

for (int i = 1; i <= n ; i++) {

for (int j = 1; j <= m; j++) {

char ch = g[i][j];

// 是水洼, 并且没有被遍历过

if (ch == 'W' && !st[i][j]){

res++;

bfs(new Point(i, j));

}

}

}

wI(res);

bw.write('\n');

flush();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值