Thursday, January 05, 2012

Solucion a 12398 NumPuzz I [http://uva.onlinejudge.org] con Java

Solucion a 12398 NumPuzz I [http://uva.onlinejudge.org] con Java
El enunciado esta en el siguiente link enunciado

import java.util.Scanner;

/**
 * Problem B NumPuzz I [http://uva.onlinejudge.org]
 * 
 * @author BreakDark
 * @version 1.0 beta
 */
// ACEPTADO!!!
public class Main {
 public static void main(String[] args) {
  Scanner Lee;
  String linea;
  byte[][] mat;
  int i, j, k, caso;

  // AQUI INICIA EL PROGRAMA
  Lee = new Scanner(System.in);
  caso = 1;
  while (Lee.hasNextLine()) {
   linea = Lee.nextLine();
   mat = new byte[3][3];
   for (k = linea.length() - 1; k >= 0; k--) {
    switch (linea.charAt(k)) {
    case 'a':
     i = 0;
     j = 0;
     break;
    case 'b':
     i = 0;
     j = 1;
     break;
    case 'c':
     i = 0;
     j = 2;
     break;
    case 'd':
     i = 1;
     j = 0;
     break;
    case 'e':
     i = 1;
     j = 1;
     break;
    case 'f':
     i = 1;
     j = 2;
     break;
    case 'g':
     i = 2;
     j = 0;
     break;
    case 'h':
     i = 2;
     j = 1;
     break;
    default:
     i = 2;
     j = 2;
     break;
    }
    // modificamos la casilla
    if (mat[i][j] < 9)
     mat[i][j]++;
    else
     mat[i][j] = 0;
    // modificamos arriba
    if (i > 0) {
     if (mat[i - 1][j] < 9)
      mat[i - 1][j]++;
     else
      mat[i - 1][j] = 0;
    }
    // modificamos abajo
    if (i < 2) {
     if (mat[i + 1][j] < 9)
      mat[i + 1][j]++;
     else
      mat[i + 1][j] = 0;
    }
    // modificamos izquierda
    if (j > 0) {
     if (mat[i][j - 1] < 9)
      mat[i][j - 1]++;
     else
      mat[i][j - 1] = 0;
    }
    // modificamos derecha
    if (j < 2) {
     if (mat[i][j + 1] < 9)
      mat[i][j + 1]++;
     else
      mat[i][j + 1] = 0;
    }
   }
   System.out.println("Case #" + (caso++) + ":");
   for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
     System.out.print(mat[i][j]);
     if (j < 2)
      System.out.print(" ");
    }
    System.out.println();
   }
  }
 }
}

No comments: