baovenongtrangquoc14
#include <iostream> using namespace std; int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; int n, m; int a[1005][1005]; int visited[1005][1005]; int check = 1; int h; void dfs(int i, int j) { visited[i][j] = 1; for (int k = 0; k < 8; k++) { int x_next = i + dx[k]; int y_next = j + dy[k]; if (!visited[x_next][y_next] && a[i][j] == a[x_next][y_next]) dfs(x_next, y_next); if (h < a[x_next][y_next]) check = 0; } } void solve(int testcase) { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j =1; j<= m; j++) { cin >> a[i][j]; } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (visited[i][j] == 0 && a[i][j] != 0) { h = a[i][j]; check = 1; dfs(i, j); ans += check; } } } cout << "#" << testcase << " " << ans << endl; for (int i = 0; i < 1005; i++) { for (int j = 0; j < 1005; j++) { visited[i][j] = 0; } } } int main() { freopen("Text.txt", "r", stdin); int t; cin >> t; for (int i = 1; i <= t; i++) { solve(i); } return 0; }
Leave a Comment