1. The opening ceremony of the World Cup will be held in Stadium C. The fans stand of Stadium C can accommodate M*N fans. After the sale of tickets for the stadium has been completed, the government now wants to count the number of football fans in the opening ceremony and the number of football fans in the largest football team.
Through investigation and research, it is found that fans have the following characteristics in the selection of seats:
The fans of the same team will choose adjacent seats, and the fans of different teams will choose non-adjacent seats (Note: adjacent includes front and back adjacent, left and right adjacent, diagonal adjacent).
Given a two-dimensional course of M*N, 0 represents that there is no seat in the position, and 1 represents that the position has been selected, hoping to output the number of team groups P, the largest number of team groups Q.
Input Description:
Line 1, 2 digits, M and N, separated by commas in English
Next M lines, the number of N in each line, separated by commas in English
Output description:
One line, two numbers, P and Q, separated by commas in English
Among them, P means the number of team groups and Q means the largest number of team groups.
Example: input
10,10
0,0,0,0,0,0,0,0,0,0
0,0,0,1,1,0,1,0,0,0
0,1,0,0,0,0,0,1,0,1
1,0,0,0,0,0,0,0,1,1
0,0,0,1,1,1,0,0,0,1
0,0,0,0,0,0,1,0,1,1
0,1,1,0,0,0,0,0,0,0
0,0,0,1,0,1,0,0,0,0
0,0,1,0,0,1,0,0,0,0
0,1,0,0,0,0,0,0,0,0
Output: 6,8
java implementation:
public class Problem1 { static String input = "10,10\n" + "0,0,0,0,0,0,0,0,0,0\n" + "0,0,0,1,1,0,1,0,0,0\n" + "0,1,0,0,0,0,0,1,0,1\n" + "1,0,0,0,0,0,0,0,1,1\n" + "0,0,0,1,1,1,0,0,0,1\n" + "0,0,0,0,0,0,1,0,1,1\n" + "0,1,1,0,0,0,0,0,0,0\n" + "0,0,0,1,0,1,0,0,0,0\n" + "0,0,1,0,0,1,0,0,0,0\n" + "0,1,0,0,0,0,0,0,0,0"; static class Person { int num; int reach; public Person(int num, int reach) { this.num = num; this.reach = reach; } @Override public String toString() { return "Person{" + "num=" + num + ", reach=" + reach + '}'; } } static Person[][] handleInput() { String[] lines = input.split("\n"); String[] first = lines[0].split(","); int row = Integer.valueOf(first[0]); int col = Integer.valueOf(first[1]); Person[][] arr = new Person[row][col]; String[] line; for (int i = 1; i < lines.length; i++) { line = lines[i].split(","); for (int j = 0; j < line.length; j++) { arr[i - 1][j] = new Person(Integer.valueOf(line[j]), 0); } } return arr; } static void searchMaxPerson(Person[][] arr) { int row = arr.length; int col = arr[0].length; int tmp ; int max = 0; int count = 0; Person cur; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cur = arr[i][j]; if (cur.num != 0 && cur.reach == 0) { count ++; tmp = searchAll(arr,i,j); if(tmp > max){ max = tmp; } } } } System.out.println(count+","+max); } static int searchAll(Person[][] arr, int r, int c) { int row = arr.length; int col = arr[0].length; if (r >= row || r < 0 || c >= col || c < 0) { return 0; } Person cur = arr[r][c]; if (cur.num != 0 && cur.reach == 0) { cur.reach = 1; int n1 = searchAll(arr, r - 1, c) + searchAll(arr, r + 1, c); int n2 = searchAll(arr, r - 1, c - 1) + searchAll(arr, r + 1, c + 1); int n3 = searchAll(arr, r, c - 1) + searchAll(arr, r, c + 1); int n4 = searchAll(arr, r - 1, c + 1) + searchAll(arr, r + 1, c - 1); return n1 + n2 + n3 + n4 + 1; } return 0; } public static void main(String[] args) { Person[][] arr = handleInput(); searchMaxPerson(arr); } }