ccf 消除游戏

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();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值