Codeforces Round 871 (Div. 4) 1829E - The Lakes Solution
Problem Link : https://codeforces.com/contest/1829/problem/E
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- const int mx = 1e3+3 ;
- int a[mx][mx] , vis[mx][mx] , ans , sum = 0 , n , m ;
- vector<int> dx={+1,-1,0,0};
- vector<int> dy={0,0,+1,-1};
- void dfs ( int i , int j ) {
- if ( i < 1 || i > n || j < 1 || j > m || vis[i][j] == 1 || a[i][j] == 0 )
- return ;
- sum += a[i][j] ;
- vis[i][j] = 1 ;
- for ( int ts = 0 ; ts < 4 ; ts++) {
- dfs(i + dx[ts], j + dy[ts]);
- }
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- cin >> n >> m ;
- for ( int i = 1 ; i <= n ; i++) {
- for ( int j = 1 ; j <= m ; j++){
- cin >> a[i][j];
- vis[i][j] = 0 ;
- }
- }
- int ans = 0 ;
- for ( int i = 1 ; i <= n ; i++) {
- for ( int j = 1 ; j <= m ; j++){
- sum = 0 ;
- dfs(i , j ) ;
- ans = max ( sum , ans ) ;
- }
- }
- cout << ans <<"\n";
- }
- return 0 ;
- }
No comments