Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Solution {
- class Pair {
- int a;
- int b;
- public Pair(int a, int b) {
- this.a = a;
- this.b = b;
- }
- }
- void solve() {
- int n = in.nextInt();
- int m = in.nextInt();
- char[][] map = in.nextCharMap(n, m);
- int ans = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (map[i][j] == 'x') {
- ans++;
- bfs(i, j, map, n, m);
- }
- }
- }
- out.println(ans);
- }
- void bfs(int i, int j, char[][] map, int n, int m) {
- ArrayDeque<Pair> queue = new ArrayDeque<>();
- queue.add(new Pair(i, j));
- while (!queue.isEmpty()) {
- Pair cur = queue.poll();
- map[cur.a][cur.b] = 'o';
- if (cur.a - 1 >= 0 && map[cur.a - 1][cur.b] == 'x') {
- queue.add(new Pair(cur.a - 1, cur.b));
- }
- if (cur.a + 1 < n && map[cur.a + 1][cur.b] == 'x') {
- queue.add(new Pair(cur.a + 1, cur.b));
- }
- if (cur.b - 1 >= 0 && map[cur.a][cur.b - 1] == 'x') {
- queue.add(new Pair(cur.a, cur.b - 1));
- }
- if (cur.b + 1 < m && map[cur.a][cur.b + 1] == 'x') {
- queue.add(new Pair(cur.a, cur.b + 1));
- }
- }
- }
- Scanner in;
- PrintWriter out;
- void run() {
- try {
- in = new Scanner(new File("A.in"));
- out = new PrintWriter(new File("A.out"));
- solve();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- void runIO() {
- in = new Scanner(System.in);
- out = new PrintWriter(System.out);
- solve();
- out.close();
- }
- class Scanner {
- BufferedReader br;
- StringTokenizer st;
- public Scanner(File f) {
- try {
- br = new BufferedReader(new FileReader(f));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public Scanner(InputStream f) {
- br = new BufferedReader(new InputStreamReader(f));
- }
- String next() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return null;
- st = new StringTokenizer(s);
- }
- return st.nextToken();
- }
- private boolean isSpaceChar(int c) {
- return !(c >= 33 && c <= 126);
- }
- private int skip() {
- int b;
- while ((b = read()) != -1 && isSpaceChar(b)) ;
- return b;
- }
- private int read() {
- int res = -1;
- try {
- res = br.read();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return res;
- }
- char[] nextCharArray(int size) {
- char[] buf = new char[size];
- int b = skip(), p = 0;
- while (p < size && !(isSpaceChar(b))) {
- buf[p++] = (char) b;
- b = read();
- }
- return size == p ? buf : Arrays.copyOf(buf, p);
- }
- char[][] nextCharMap(int n, int m) {
- char[][] map = new char[n][];
- for (int i = 0; i < n; i++) {
- map[i] = nextCharArray(m);
- }
- return map;
- }
- boolean hasMoreTokens() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return false;
- st = new StringTokenizer(s);
- }
- return true;
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- }
- public static void main(String[] args) {
- new Solution().runIO();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement