UVA10908




解法:模擬 以給的點左上角開始爆搜,每搜完沒有點是不相同的大小就+2 or DP




import java.util.Scanner;

public class UVA10908 {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);

  int count = Integer.parseInt(sc.nextLine());

  for (int k = 0; k < count; k++) {

   int row = sc.nextInt();
   int col = sc.nextInt();
   int num = sc.nextInt();

   sc.nextLine();

   System.out.println(row + " " + col + " " + num);

   if(row==0 || col==0 || num==0){

    continue;

   }

   char matrix[][] = new char[row][col];

   for (int i = 0; i < row; i++) {

    String s2 = sc.nextLine();

    for (int j = 0; j < col; j++)
     matrix[i][j] = s2.charAt(j);

   }

   for (int n = 0; n < num; n++) {

    int size = 1, constant = 1;

    boolean check = true;

    int specify_x = sc.nextInt();

    int specify_y = sc.nextInt();

    if(specify_x>row-1 || specify_y>col-1)
    {
     System.out.println(0);
     continue;
    }

    char chr = matrix[specify_x][specify_y];

    while (check) {

     for (int i = specify_x - constant; i <=specify_x + constant; i++){

      for (int j = specify_y - constant; j <= specify_y+ constant; j++)

      {

       if (i < 0 || j < 0 || i >= row || j >= col) {

        check = false;

        break;

       }

       if (matrix[i][j] != chr) {
        check = false;
        break;

       }

      }
     }
      if (check) {
       size += 2;
       constant++;
      }

     }

     System.out.println(size);


    }
  }

   sc.close();

  }

 }