(URI) Jogo do Quadrado - Solution

Link to the problem: https://www.urionlinejudge.com.br/judge/pt/problems/view/2067


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

class Main {
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        int numLinhas = sc.nextInt();
        int numColunas = sc.nextInt();
        int[][] matriz = new int[numLinhas][numColunas];
        for (int i = 0; i < numLinhas; i++) {
            for (int j = 0; j < numColunas; j++) {
                int n = sc.nextInt();
                if (n == 0) {
                    n = 1;
                }
                else {
                    n = 0;
                }
                matriz[i][j] = n;
            }
        }
       
        for (int i = 0; i < numLinhas; i++) {
            int somaLinha = 0;
            for (int j = 0; j < numColunas; j++) {
                int soma = 0;
                if (i-1 >= 0) {
                    soma = matriz[i-1][j];
                }
               
                somaLinha += matriz[i][j];
                matriz[i][j] = soma+somaLinha;
            }
        }
            
        int numConsultas = sc.nextInt();
        for (int i = 0; i < numConsultas; i++) {
            int tamanho = sc.nextInt();
            int startLinha = tamanho-1;
            int startColuna = tamanho-1;
            boolean pontuacaoMaiorQueZero = false;
            for (int linha = startLinha; linha < numLinhas && !pontuacaoMaiorQueZero; linha++) {
                for (int coluna = startColuna; coluna < numColunas && !pontuacaoMaiorQueZero; coluna++) {
                    int valorPosAtual = matriz[linha][coluna];
                    if (linha-tamanho >= 0) {
                        valorPosAtual -= matriz[linha-tamanho][coluna];
                    }
                    if (coluna-tamanho >= 0) {
                        valorPosAtual -= matriz[linha][coluna-tamanho];
                    }
                    if (linha-tamanho >= 0 && coluna-tamanho >= 0) {
                        valorPosAtual += matriz[linha-tamanho][coluna-tamanho];
                    }
                    if (valorPosAtual == 0) {
                        pontuacaoMaiorQueZero = true;
                        break;
                    }
                }
            }
            if (pontuacaoMaiorQueZero) {
                System.out.println("yes");
            }           
            else {
                System.out.println("no");
            }
        }
       
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução