Advertisement
UNoobAle

UVA 572

Dec 31st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std; typedef long long ll; typedef unsigned long long ull; const int mod = 1e9 + 7;
  3. #define fi first
  4. #define se second
  5. #define pb push_back
  6. #define mp make_pair
  7. //#define int ll
  8.  
  9. int n, m, idx = 0, cnt;
  10. char a[202][202];
  11. bool visited[202][202];
  12.  
  13. void dfs(int i, int j)
  14. {
  15.     if (visited[i][j])
  16.     {
  17.         return;
  18.     }
  19.     if (i == 0 || j == 0 || i > n || j > m)
  20.     {
  21.         return;
  22.     }
  23.     if (a[i][j] != '@')
  24.     {
  25.         return;
  26.     }
  27.     visited[i][j] = 1;
  28.     dfs(i, j + 1);
  29.     dfs(i + 1, j);
  30.     dfs(i - 1, j);
  31.     dfs(i, j - 1);
  32.     dfs(i - 1, j + 1);
  33.     dfs(i + 1, j - 1);
  34.     dfs(i + 1, j + 1);
  35.     dfs(i - 1, j - 1);
  36. }
  37.  
  38. signed main()
  39. {
  40.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  41.     while (cin >> n >> m)
  42.     {
  43.         if (!n && !m)
  44.         {
  45.             return 0;
  46.         }
  47.         memset(visited, 0, sizeof visited);
  48.         idx++;
  49.         cnt = 0;
  50.         for (int i = 1; i <= n; i++)
  51.         {
  52.             for (int j = 1; j <= m; j++)
  53.             {
  54.                 cin >> a[i][j];
  55.             }
  56.         }
  57.         for (int i = 1; i <= n; i++)
  58.         {
  59.             for (int j = 1; j <= m; j++)
  60.             {
  61.                 if (!visited[i][j] && a[i][j] == '@')
  62.                 {
  63.                     dfs(i, j);
  64.                     cnt++;
  65.                 }
  66.             }
  67.         }
  68.         cout << cnt << endl;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement