Advertisement
DimasDark

L3Q2

Aug 14th, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.44 KB | None | 0 0
  1. public class L3Q2 {
  2.  
  3.     static final int distMax = 218309120;
  4.  
  5.     public static int charToInt(char chrLetra) //Método que recebe uma letra (char) e retorna o valor inteiro atribuído a mesma.
  6.     {
  7.         int intLetra = 0;
  8.  
  9.         switch (chrLetra)
  10.         {
  11.         case 'a':
  12.         {
  13.             intLetra = 1;
  14.             break;
  15.         }
  16.         case 'b':
  17.         {
  18.             intLetra = 2;
  19.             break;
  20.         }
  21.         case 'c':
  22.         {
  23.             intLetra = 3;
  24.             break;
  25.         }
  26.         case 'd':
  27.         {
  28.             intLetra = 4;
  29.             break;
  30.         }
  31.         case 'e':
  32.         {
  33.             intLetra = 5;
  34.             break;
  35.         }
  36.         case 'f':
  37.         {
  38.             intLetra = 6;
  39.             break;
  40.         }
  41.         case 'g':
  42.         {
  43.             intLetra = 7;
  44.             break;
  45.         }
  46.         case 'h':
  47.         {
  48.             intLetra = 8;
  49.             break;
  50.         }
  51.         case 'i':
  52.         {
  53.             intLetra = 9;
  54.             break;
  55.         }
  56.         case 'j':
  57.         {
  58.             intLetra = 10;
  59.             break;
  60.         }
  61.         case 'k':
  62.         {
  63.             intLetra = 11;
  64.             break;
  65.         }
  66.         case 'l':
  67.         {
  68.             intLetra = 12;
  69.             break;
  70.         }
  71.         case 'm':
  72.         {
  73.             intLetra = 13;
  74.             break;
  75.         }
  76.         case 'n':
  77.         {
  78.             intLetra = 14;
  79.             break;
  80.         }
  81.         case 'o':
  82.         {
  83.             intLetra = 15;
  84.             break;
  85.         }
  86.         case 'p':
  87.         {
  88.             intLetra = 16;
  89.             break;
  90.         }
  91.         case 'q':
  92.         {
  93.             intLetra = 17;
  94.             break;
  95.         }
  96.         case 'r':
  97.         {
  98.             intLetra = 18;
  99.             break;
  100.         }
  101.         case 's':
  102.         {
  103.             intLetra = 19;
  104.             break;
  105.         }
  106.         case 't':
  107.         {
  108.             intLetra = 20;
  109.             break;
  110.         }
  111.         case 'u':
  112.         {
  113.             intLetra = 21;
  114.             break;
  115.         }
  116.         case 'v':
  117.         {
  118.             intLetra = 22;
  119.             break;
  120.         }
  121.         case 'w':
  122.         {
  123.             intLetra = 23;
  124.             break;
  125.         }
  126.         case 'x':
  127.         {
  128.             intLetra = 24;
  129.             break;
  130.         }
  131.         case 'y':
  132.         {
  133.             intLetra = 25;
  134.             break;
  135.         }
  136.         case 'z':
  137.         {
  138.             intLetra = 26;
  139.             break;
  140.         }
  141.         case '.':
  142.         {
  143.             intLetra = 1;
  144.             break;
  145.         }
  146.         case 'F':
  147.         {
  148.             intLetra = 0;
  149.             break;
  150.         }      
  151.         default:
  152.         {
  153.             intLetra = distMax;
  154.             break;
  155.         }
  156.         }
  157.         return intLetra;
  158.     }
  159.  
  160.  
  161.     public static void main(String...Arguments) {
  162.         Arquivo arquivo = new Arquivo("L3Q2.in", "L3Q2.out");
  163.         int t, n, m, s, a, c; //T casos teste
  164. //      long tempoInicio = System.currentTimeMillis();  
  165.         t = arquivo.readInt();
  166.         while (t > 0)
  167.         {
  168.  
  169.             n = arquivo.readInt();
  170.             m = arquivo.readInt();
  171.             s = arquivo.readInt();
  172.             a = arquivo.readInt();
  173.             c = arquivo.readInt();
  174.             No inicio = null, fim = null;
  175.             No mapa[][] = new No[n][m];
  176.             Heap distancia = new Heap(n*m);
  177.             for (int i = 0; i < n; i++)
  178.             {
  179.  
  180.                 String ler = arquivo.readString();
  181.                 for (int j = 0; j < m; j++)
  182.                 {
  183.                     mapa[i][j] = new No(ler.charAt(j), charToInt(ler.charAt(j)), i, j);
  184.                     if (mapa[i][j].c == 'F') {
  185.                         mapa[i][j].pontoPartida();
  186.                         inicio = mapa[i][j];
  187.                     }
  188.                     else if(mapa[i][j].c == 'R') {
  189.                         fim = mapa[i][j];
  190.                     }
  191.                     distancia.inserir(mapa[i][j]);
  192.                 }
  193.             }
  194.                        
  195. //          for (int i = 0; i < n; i++) {
  196. //              for (int j = 0; j < m; j++) {
  197. //                  System.out.println(mapa[i][j].i + " " + mapa[i][j].j + " " + mapa[i][j].c + " Peso: " + mapa[i][j].peso);
  198. //              }
  199. //          }
  200.            
  201.  
  202.             //Faz heap     
  203.             //mapa[distancia.heap[0].i][distancia.heap[0].j].visitar();        
  204.             //          System.out.println(distancia.remover().c);
  205.             while (distancia.size > 0) {
  206.                 No u = distancia.remover();
  207.                 mapa[u.i][u.j].visitar();
  208.  
  209.                 //              mapa[u.i][u.j].visitar();  
  210. //              System.out.println("Removido da vez: " + u.i + " " + u.j + " " + u.c);
  211.  
  212.                 int[] I = { 0, 0, 1, -1 };
  213.                 int[] J = { 1, -1, 0, 0 };
  214.                 for(int x = 0; x < 4; x++)
  215.                 {
  216.                     int tempI = u.i + I[x];
  217.                     int tempJ = u.j + J[x];
  218.                        
  219.                     if (tempI >= 0 && tempI < n && tempJ >= 0 && tempJ < m && mapa[tempI][tempJ].c != '#') {
  220.                         if (mapa[tempI][tempJ].c == 'R' && !mapa[tempI][tempJ].visitado)
  221.                             mapa[tempI][tempJ].peso = 0;
  222.                         //System.out.println("entrou");
  223.                         int v = mapa[tempI][tempJ].peso;
  224.                         int aux = distancia.isInMinHeap(tempI, tempJ);
  225.                         //                      System.out.println("Pos na heap:" + aux);                      
  226.  
  227.                         if (aux != -1) {           
  228. //                          System.out.println(distancia.heap[aux].i + " " + distancia.heap[aux].j + " " + distancia.heap[aux].c + " " + distancia.heap[aux].tempoChegada);
  229.                             if (u.tempoChegada + v + 1<  mapa[tempI][tempJ].tempoChegada) {
  230.                                 //                              System.out.println("entrouAux");
  231.                                 mapa[tempI][tempJ].tempoChegada = u.tempoChegada + v + 1;
  232.                                 //distancia.inserir(mapa[tempI][tempJ]);
  233.                                 distancia.heap[aux].tempoChegada =  u.tempoChegada + v + 1;
  234.                             }
  235.                         }
  236.  
  237.                     }
  238.                 }
  239.                 //distancia.buildMinHeap();
  240.             }
  241.             /*          for (int i = 0; i < n; i++)
  242.             {
  243.                 for (int j = 0; j < m; j++)
  244.                 {
  245.                     System.out.println(mapa[i][j].c + "..." + mapa[i][j].tempoChegada);
  246.                 }
  247.             }*/
  248.  
  249.             int saida = ((a - s)  - c - (fim.tempoChegada*2));
  250.             arquivo.println(saida);
  251.             t--;
  252.         }
  253. //      System.out.println("Tempo Total: "+(System.currentTimeMillis()-tempoInicio));  
  254.  
  255.     }
  256.  
  257. }
  258.  
  259. class No{
  260.     char c;
  261.     int peso;
  262.     int tempoChegada;
  263.     int i;
  264.     int j; 
  265.     No next;
  266.     boolean visitado;
  267.     int distMin;
  268.  
  269.  
  270.     No (char c, int peso, int i, int j){
  271.         this.c = c;
  272.         this.peso = peso;
  273.         this.i = i;
  274.         this.j = j;
  275.         tempoChegada = 218309120;
  276.         visitado = false;
  277.     }
  278.  
  279.     public void visitar() {
  280.         this.visitado = true;
  281.  
  282.     }
  283.  
  284.  
  285.     public void pontoPartida() {
  286.         this.tempoChegada = 0;
  287.     }
  288. }
  289.  
  290.  
  291. class Heap{
  292.     No[] heap;
  293.     int maxsize;
  294.     int size;
  295.  
  296.     Heap(){
  297.         heap = new No[10];
  298.         maxsize = 10;
  299.         size = 0;
  300.     }
  301.  
  302.     public int isInMinHeap(int i, int j) {
  303.         int aux = 0;
  304.         while (aux < this.size) {
  305.             if (this.heap[aux].i == i && this.heap[aux].j == j)            
  306.                 break;
  307.             aux++;
  308.         }      
  309.         if (aux != this.size)
  310.             return aux;
  311.         else
  312.             return -1;
  313.     }
  314.  
  315.     Heap(int i) {
  316.         heap = new No[i];
  317.         maxsize = i;   
  318.         size = 0;
  319.     }
  320.  
  321.     No[] duplica(){
  322.         No[] aux = new No [2*maxsize];
  323.  
  324.         for (int i = 0; i < maxsize; i++) {
  325.             aux[i] = heap[i];
  326.         }
  327.         maxsize =  2*maxsize;
  328.         return aux;
  329.     }
  330.  
  331.     void inserir(No n){
  332.         if(size == maxsize){
  333.             heap = duplica();
  334.         }
  335.         heap[size] = n;
  336.         size++;
  337.     }
  338.  
  339.     No remover (){
  340.         No t =  null;
  341.         buildMinHeap();
  342.  
  343.         if(heap[0] != null){
  344.             t = heap[0];
  345.             heap[0] = heap[size-1];
  346.             heap[size-1] = null;
  347.             size--;
  348.         }
  349.         return t;
  350.     }
  351.  
  352.  
  353.     void buildMinHeap(){
  354.         for (int i = size/2; i >= 0; i--) {
  355.             minHeapfy(i);
  356.         }
  357.     }
  358.     void minHeapfy(int indice){
  359.         int l = 2*indice +1, r = 2*indice + 2, max = indice;
  360.  
  361.         if(l < size && heap[l].tempoChegada < heap[max].tempoChegada){
  362.             max = l;
  363.         }
  364.         if(r < size && heap[r].tempoChegada < heap[max].tempoChegada){
  365.             max = r;
  366.         }
  367.  
  368.         if(max != indice){
  369.             No aux = heap[indice];
  370.             heap[indice] = heap[max];
  371.             heap[max] =aux;
  372.             minHeapfy(max);
  373.         }
  374.     }
  375.  
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement