Advertisement
DimasDark

L3Q3

Aug 11th, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //BSF ~
  8.  
  9.  
  10. typedef struct nofila
  11. {
  12.     int i;
  13.     int j;
  14.     struct nofila* proximo;
  15.  
  16. } No;
  17.  
  18. typedef struct fila
  19. {
  20.     No* cabeca;
  21.     No* rabo;
  22.     int numElementos;
  23.  
  24. } Fila;
  25.  
  26.  
  27.  
  28. Fila* inicializar()
  29. {
  30.  
  31.     Fila* f = new Fila();
  32.     f->cabeca = f->rabo = NULL;
  33.     f->numElementos = 0;
  34.     return f;
  35. }
  36.  
  37. //Insere no rabo
  38. void insere(Fila* f, int i, int j)
  39. {
  40.     f->numElementos++;
  41.     No* novo = new No();
  42.     novo -> i = i;
  43.     novo -> j = j;
  44.     novo ->proximo = NULL;
  45.     if (!f->cabeca)
  46.     {
  47.         f->cabeca = f->rabo = novo;
  48.         return;
  49.     }
  50.  
  51.     f->rabo->proximo = novo;
  52.     f->rabo = novo;
  53.  
  54. }
  55.  
  56. //Retira da cabeça
  57. No* retira(Fila* f)
  58. {
  59.     if (!f->cabeca)
  60.     {
  61.         return NULL;
  62.     }
  63.     f->numElementos--;
  64.     No* ret = f->cabeca;
  65.  
  66.     if (f->cabeca == f->rabo)
  67.     {
  68.         f->cabeca = f->rabo = NULL;
  69.         return ret;
  70.     }
  71.     No* aux = f->cabeca;
  72.     f->cabeca = f->cabeca->proximo;
  73.     delete(aux);
  74.     return ret;
  75. }
  76.  
  77.  
  78. void imprimeFila(Fila* f)
  79. {
  80.     No* p;
  81.     for (p = f->cabeca; p != NULL; p = p->proximo)
  82.     {
  83.         if (p->proximo == NULL)
  84.             cout << "[" << p->i << "," << p->j << "]";
  85.         else
  86.             cout << "[" << p->i << "," << p->j << "]" << " ";
  87.     }
  88.  
  89. }
  90.  
  91. Fila* esvaziarFila(Fila* f)
  92. {
  93.     while (f->numElementos > 0)
  94.         retira(f);
  95.     return f;
  96. }
  97.  
  98. bool isVazia(Fila* f)
  99. {
  100.     return f->cabeca == NULL;
  101. }
  102.  
  103. bool existe(Fila* f, int n, int m)
  104. {
  105.     No* aux = f->cabeca;
  106.     while (aux != NULL)
  107.     {
  108.         if (aux->i == n && aux->j == m)
  109.             return true;
  110.         aux = aux->proximo;
  111.     }
  112.     return false;
  113.  
  114. }
  115.  
  116. int numElementos(Fila* f)
  117. {
  118.     No* aux = f->cabeca;
  119.     int n = 0;
  120.     while (aux)
  121.     {
  122.         n++;
  123.         aux = aux->proximo;
  124.     }
  125.     return n;
  126.  
  127. }
  128.  
  129.  
  130. int main()
  131. {
  132.     freopen("L3Q3.in", "r", stdin);
  133.     freopen("L3Q3.out", "w", stdout);
  134.  
  135.     int k; //k de "kasos"
  136.     int q, n, m; //Matriz ~
  137.     cin >> k;
  138.     while (k > 0)
  139.     {
  140.         int saida = 0;
  141.         cin >> q;
  142.         cin >> n;
  143.         cin >> m;
  144. //        cout << q << " " << n << " " << m << endl;
  145.         int mapa[n][m];
  146.  
  147.         //Lê a matriz
  148.         for (int i = 0; i < n; i++)
  149.         {
  150.             for (int j = 0; j < m; j++)
  151.             {
  152.                 cin >> mapa[i][j];
  153.             }
  154.         }
  155.  
  156. //        for (int i = 0; i < n; i++)
  157. //        {
  158. //            for (int j = 0; j < m; j++)
  159. //            {
  160. //                if (j == m-1)
  161. //                    cout << mapa[i][j] << endl;
  162. //                else
  163. //                    cout << mapa[i][j] << " ";
  164. //            }
  165. //        }
  166.  
  167. //        cout << "chegou aqui";
  168.  
  169.         //Faz BFS
  170.         Fila* f = new Fila();
  171.         f = inicializar();
  172.  
  173.         bool marcados[n][m]; //inicializa array
  174.  
  175. //        cout << "passa do bool";
  176.  
  177.         for (int i = 0; i < n; i++)
  178.         {
  179.             for (int j = 0; j < m; j++)
  180.             {
  181.                 if(mapa[i][j] > q)
  182.                 {
  183.                     marcados[i][j] = true; //marcando o que não é buraco
  184.                 }
  185.  
  186.                 if (!marcados[i][j])   //É buraco
  187.                 {
  188.                     marcados[i][j] = true;
  189.                     insere(f, i, j);
  190.                     while (!isVazia(f))
  191.                     {
  192.                         No* aux = retira(f);
  193.                         int minSubX = ((aux->i -1) < 0) ? 0 : aux->i -1 ;
  194.                         int minSubY = ((aux->j-1) <0) ? 0 : aux->j -1 ;
  195.                         int maxSubX = ((aux->i +1) > n) ? n : aux->i +1 ;
  196.                         int maxSubY = ((aux->j+1) >= m) ? m : aux->j +1 ;
  197.                         for(int x = minSubX; x <= maxSubX; x++)
  198.                         {
  199.                             for(int y = minSubY; y <= maxSubY; y++)
  200.                             {
  201.                                 if(x != i || y != j)
  202.                                 {
  203.                                     if(mapa[x][y] <= q && !marcados[x][y])
  204.                                     {
  205. //                                        cout << "valorI:" << x << " ValorJ:" << y << " Tipo:" << mapa[x][y] << endl;
  206.                                         marcados[x][y] = true;
  207.                                         insere(f, x, y);
  208.                                     }
  209.                                 }
  210.                             }
  211.                         }
  212.  
  213.  
  214.  
  215.  
  216.                     }
  217.                     saida++;
  218.                 }
  219.  
  220.             }
  221.         }
  222.          cout << saida << endl;
  223.  
  224. //        //Teste vizinhos
  225. //        int i = 0, j = 0;
  226. //        cout << "vizinhos de " << "[" << i << ", " << j << "]";
  227. //        for(int x = max(0, i-1); x <= min(i+1, n); x++)
  228. //        {
  229. //            for(int y = max(0, j-1); y <= min(j+1, m); y++)
  230. //            {
  231. //                if(x != i || y != j)
  232. //                {
  233. //                    cout << "valorI:" << x << "ValorJ:" << y << "Tipo:" << mapa[x][y] << endl;
  234. //                }
  235. //            }
  236. //        }
  237.  
  238.  
  239.  
  240.         //fim BFS
  241.  
  242.         k--;
  243.     }
  244.     return 0;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement