Advertisement
roronoa

nombre ennemis matrice touchés explosion

Jun 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. /**
  6.  * Auto-generated code below aims at helping you parse
  7.  * the standard input according to the problem statement.
  8.  **/
  9. class Solution {
  10.  
  11.     public static void main(String args[]) {
  12.         Scanner in = new Scanner(System.in);
  13.         int n = in.nextInt();
  14.         int m = in.nextInt();
  15.         List<Integer> enemies = new ArrayList<>();
  16.         char[][] mat = new char[m][m];
  17.         if (in.hasNextLine()) {
  18.             in.nextLine();
  19.         }
  20.         for (int i = 0; i < m; i++) {
  21.             String battleground = in.nextLine();
  22.             for(int j = 0; j < m; j++)
  23.                 mat[i][j] = battleground.charAt(j);
  24.         }
  25.         if(m <= n)
  26.             System.out.println(numberOfX(mat,m));
  27.             else
  28.             {
  29.         for(int i = 0; i <= m - n; i++)
  30.         {
  31.             for(int j = 0; j <= m - n; j++)
  32.                 enemies.add(numberEnemiesInSquare(mat,i,j,n));
  33.         }
  34.         int max = 0;
  35.         for(int x : enemies)
  36.             if(x > max)
  37.                 max = x;
  38.         System.out.println(max);
  39.             }
  40.  
  41.     }
  42.     public static int numberEnemiesInSquare(char[][] mat,int x, int y, int size)
  43.     {
  44.         int enemies = 0;
  45.         for(int i = x; i < x + size; i++)
  46.             for(int j = y; j < y + size; j++)
  47.                 if(mat[i][j] == 'x')
  48.                     enemies++;
  49.         return enemies;
  50.     }
  51.     public static int numberOfX(char[][] mat,int size)
  52.     {
  53.         /* plutôt
  54.     return numberEnemiesInSquare(mat, 0, 0, size);
  55.         */
  56.         int total = 0;
  57.         for(int i = 0; i <  size; i++)
  58.             for(int j = 0; j < size; j++)
  59.                 if(mat[i][j] == 'x')
  60.                     total++;
  61.                     return total;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement