Guest User

Untitled

a guest
Dec 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct tipo
  4. {
  5.     int linha;
  6.     int coluna;
  7.     int custo;
  8. }Tipo;
  9. int Tamanho;
  10. void Heapify (Tipo*vetor,int tamanho,int  i)
  11. {
  12.     int menor;
  13.     Tipo*temp=(Tipo*)malloc(sizeof(Tipo));
  14.     while(i<=tamanho/2)
  15.     {
  16.         if(i<(tamanho/2+tamanho%2))
  17.         {
  18.             if(vetor[2*i].custo<vetor[2*i+1].custo)
  19.             {
  20.                 menor=2*i;
  21.             }
  22.             else
  23.             {
  24.                 menor=2*i+1;
  25.             }
  26.         }
  27.         else
  28.         {
  29.             menor=2*i;
  30.         }
  31.         if(vetor[i].custo>vetor[menor].custo)
  32.         {
  33.             temp->coluna=vetor[i].coluna;
  34.             temp->linha=vetor[i].linha;
  35.             temp->custo=vetor[i].custo;
  36.             ///
  37.             vetor[i].linha=vetor[menor].linha;
  38.             vetor[i].coluna=vetor[menor].coluna;
  39.             vetor[i].custo=vetor[menor].custo;
  40.             ///
  41.             vetor[menor].coluna=temp->coluna;
  42.             vetor[menor].linha=temp->linha;
  43.             vetor[menor].custo=temp->custo;
  44.             i=menor;
  45.         }
  46.         else
  47.         {
  48.             i=tamanho;
  49.         }
  50.  
  51.     }
  52.     free(temp);
  53. }
  54. void constroi(Tipo*vetor,int tamanho)
  55. {
  56.     int i;
  57.     for(i=tamanho/2;i>=1;i--)
  58.     {
  59.         Heapify (vetor,tamanho,i);
  60.     }
  61. }
  62. Tipo* Remove(Tipo*heap)
  63. {
  64.     heap[1]=heap[Tamanho];///o primeiro recebe o ultimo elemento e diminui o tamanho
  65.     Tamanho--;
  66.     constroi(heap,Tamanho);///faz heapfy
  67.     return heap;
  68. }
  69. void Imprime(Tipo*heap)
  70. {
  71.     int i;
  72.     for(i=1;i<=Tamanho;i++)
  73.     {
  74.         printf("x= %d,y= %d e custo =%d\n",heap[i].linha,heap[i].coluna,heap[i].custo);
  75.     }
  76. }
  77. int main()
  78. {
  79.    FILE*entrada=fopen("L4Q3.in","r");
  80.    FILE*saida=fopen("L4Q3.out","w");
  81.    int i,j,qtd,contador;
  82.    fscanf(entrada,"%d",&qtd);
  83.    contador=0;
  84.    int linhas,colunas;
  85.    int matriz[600][600];
  86.    int marcado[600][600];
  87.    Tipo*vetor;//pra heap
  88.    int dx[4]= {0,0,-1,1};
  89.    int dy[4]= {1,-1,0,0};
  90.    int x,y;
  91.    int c;
  92.    //int tamanho;
  93.    int destinox,destinoy;
  94.    while(contador<qtd)
  95.    {
  96.        printf("%d\n",contador);
  97.        Tamanho=1;
  98.        //tamanho=1;
  99.        contador++;
  100.        fscanf(entrada,"%d %d",&linhas,&colunas);
  101.        destinox=linhas-1;
  102.        destinoy=colunas-1;
  103.        for(i=0;i<linhas;i++)
  104.        {
  105.            for(j=0;j<colunas;j++)
  106.            {
  107.                fscanf(entrada,"%d",&matriz[i][j]);
  108.                //printf("%d\n",matriz[i][j]);
  109.                marcado[i][j]=0;
  110.            }
  111.        }
  112.        vetor=(Tipo*)malloc(sizeof(Tipo)*(10000));
  113.        vetor[1].linha=0;
  114.        vetor[1].coluna=0;
  115.        vetor[1].custo=matriz[0][0];
  116.        while(vetor[1].linha!=destinox || vetor[1].coluna!=destinoy)
  117.        {
  118.            //printf("contador eh %d\n",contador);
  119.            //Imprime(vetor);
  120.            ///tira do heap
  121.            x=vetor[1].linha;
  122.            y=vetor[1].coluna;
  123.            c=vetor[1].custo;
  124.            //printf("depois de remover\n");
  125.            vetor=Remove(vetor);
  126.            //Tamanho--;
  127.            //Imprime(vetor);
  128.            //printf("eu tou em %d\n",matriz[x][y]);
  129.            marcado[x][y]=1;///marca
  130.            for (i=0; i<4; i++)
  131.             {
  132.                 if (x+dx[i]>=0 && y+dy[i]>=0)
  133.                 {
  134.                     if (x+dx[i]<linhas && y+dy[i]<colunas)
  135.                     {
  136.                         if (marcado[x+dx[i]][y+dy[i]]==0)
  137.                         {
  138.                             Tamanho++;
  139.                             vetor[Tamanho].linha=x+dx[i];
  140.                             vetor[Tamanho].coluna=y+dy[i];
  141.                             vetor[Tamanho].custo=matriz[x+dx[i]][y+dy[i]]+c;
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.               constroi(vetor,Tamanho);
  147.        }
  148.        fprintf(saida,"%d\n",vetor[1].custo);
  149.        free(vetor);
  150.  
  151.  
  152.    }
  153.     return 0;
  154. }
Add Comment
Please, Sign In to add comment