Guest User

CH7 Tile Game

a guest
Jun 20th, 2011
981
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. /*
  7.  * Tuenti Contest
  8.  * Challenge 7 - Tile Game
  9.  * Author: Pedro Antonio Pardal Jimena
  10.  * Email: pardal@alu.uma.es
  11.  */
  12.  
  13. public class TileGame
  14. {
  15.     private static int minimo( int a, int b, int c )
  16.     {
  17.         if ( a <= b && a <= c )
  18.             return a;
  19.         else if ( b <= a && b <= c )
  20.             return b;
  21.         else
  22.             return c;
  23.     }
  24.    
  25.     private static int distanciaLevenshtein( char[] s1, char[] s2, int add, int del, int exc )
  26.     {
  27.         int[][] tabla = new int[s1.length + 1][s2.length + 1];
  28.        
  29.         for ( int i = 0; i <= s1.length ;i++ )
  30.             tabla[i][0] = i * add;
  31.        
  32.         for ( int j = 0; j <= s2.length; j++ )
  33.             tabla[0][j] = j * del;
  34.        
  35.         for ( int i = 1; i <= s1.length; i++ )
  36.         {
  37.             for ( int j = 1; j <= s2.length; j++ )
  38.             {
  39.                 tabla[i][j] = minimo( tabla[i-1][j] + add,
  40.                                       tabla[i][j-1] + del,
  41.                                       tabla[i-1][j-1] + ((s1[j-1] == s2[i-1]) ? 0 : exc ));
  42.             }
  43.         }
  44.        
  45.         return tabla[s1.length][s2.length];
  46.     }
  47.    
  48.     private static int parseInput( String linea )
  49.     {
  50.         StringTokenizer st = new StringTokenizer( linea, " " );
  51.        
  52.         String inicio = st.nextToken();
  53.         String objetivo = st.nextToken();
  54.  
  55.         String costes = st.nextToken();
  56.         StringTokenizer st2 = new StringTokenizer( costes, "," );
  57.        
  58.         int costeAdd = Integer.parseInt(st2.nextToken());
  59.         int costeDel = Integer.parseInt(st2.nextToken());
  60.         int costeExc = Integer.parseInt(st2.nextToken());
  61.        
  62.         return distanciaLevenshtein( inicio.toCharArray(), objetivo.toCharArray(),
  63.                                      costeAdd, costeDel, costeExc );
  64.     }
  65.    
  66.     public static void main( String[] args ) throws IOException
  67.     {
  68.         BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
  69.        
  70.         while ( reader.ready() )
  71.         {
  72.             String linea = reader.readLine();
  73.             int result = parseInput( linea );
  74.            
  75.             System.out.println( result );
  76.         }
  77.     }
  78. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×