Train your English by the way:
Because of the recent rain, farmer John's farm has many puddles (represented by a NM matrix, | representative, rectangle matrix). Each area is represented by water (w) or land (.), farmer jhon wants to point out how many puddles have been formed in his rural area. A puddle is formed by a water and eight adjacent areas connected together.
Give a schematic diagram of a farm and guess how many water pits there are (square)
Input:
Two numbers N and M in the first row
Second line M characters one line
Output:
Number
Baidu Translate
Due to the recent rainfall, water has gathered in all parts of Farmer John's farm, represented by a rectangle of n x m (1 < = n < = 100; 1 < = m < = 100). Each square has water ("W") or dry land ("W"). Farmer John wants to know how many ponds have formed in his field. A pond is a set of connected squares, in which there is water, and a square is considered to be adjacent to eight neighbors. Give a chart of Farmer John's field to determine how many ponds he has.
Solution 1:
import java.util.Scanner; public class Main { static char[][] c; static boolean flag[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int N = sc.nextInt(); int M = sc.nextInt(); c = new char[N][M]; //assignment flag = new boolean[c.length][c[0].length]; //assignment for (int i = 0; i < N; i++) { String s = sc.next(); c[i] = s.toCharArray(); //assignment for (int j = 0; j < M; j++) { flag[i][j] = false; //Initialization } } int s = 0; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { if (flag[i][j] != true) { //If this value has not been read // flag[i][j] = true; redundant because the following functions will be assigned again. If assigned here, each value will fail to pass the / / assigned read status. if (c[i][j] == 'W') { //Depth traversal if w dfs(c, i, j, flag); //All conditions met s++; } } } } System.out.println(s); } } public static void dfs(char[][] arr, int x, int y, boolean[][] temp) { if (x < 0 || x >= c.length || y >= c[0].length || y < 0) //Jump out of range return; if (temp[x][y] == true) // Description visited return; if (arr[x][y] == '.') { //Is. That is, the condition is not met temp[x][y] = true; //And give read status return; } temp[x][y] = true; //All that's left are unread w dfs(arr, x, y + 1, temp); // Value true for traversal of surrounding qualified positions dfs(arr, x, y - 1, temp); dfs(arr, x + 1, y, temp); dfs(arr, x + 1, y + 1, temp); dfs(arr, x + 1, y - 1, temp); dfs(arr, x - 1, y, temp); dfs(arr, x - 1, y + 1, temp); dfs(arr, x - 1, y - 1, temp); } }
Solution two:
Change W directly to.!! To indicate read
import java.util.Scanner; public class Main { static char[][] field; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int N = sc.nextInt(); int M = sc.nextInt(); field = new char[N][M]; // assignment for (int i = 0; i < N; i++) { String s = sc.next(); field[i] = s.toCharArray(); // assignment } int s = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (field[i][j] == 'W') { dfs(i, j, N, M); s++; } } } System.out.println(s); } } public static void dfs(int x, int y, int N, int M) { field[x][y] = '.'; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { int nx = x + i; int ny = y + j; if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W') dfs(nx, ny, N, M); } } } }