Codeforces Round 871 (Div. 4) 1829E - The Lakes Solution



 Problem Link :  https://codeforces.com/contest/1829/problem/E

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. const int mx = 1e3+3 ;
  5. int a[mx][mx] , vis[mx][mx] , ans , sum = 0 , n , m ;
  6. vector<int> dx={+1,-1,0,0};
  7. vector<int> dy={0,0,+1,-1};
  8. void dfs ( int i , int j ) {
  9. if ( i < 1 || i > n || j < 1 || j > m || vis[i][j] == 1 || a[i][j] == 0 )
  10. return ;
  11. sum += a[i][j] ;
  12. vis[i][j] = 1 ;
  13. for ( int ts = 0 ; ts < 4 ; ts++) {
  14. dfs(i + dx[ts], j + dy[ts]);
  15. }
  16. }
  17. int32_t main() {
  18. ios_base::sync_with_stdio(0);
  19. cin.tie(0);
  20. int t;
  21. cin >> t;
  22. while (t--) {
  23. cin >> n >> m ;
  24. for ( int i = 1 ; i <= n ; i++) {
  25. for ( int j = 1 ; j <= m ; j++){
  26. cin >> a[i][j];
  27. vis[i][j] = 0 ;
  28. }
  29. }
  30. int ans = 0 ;
  31. for ( int i = 1 ; i <= n ; i++) {
  32. for ( int j = 1 ; j <= m ; j++){
  33. sum = 0 ;
  34. dfs(i , j ) ;
  35. ans = max ( sum , ans ) ;
  36. }
  37. }
  38. cout << ans <<"\n";
  39. }
  40. return 0 ;
  41. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.