Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- //{
- //#pragma gcc optimize("Ofast,unroll-loops")
- //#pragma gcc target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,abm,mmx,popcnt,tune=native")
- //#define int long long
- //#define double long double
- #define MP make_pair
- #define F first
- #define S second
- #define MOD 1000000007
- //}
- bool g[1002][1002], vis[1002][1002];
- int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
- void dfs(int x, int y)
- {
- vis[x][y] = true;
- for (int i = 0; i < 4; i++)
- {
- int nx = x + dx[i], ny = y + dy[i];
- if (!vis[nx][ny] && g[nx][ny])
- dfs(nx, ny);
- }
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int T;
- cin >> T;
- while (T--)
- {
- int N, M;
- cin >> N >> M;
- for (int i = 0; i <= N + 1; i++)
- for (int j = 0; j <= M + 1; j++)
- g[i][j] = false, vis[i][j] = false;
- for (int i = 1; i <= N; i++)
- {
- string s;
- cin >> s;
- for (int j = 1; j <= M; j++)
- if (s[j - 1] == '.')
- g[i][j] = true;
- }
- int ans = 0;
- for (int i = 1; i <= N; i++)
- for (int j = 1; j <= M; j++)
- if (!vis[i][j] && g[i][j])
- ans++, dfs(i, j);
- cout << ans << '\n';
- }
- return 0;
- }
- // ***** ***** * * * ***** * * ***** *****
- // * * * * * * * ** * * * *
- // * * * ***** * * * * * * *****
- // * * * * * * * * ** * *
- // **** ***** * ***** ***** * * * *
- // * * *** * * **
- // * * * * * * **
- // * * * ***** ***** **
- // * * * * * * *
- // * * * * * * **
Advertisement
Add Comment
Please, Sign In to add comment