Advertisement
Es7evam

DFS

May 20th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  5. void mark(int **M, ,int x, int y){
  6.     if (M[x][y] == -1) return; //se for -1, nΓ£o marca
  7.    
  8.    
  9.     for (int d = 0; d < 4; ++d) {
  10.         int nx = x + dir[d][0]; //esq e dir (pegar o termo 0 das chaves)
  11.         int ny = y + dir[d][1]; //cima e baixo (pegar o termo 1 das chaves)
  12.         if (nx < 0 || ny < 0 || nx >= tam || ny >= tam) continue;
  13.         if (M[nx][ny] > M[x][y]+1 || M[nx][ny] == 0){
  14.             M[nx][ny] = M[x][y] + 1;
  15.         }
  16.         mark(**M, nx, ny);
  17.     }
  18. }
  19.  
  20.  
  21. int main(void){
  22.     int tam,i,j, poss;
  23.     printf("Digite o tamanho da matriz:\n");
  24.     scanf("%d", &tam);
  25.     int M[tam][tam];
  26.  
  27.     for(int i=0;i<tam;i++){
  28.         for(int j=0;j<tam;j++){
  29.             scanf("%d", &M[i][j]);
  30.         }
  31.     }
  32.    
  33.     printf("Digite as coordenadas de B\n");
  34.     scanf("%d %d", &i, &j);
  35.     M[i][j] = 1;
  36.     mark(x,y)
  37.     printf("Digite as coordenadas de A\n");
  38.     scanf("%d %d", &i, &j);
  39.  
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement