(UVA) Largest Square - Solution

Breadth-First Search was used to solve this problem.


import java.io.*;
import java.util.*;

class Main {
    public static char[][] matrix;
    public static boolean[][] visited;
    public static int vi[] = {-1,-1,-1,0,0,1,1,1};
    public static int vj[] = {-1,0,1,-1,1,-1,0,1};
   
    public static int bfs(int row, int col, int numRows, int numCols) {
        Queue<Coord> queue = new ArrayDeque<Coord>();
        queue.add(new Coord(row, col, 1));
       
        while (queue.size() > 0) {
            Coord curr = queue.poll();
            int currRow = curr.row;
            int currCol = curr.col;
            int currSizeSide = curr.sizeSide;
           
            if (visited[currRow][currCol]) {
                continue;
            }
            visited[currRow][currCol] = true;
           
            for (int i = 0; i < 8; i++) {
                int nextRow = currRow+vi[i];
                int nextCol = currCol+vj[i];
                if (nextRow < 0 || nextCol < 0 || nextRow >= numRows || nextCol >= numCols || matrix[nextRow][nextCol] != matrix[row][col]) {
                    return currSizeSide;
                }
                queue.add(new Coord(nextRow, nextCol, currSizeSide+2));
            }
        }
       
        return Math.max(numRows, numCols);
    }
   
    public static void process() throws NumberFormatException, IOException {  
        Scanner sc = new Scanner(System.in);
       
        int numTests = sc.nextInt();
        for (int i = 0; i < numTests; i++) {
            int numRows = sc.nextInt();
            int numCols = sc.nextInt();
            int numQueries = sc.nextInt();
           
            matrix = new char[numRows][numCols];
            for (int j = 0; j < numRows; j++) {
                String line = sc.next();
                for (int k = 0; k < numCols; k++) {
                    matrix[j][k] = line.charAt(k);
                }
            }
           
            System.out.println(numRows + " " + numCols + " " + numQueries);
            for (int j = 0; j < numQueries; j++) {
                int queryRow = sc.nextInt();
                int queryCol = sc.nextInt();
                if (queryRow >= numRows || queryCol >= numCols) {
                    System.out.println("0");
                    continue;
                }
                visited = new boolean[numRows][numCols];
                System.out.println(bfs(queryRow, queryCol, numRows, numCols));
            }
        }
                      
        return;
    }
  
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

class Coord {
    int row;
    int col;
    int sizeSide;
   
    public Coord(int row, int col, int sizeSide) {
        this.row = row;
        this.col = col;
        this.sizeSide = sizeSide;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução