Posts

Showing posts from March, 2014

(UVA) 729 - The Hamming Distance Problem - Solução 2

Solução usando recursão. (fica mais rápido) import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     static int leitor(BufferedReader br) throws NumberFormatException, IOException {         int n;         int resp = 0;                  while (true) {             n = br.read();             if (n >= '0' && n <= '9') break;         }         while (true) {             resp = resp*10 + n-'0';             n = br.read();             if (n < '0' || n > '9') break;         }                  return resp;     }         void funcao(int[] digito, int p, int comprimento, int distanciaHamming, BufferedWriter bw, int qteZero, int qteUm) throws NumberFormatException, IOException {         if (p == comprimento) {       

(UVA) 729 - The Hamming Distance Problem - Solução 1

Solução sem recursão. import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     static int leitor(BufferedReader br) throws NumberFormatException, IOException {         int n;         int resp = 0;                  while (true) {             n = br.read();             if (n >= '0' && n <= '9') break;         }         while (true) {             resp = resp*10 + n-'0';             n = br.read();             if (n < '0' || n > '9') break;         }                  return resp;     }         void funcao(int comprimento, int distanciaHamming, int valorMaximo, BufferedWriter bw) throws NumberFormatException, IOException {         for (int i = 0; i < valorMaximo; i++) {             String binario = Integer.t

(UVA) 639 - Don't Get Rooked - Solução 2

Segunda solução para o problema. Não utiliza o for no interior da função recursiva. import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     int funcao (int[][] matriz, int tamanho, int linha, int coluna, int qteTorres) {         if (coluna == tamanho) {             return qteTorres;         }                 int resposta = qteTorres;         int i = linha;         int valorCampo = matriz[i][coluna];                 int lin = 0;         int col = 0;                 if (i == tamanho-1) { // tá no limite das linhas             lin = 0;             col = coluna+1;         }         else {             lin = i+1;             col = coluna;         }         // não coloca torre         resposta = Math.max(resposta, funcao(matriz, tamanho, lin, col, qteTorres));  

(UVA) 639 - Don't Get Rooked - Solução

import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     int funcao (int[][] matriz, int tamanho, int linha, int coluna, int qteTorres) {         if (coluna == tamanho) {             return qteTorres;         }                 int resposta = qteTorres;         for (int i = linha; i < tamanho; i++) {             int valorCampo = matriz[i][coluna];                         if (matriz[i][coluna] > 0 && i < tamanho-1) { // dominado por uma torre e não é X, tenta a próxima linha                 continue;             }             // se não tá dominado por uma torre e é X             if (valorCampo == 0) {                 qteTorres++;                             for (int j = coluna; j < tamanho; j++) {                     if (matriz[i][j] == -1) { // é

(UVA) 539 - The Settlers of Catan - Solução

import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     static int leitor(BufferedReader br) throws NumberFormatException, IOException {         int n;         int resp = 0;                  while (true) {             n = br.read();             if (n >= '0' && n <= '9') break;         }         while (true) {             resp = resp*10 + n-'0';             n = br.read();             if (n < '0' || n > '9') break;         }                  return resp;          }         int funcao (int[][] matriz, int tamanho, int vertice, int valor, int[][] visitados) {         int resposta = valor;                 for (int i = 0; i < tamanho; i++) {             if (matriz[vertice][i] == 1 && visitados[vert

(UVA) 11195 - Another n-Queen Problem - Solução

Esta solução resolve o problema, mas excede o tempo limite estabelecido. Não foi aceito por exceder o tempo limite. import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     int funcao (int[][] matriz, int tamanho, int coluna, int[] vetorConfereLinha, int[] vetorConfereColuna, int[] vetorDiagonal1, int[] vetorDiagonal2) {         if (coluna == tamanho) { // viu tudo - conseguiu colocar rainha em todas as colunas             return 1;         }                 int resposta = 0;         for (int i = 0; i < tamanho; i++) {             int indice1 = i+coluna;             int indice2 = i+tamanho-coluna-1;             if (vetorConfereLinha[i] > 0 || vetorConfereColuna[coluna] > 0 || vetorDiagonal1[indice1] > 0 || vetorDiagonal2[indice2] > 0 || matriz[i][coluna] &g

(SPOJ) 1389 - Pedido de Desculpas - Solução

import java.io.*; import java.util.*; class Main {     class Desculpa {         int qteCaracteres;         int qteDesculpa;     }         public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }         static int leitor(BufferedReader br) throws NumberFormatException, IOException {         int n;         int resp = 0;                  while (true) {             n = br.read();             if (n >= '0' && n <= '9') break;         }         while (true) {             resp = resp*10 + n-'0';             n = br.read();             if (n < '0' || n > '9') break;         }                  return resp;          }     int funcao (Desculpa[] desculpa, int[][] tabelaVerificadora, int tamanhoAtual, int qteFrases, int comprimentoCartao, int posicao) {         if (tamanhoAtual > comprimentoCartao) { //

(UVA) 167 - The Sultan's Successors - Solução

import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();                 System.exit(0);     }     static int leitor(BufferedReader br) throws NumberFormatException, IOException {         int n;         int resp = 0;                  while (true) {             n = br.read();             if (n >= '0' && n <= '9') break;         }         while (true) {             resp = resp*10 + n-'0';             n = br.read();             if (n < '0' || n > '9') break;         }                  return resp;          }         int funcao (int[][] matriz, int coluna, int[] vetorConfereLinha, int[] vetorConfereColuna, int[] vetorDiagonal1, int[] vetorDiagonal2, int valor) {         if (coluna == 8) { // viu tudo - conseguiu colocar rainha em todas as colunas