import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt(), col = sc.nextInt();
int matrix[][] = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = sc.nextInt();
}
}
boolean flag[][] = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int row_sum = 0;
int col_sum = 0;
//if(i-1>0){//判断是否能向上走
int row_temp = i - 1;
while (row_temp > 0) {
if (matrix[row_temp][j] != matrix[i][j]) {
break;
} else {
col_sum++;
}
row_temp--;
}
// }
//if(i+1<row){//判断是否能向下走
int temp_row = i + 1;
while (temp_row < row) {
if (matrix[temp_row][j] != matrix[i][j]) {
break;
} else {
col_sum++;
}
temp_row++;
}
// }
// if(j-1>0){//判断if向左走
int temp_col = j - 1;
while (temp_col > 0) {
if (matrix[i][temp_col] != matrix[i][j]) {
break;
} else {
row_sum++;
}
temp_col--;
}
// }
// if(j+1<col){//盘那段if向右走
temp_col = j + 1;
while (temp_col < col) {
if (matrix[i][temp_col] != matrix[i][j]) {
break;
} else {
row_sum++;
}
temp_col++;
}
//}
int temp = matrix[i][j];
if (row_sum >= 2) {//把行方向的消除,分为左边和右边
flag[i][j] = true;
temp_col = j - 1;//左边
while (temp_col > 0) {
if (matrix[i][temp_col] != temp) {
break;
} else {
flag[i][temp_col] = true;
}
temp_col--;
}
temp_col = j + 1;
while (temp_col < col) {
if (matrix[i][temp_col] != temp) {
break;
} else {
flag[i][temp_col] = true;
}
temp_col++;
}
}
if (col_sum >= 2) {//把列方向消除
flag[i][j] = true;
temp_row = i + 1;
while (temp_row < row) {
if (matrix[temp_row][j] != temp) {
break;
} else {
flag[temp_row][j] = true;
}
temp_row++;
}
temp_row = i - 1;
while (temp_row > 0) {
if (matrix[temp_row][j] != temp) {
break;
} else {
flag[temp_row][j] = true;
}
temp_row--;
}
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (flag[i][j]) {
System.out.print(0 + " ");
} else {
System.out.print(matrix[i][j] + " ");
}
}
System.out.println();
}
}
}
ccf 消除游戏
最新推荐文章于 2021-11-17 16:35:15 发布