用二位数组接收棋盘,然后把定义两个相同的映射数组,分别进行行消除和咧消除,消除后这两个映射数组中为0的元素,就是棋盘消除棋子的地方。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int [][]qipan=new int[n][m];
int [][]qipan1=new int[n][m];
int [][]qipan2=new int[n][m];
int flag1=1;
int flag2=1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
qipan[i][j]=sc.nextInt();
qipan1[i][j]= qipan[i][j];
qipan2[i][j]=qipan[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m-1; j++) {
if(qipan1[i][j]==qipan1[i][j+1]){
flag1++;
if(j==m-2){ //count1[qipan1[i][j]]
if(flag1>=3){
for(int k=m-1;k>m-1-flag1;k--){
qipan1[i][k]=0;
}
}
flag1=1;
}
}
else {
if( flag1>=3){
for(int k=j;k>j-flag1;k--){
qipan1[i][k]=0;
}
}
flag1=1;
}
}
}
for (int j = 0; j
=3){
for(int k=n-1;k>n-1-flag2;k--){
qipan2[k][j]=0;
}
}
flag2=1;
}
}
else{
if( flag2>=3){
for(int k=i;k>=i+1-flag2;k--){
qipan2[k][j]=0;
}
}
flag2=1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if((qipan1[i][j]==0)||(qipan2[i][j]==0)){
qipan[i][j]=0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(qipan[i][j]+" ");
}
System.out.println("");
}
}
}