Advertisement
velimir

Nafta277

Feb 20th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int x, y;
  6. char mat[101][101];
  7.  
  8. void BFS(int a, int b)
  9. {
  10.     queue<int> redx, redy;
  11.     redx.push(a);
  12.     redy.push(b);
  13.     while(!redx.empty());
  14.     {
  15.         int m = redx.front();
  16.         int n = redy.front();
  17.         redx.pop();
  18.         redy.pop();
  19.         mat[m][n]='*';
  20.         if(m+1<x and mat[m+1][n] == '@') { redx.push(m+1); redy.push(n); }
  21.         if(m+1<x and n+1<y and mat[m+1][n+1] == '@') { redx.push(m+1); redy.push(n+1); }
  22.         if(n+1<y and mat[m][n+1] == '@') { redx.push(m); redy.push(n+1); }
  23.         if(m-1>=0 and n+1<y and mat[m-1][n+1] == '@') { redx.push(m-1); redy.push(n+1); }
  24.         if(m-1>=0 and mat[m-1][n] == '@') { redx.push(m-1); redy.push(n); }
  25.         if(m-1>=0 and n-1>=0 and mat[m-1][n-1] == '@') { redx.push(m-1); redy.push(n-1); }
  26.         if(n-1>=0 and mat[m][n-1] == '@') { redx.push(m); redy.push(n-1); }
  27.         if(m+1<x and n-1>=0 and mat[m+1][n-1] == '@') { redx.push(m+1); redy.push(n-1); }
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     int i, j, nafteniP=0;
  34.     cin >> x >> y;
  35.     for(i=0; i<x; i++)
  36.         for(j=0; j<y; j++)
  37.             cin >> mat[i][j];
  38.  
  39.     for(i=0; i<x; i++)
  40.     {
  41.         for(j=0; j<y; j++)
  42.         {
  43.             if(mat[i][j] == '@'){ nafteniP++; BFS(i, j);}
  44.         }
  45.  
  46.     }
  47.     cout << nafteniP;
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement