Advertisement
Guest User

Queijo_e_Rato

a guest
Nov 12th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct coordenada
  5. {
  6.     int x;
  7.     int y;
  8. } Coordenada;
  9.  
  10. int** alocaMatriz(int nl, int nc);
  11. void imprimeMatriz(int **m, int tam);
  12. void imprimeCriativ(int **m, int tam); //a ser implementada na fase das tarefas
  13. void inicializaLabirinto(int **m, int tam);
  14. void preencheMatrizCusto(int **m, int tam, int xQueijo, int yQueijo);
  15. Coordenada* calculaCaminho (int **m, int tam, int xRato, int yRato, int *nroPassos);
  16. //int testaCoordenadas(Coordenada c, int tam, char alvo[]);//a ser implementada na fase dos desafios
  17.  
  18. int main(){
  19.     int **lab,
  20.         tam, //tamanho fixo para fase de testes
  21.         nPassos, //vai armazenar o tamanho do caminho c
  22.         i;
  23.     Coordenada *c, //armazena as coordenadas do caminho
  24.     cRato,//coordenadas do Rato
  25.     cQueijo; //coordenadas do Queijo
  26.  
  27.  
  28.      tam=10;
  29.  
  30.  
  31.     cQueijo.x=5;
  32.     cQueijo.y=2;
  33.  
  34.     cRato.x=8;
  35.     cRato.y=8;
  36.  
  37.  
  38.     lab = alocaMatriz(tam, tam); //aloca labirinto
  39.     inicializaLabirinto(lab, tam); //inicializa com configura��o padr�o
  40.     imprimeMatriz(lab, tam); //imprime
  41.     preencheMatrizCusto(lab, tam, cQueijo.x, cQueijo.y); //preenche a matriz de custo, considerando a
  42.                                         //(5,2) como localiza��o do queijo - configura��o fixa
  43.                                         // para facilitar os testes
  44.  
  45.     imprimeMatriz(lab, tam);
  46.  
  47.     c=calculaCaminho(lab, tam, cRato.x, cRato.y, &nPassos); // preenche c com coordenadas do caminho entre as coordenadas
  48.                                         // do rato (8,8). Depois, alterar de tal forma
  49.                                             // a ler do teclado.
  50.  
  51.     //imprimir caminho de forma criativa
  52.     //desalocar todos vetores e matrizes alocados dinamicamente
  53.     return 0;
  54. }
  55.  
  56. int** alocaMatriz(int nl, int nc){
  57.  
  58.     int **m, i;
  59.  
  60.     m = (int**)malloc((unsigned int) nl * sizeof(int*));
  61.  
  62.     for(i=0; i<nl; i++)
  63.         m[i] = (int*) malloc(nc * sizeof(int));
  64.  
  65.  
  66.     return m;
  67.  
  68. }
  69.  
  70. void imprimeMatriz(int **m, int tam){
  71.  
  72.     int i,j;
  73.  
  74.     for(i=0; i<tam; i++){
  75.         for(j=0; j<tam; j++)
  76.             printf("%4d", m[i][j]);
  77.         printf("\n");
  78.     }
  79.     printf("\n");
  80. }
  81.  
  82.  
  83.  
  84. void inicializaLabirinto(int **m, int tam){
  85.  
  86.     int i,j;
  87.  
  88.     for(i=0; i<tam; i++)
  89.         for(j=0; j<tam; j++){
  90.             m[i][j] = -1;
  91.         }
  92.  
  93.  
  94.  
  95.     m[2][1] = -2;
  96.     m[2][2] = -2;
  97.     m[2][3] = -2;
  98.     m[3][3] = -2;
  99.     m[3][4] = -2;
  100.     m[4][4] = -2;
  101.     m[5][4] = -2;
  102.     m[6][4] = -2;
  103. }
  104.  
  105.  
  106. void preencheMatrizCusto(int **m, int tam, int xQueijo, int yQueijo){
  107.  
  108.     int i, j,
  109.         aux = 0,
  110.         flag=1;
  111.  
  112.     m[xQueijo][yQueijo] = 0;
  113.     while(flag == 1){
  114.         flag=0;
  115.         for(i=1; i<tam-1; i++){
  116.             for(j=1; j<tam-1; j++)
  117.                 if(m[i][j]==aux){
  118.                     if(m[i-1][j]==-1)
  119.                         m[i-1][j]=aux+1;
  120.                     if(m[i+1][j]==-1)
  121.                         m[i+1][j]=aux+1;
  122.                     if(m[i][j-1]==-1)
  123.                         m[i][j-1]=aux+1;
  124.                     if(m[i][j+1]==-1)
  125.                         m[i][j+1]=aux+1;
  126.                     flag=1;
  127.                 }
  128.         }
  129.         aux++;
  130.     }
  131. }
  132.  
  133.  
  134.  
  135. Coordenada* calculaCaminho (int **m, int tam, int xRato, int yRato, int *nroPassos){
  136.  
  137.   Coordenada *c,cCoord_caminho;
  138.   int **caminho,i,j,cont_caminho,aux;
  139.  
  140.   *nroPassos=m[xRato][yRato];
  141.  
  142.   caminho=(int **)malloc(*nroPassos*sizeof(int *));
  143.   for(i=0;i<*nroPassos;i++){
  144.     caminho[i]=(int *)malloc(2*sizeof(int));
  145.   }
  146.  
  147.   cont_caminho=0;
  148.  
  149.   caminho[0]={xRato,yRato};
  150.   while(*nroPassos!=cont_caminho){
  151.     if()
  152.   }
  153.  
  154.   /*while((i==xRato-1 && j==yRato) || (i==xRato && j==yRato+1) || (i==xRato+1 && j==yRato) || (i==xRato && j==yRato-1)){
  155.     if(m[i][j]==cont_caminho-1){
  156.       cCoord_caminho.x=i;
  157.       cCoord_caminho.y=j;
  158.       caminho[i]=cCoord_caminho;
  159.       cont_caminho--;
  160.     }
  161.   }*/
  162.  
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement