游游拿到了一个字符矩阵。她想知道有多少个2*2的子矩阵同时包含了’y’、’o’和’u’这三种字符?
区块链毕设网qklbishe.com为您提供问题的解答
游游拿到了一个字符矩阵。她想知道有多少个2*2的子矩阵同时包含了’y’、’o’和’u’这三种字符?
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int rows = scanner.nextInt();
int cols = scanner.nextInt();
char[][] matrix = new char[rows][cols];
int count = 0;
Set<Character> set = new HashSet<>();
set.add(‘y’);
set.add(‘o’);
set.add(‘u’);
for (int i = 0; i < rows; i++) {
String row = scanner.next();
for (int j = 0; j < cols; j++) {
char c = row.charAt(j);
matrix[i][j] = c;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if(set.contains(matrix[i][j])){
set.remove(matrix[i][j]);
for (int k = 0; k < matrix.length; k++) {
if(set.contains(matrix[k][j])) {
set.remove(matrix[k][j]);
for(int l = 0; l < matrix[0].length ; l++){
if(set.contains(matrix[i][l])) count++;
}
set.add(matrix[k][j]);
}
}
set.add(matrix[i][j]);
}
}
}
System.out.println(count);
}
}
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int rows = scanner.nextInt();
int cols = scanner.nextInt();
char[][] matrix = new char[rows][cols];
int count = 0;
Set<Character> set = new HashSet<>();
set.add(‘y’);
set.add(‘o’);
set.add(‘u’);
for (int i = 0; i < rows; i++) {
String row = scanner.next();
for (int j = 0; j < cols; j++) {
char c = row.charAt(j);
matrix[i][j] = c;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if(set.contains(matrix[i][j])){
set.remove(matrix[i][j]);
for (int k = 0; k < matrix.length; k++) {
if(set.contains(matrix[k][j])) {
set.remove(matrix[k][j]);
for(int l = 0; l < matrix[0].length ; l++){
if(set.contains(matrix[i][l])) count++;
}
set.add(matrix[k][j]);
}
}
set.add(matrix[i][j]);
}
}
}
System.out.println(count);
}
}
01:02
以上就是关于问题游游拿到了一个字符矩阵。她想知道有多少个2*2的子矩阵同时包含了’y’、’o’和’u’这三种字符?的答案
欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。
区块链NFT链游项目方科学家脚本开发培训