简体   繁体   中英

Find the remaining elements in a matrix

Problem statement

You're given a multi-layered rack in the form of a matrix with m rows and n columns. Each cell is occupied either by a red pill (marked by r), a blue pill (marked by b) or a germ (marked by x). A red pill can kill adjacent germs (if any) in horizontal, vertical and diagonal directions whereas a blue pill can kill adjacent germs (if any) in horizontal and vertical direction only. Initially the pills are inactive. Once active, they can act on adjacent germs. You need to find the count of the remaining germs in the rack once the pills are activated.

m = rows n = columns , test = number of iterations

r = red pills ,
x = germs

Conditions are :

r = Red pills can kill adjacent germs in the direction of horizontal, vertical and diagonal.

I need to find the remaining germs.

My (incomplete)solution is - I created an 2d array(arr1) which accepts string and then converted that array elements into integers ie 0 & 1 ( r = 0 and x = 1) and stored in another 2d array(arr2). Now I'm able to calculate the total number of Germs only ,but I want to find remaining germs.

This is my code

    import java.util.Scanner; 
    import java.*;

   public class Pills  {
   public static void main(String args[])
  {
  // initialize here. 
   int row, col, i, j, k, test;  
  String  arr[][] = new String[10][10];
  int arr2[][] = new int[10][10];
 Scanner scan = new Scanner(System.in);
 System.out.println("Enter number of tests :");

 test = scan.nextInt();
 for(k=0;k<test;k++)
 {
  //enter row and column for array.
   System.out.println(" ");
   row = scan.nextInt(); 
   System.out.println(" "); 
   col = scan.nextInt(); 

   // enter array elements.
   System.out.println(" "); 
   for(i=0; i<row; i++)
    { 
    for(j=0; j<col; j++)
     {
      arr[i][j] = scan.next(); 
      if(arr[i][j].equals("r"))
      {
          arr2[i][j]=0;
      }
      else {
         arr2[i][j]=1;
              }
            }
         }    
        // the 2D array is here.
        System.out.print("\n"); 
        for(i=0; i<row; i++)        
       { 
        for(j=0; j<col; j++)   
         {
           System.out.print(arr[i][j]+ " ");
           }
           System.out.println();
           }
           System.out.print("\n"); 

            int sum = 0;
            for ( i=0; i < arr2.length; i++)
           {
            for (j=0; j < arr2[i].length; j++)
            {
            sum = sum + arr2[i][j];
             }
              }
              System.out.println("Total Germs : "+sum);    
                  }
                } 
              }

I need help finding remaining germs.

public class Main
{
  public static void main (String[]args)
  {

    String[][]arr =
    {
      {
      "g", "g", "g", "g"},
      {
      "g", "r", "g", "g"},
      {
      "g", "g", "g", "g"},
      {
      "g", "g", "g", "g"}
    };

    int germCount = getGerms (arr);
    System.out.print (germCount);

  }

  public static int getGerms (String[][]arr)
  {
    int germCount = 0;
    int row = arr.length;
    int col = arr[0].length;

    for (int i = 0; i < row; i++)
      {
    for (int j = 0; j < col; j++)
      {
        if (arr[i][j].equals ("r") || arr[i][j].equals ("b"))
          {
        if (i + 1 < row && arr[i + 1][j].equals ("g"))
          {
            arr[i + 1][j] = "k";

          }
        if (i - 1 >= 0 && arr[i - 1][j].equals ("g"))
          {
            arr[i - 1][j] = "k";

          }
        if (j + 1 < col && arr[i][j + 1].equals ("g"))
          {
            arr[i][j + 1] = "k";

          }
        if (j - 1 >= 0 && arr[i][j - 1].equals ("g"))
          {
            arr[i][j - 1] = "k";

          }
          }


        if (arr[i][j].equals ("r"))
          {
        if (i + 1 < row && j + 1 < col
            && arr[i + 1][j + 1].equals ("g"))
          {
            arr[i + 1][j + 1] = "k";

          }
        if (i + 1 < row && j - 1 >= 0
            && arr[i + 1][j - 1].equals ("g"))
          {
            arr[i + 1][j - 1] = "k";

          }

        if (i - 1 >= 0 && j - 1 >= 0
            && arr[i - 1][j - 1].equals ("g"))
          {
            arr[i - 1][j - 1] = "k";

          }


        if (i - 1 >= 0 && j + 1 < col
            && arr[i - 1][j + 1].equals ("g"))
          {
            arr[i - 1][j + 1] = "k";

          }
          }

      }
      }

    for (int i = 0; i < row; i++)
      {
    for (int j = 0; j < col; j++)
      {
        System.out.print (arr[i][j]);
        if (arr[i][j].equals ("g"))
          germCount++;
      }
    System.out.println ();
      }

    return germCount;
  }
}

o/p-

kkkg
krkg
kkkg
gggg
7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM