思路:
先遍历二维数组的每一行,查看是否可以消除的元素,可以的话则自身元素加10,
再遍历每一列,判断 每一列的下一个元素%10,与上一个元素%10是否相等,超过三个或三个以上则可抵消的元素自身再加10.
最后遍历,大于10的元素,则替换成0。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int[][] a = new int[n][m];
int temp = 0, count = 1;
//输入,且判断每一行是否有可消除的元素,有则自身加10.
for (int i = 0; i < n; i++) {
count = 1;
a[i][0] = input.nextInt();
temp = a[i][0];
for (int j = 1; j < m; j++) {
a[i][j] = input.nextInt();
if (a[i][j] == temp) {
count++;
if (count == 3) {
a[i][j] = a[i][j - 1] = a[i][j - 2] = temp + 10;
} else if (count > 3) {
a[i][j] = temp + 10;
}
} else {
count = 1;
temp = a[i][j];
}
}
}
//遍历每列,判断是否有可抵消的元素。
for (int i = 0; i < m; i++) {
count = 1;
temp = a[0][i];
for (int j = 1; j < n; j++) {
if (a[j][i] % 10 == temp % 10) {
count++;
if (count == 3) {
a[j][i] = a[j - 1][i] = a[j - 2][i] = temp + 10;
} else if (count > 3) {
a[j][i] = temp + 10;
}
} else {
count = 1;
temp = a[j][i];
}
}
}
// 遍历输出
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] > 10) {
if (j == m - 1) {
System.out.print("0");
break;
}
System.out.print("0 ");
} else {
if (j == m - 1) {
System.out.print(a[i][j]);
break;
}
System.out.printf("%d ", a[i][j]);
}
}
System.out.println();
}
}
}