2021-04-11

/**

  • 题目:

  • 存在一个m*n的二维数组,其成员取值范围为0或1。其中值为1的成员具备扩散性,

  • 每经过1S,将上下左右值为0的成员同化为1。

  • 二维数组的成员初始值都为0,将第[i,j]和[k,l]两个位置上元素修改成1后,求矩阵的所有元素变为1需要多长时间。

  • 输入描述:

  • 前两个数是矩阵m*n,中间两个数是第一个点的坐标,最后两个数是第二个点的坐标

  • 其中这两个点初始为1,其他点初始为0

  • 输出描述:

  • 输出矩阵的所有元素变为1所需要秒数。
    */
    public class MatrixDiffusion {
    public static void main(String[] args) {
    int[] input = new int[]{4, 4, 0, 0, 3, 3};

     int result = getTime(input);
    
     System.out.println(result);
    

    }

    /**

    • 思路:模拟扩散,暴力破解
      */
      private static int getTime(int[] input) {
      int xMax = input[0];
      int yMax = input[1];

      int xA = input[2];
      int yA = input[3];

      int xB = input[4];
      int yB = input[5];

      int[][] matrix = new int[xMax][yMax];

      // 初始化
      for (int x =0; x < xMax; x++) {
      for (int y = 0; y < yMax; y++) {
      matrix[x][y] = 0;

           // 把初始的两个点改成1
           if (x == xA && y == yA || x == xB && y == yB) {
               matrix[x][y] = 1;
           }
       }
      

      }

      // 是否继续扩散
      boolean needContinue = true;
      // 扩散次数
      int count = 0;
      // 模拟扩散
      while (needContinue) {
      // 每次扩散前假设这次扩散后就结束了
      needContinue = false;
      // 本次操作是否真实的扩散了
      boolean curDid = false;

       // 先扩散成2(因为本次信扩散的点不能扩散)
       for (int x =0; x < xMax; x++) {
           for (int y = 0; y < yMax; y++) {
               if (matrix[x][y] == 1) {
                   int topX = x - 1;
                   int topY = y;
                   if (topX >= 0 && topX < xMax && matrix[topX][topY] == 0) {
                       matrix[topX][topY] = 2;
                       curDid = true;
                   }
      
                   int leftX = x;
                   int leftY = y - 1;
                   if (leftY >= 0 && leftY < yMax && matrix[leftX][leftY] == 0) {
                       matrix[leftX][leftY] = 2;
                       curDid = true;
                   }
      
                   int belowX = x + 1;
                   int belowY = y;
                   if (belowX >= 0 && belowX < xMax && matrix[belowX][belowY] == 0) {
                       matrix[belowX][belowY] = 2;
                       curDid = true;
                   }
      
                   int rightX = x;
                   int rightY = y + 1;
                   if (rightY >= 0 && rightY < yMax && matrix[rightX][rightY] == 0) {
                       matrix[rightX][rightY] = 2;
                       curDid = true;
                   }
               }
           }
       }
      
       // 本次扩散了就将扩散次数加一
       if (curDid) {
           count++;
       }
      
       // 善后:把2修改成1,让下一次正常扩散,并且判断需不需要进一步扩散
       for (int x =0; x < xMax; x++) {
           for (int y = 0; y < yMax; y++) {
               if (matrix[x][y] == 2) {
                   matrix[x][y] = 1;
               }
      
               // 还有0的就继续扩散
               if (matrix[x][y] == 0) {
                   needContinue = true;
               }
      
               // 输出扩散的中间结果
               System.out.print(matrix[x][y] + " ");
           }
           // 输出扩散的中间结果
           System.out.println();
       }
      
       // 输出扩散的中间结果
       System.out.println("\n");
      

      }

      return count;
      }
      }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值