Ver Mensaje Individual
  #2 (permalink)  
Antiguo 01/06/2012, 10:00
Avatar de Lalounam
Lalounam
 
Fecha de Ingreso: mayo-2012
Ubicación: México D.F.
Mensajes: 59
Antigüedad: 12 años
Puntos: 19
Respuesta: Problema: no encuentro la solución

Mira, si te entedí bien, aquí hay un ejemplo:

Código Java:
Ver original
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.     int[][] matriz = new int[25][20];
  5.  
  6.     // proceso para llenar la matriz
  7.     public void llenado() {
  8.         Random rnd = new Random();
  9.  
  10.         for (int i = 0; i < 25; i++) {
  11.             for (int j = 0; j < 20; j++) {
  12.                 do {
  13.                     matriz[i][j] = (int) (rnd.nextDouble() * 100.0);
  14.                 } while (matriz[i][j] <= 0);
  15.             }
  16.         }
  17.     }
  18.  
  19.     // proceso para encontrar el menor y el menor abs
  20.     public void menor() {
  21.         int abs = 0, ant = 0;
  22.         int absR = 0, absC = 0;
  23.         int antR = 0, antC = 0;
  24.  
  25.         for (int i = 0; i < 25; i++) {
  26.             for (int j = 0; j < 20; j++) {
  27.                 if (abs > matriz[i][j] || abs == 0) {
  28.                     abs = matriz[i][j];
  29.                     absR = i;
  30.                     absC = j;
  31.                 } else if ((ant > matriz[i][j] && abs < matriz[i][j])
  32.                         || ant == 0) {
  33.                     ant = matriz[i][j];
  34.                     antR = i;
  35.                     antC = j;
  36.                 }
  37.             }
  38.  
  39.         }
  40.  
  41.         System.out.println("\nEl menor absoluto es: "
  42.                 + (abs < 10 ? ("0" + abs) : (abs)) + " ubicado en Renglon="
  43.                 + absR + " Columna=" + absC);
  44.  
  45.         System.out.println("\nEl menor anterior es: "
  46.                 + (ant < 10 ? ("0" + ant) : (ant)) + " ubicado en Renglon="
  47.                 + antR + " Columna=" + antC);
  48.  
  49.         System.out.println("\nDistancia = "
  50.                 + ((Math.max(antR, absR) - Math.min(antR, absR)) + ((Math.max(
  51.                         antC, absC)) - (Math.min(antC, absC)))));
  52.     }
  53.  
  54.     // proceso para mostrar la matriz
  55.     public void mostrar() {
  56.         for (int i = 0; i < 25; i++) {
  57.             for (int j = 0; j < 20; j++) {
  58.                 System.out.print((matriz[i][j] < 10 ? ("0" + matriz[i][j])
  59.                         : matriz[i][j])
  60.                         + " ");
  61.             }
  62.             System.out.println();
  63.         }
  64.  
  65.     }
  66.  
  67.     // codigo principal
  68.     public static void main(String[] args) {
  69.         Main fin;
  70.         fin = new Main();
  71.         fin.llenado();
  72.         fin.mostrar();
  73.         fin.menor();
  74.     }
  75. }

Pedí que no fueran iguales el menor absoluto y el menor anterior, por supuesto aumenté a 100 el rango de los aleatorios para hacerlo interesante y saqué la distancia en celdas como en tú ejemplo (incluyendo la celda de los números), aunque sería mejor la distancia euclidiana.

Espero te sirva, saludos!