Advertisement
Asif_Anwar

UVA-871

Jan 31st, 2022
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. //using namespace chrono;
  5.  
  6. typedef long long ll;
  7. typedef vector< int > vi;
  8. typedef vector< ll > vl;
  9. typedef map<int, int > mp;
  10. typedef pair< int, int > pii;
  11.  
  12. #define pb push_back
  13. #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  14. #define F first
  15. #define S second
  16.  
  17. #define debug cout << -1 << endl;
  18. #define REP(i, a, b) for(int i=a; i<b; i++)
  19. #define f0r(i, n) for (int i = 0; i < n; ++i)
  20. #define f0r1(i, n) for (int i = 1; i <= n; ++i)
  21. #define r0f(i, n) for(int i=n-1; i>=0; i--)
  22. #define r0f1(i, n) for(int i=n; i>=1; i--)
  23. #define fore(a, x) for (auto& a : x)
  24. #define fori(i, a, b) for (int i = (a); i < (b); ++i)
  25.  
  26. #define MP make_pair
  27. #define UB upper_bound
  28. #define LB lower_bound
  29. #define nw cout << "\n"
  30.  
  31. #define issq(x) (((ll)(sqrt((x))))*((ll)(sqrt((x))))==(x))
  32. #define rev(v) reverse(v.begin(),v.end())
  33. #define asche cerr<<"Ekhane asche\n";
  34. #define rev(v) reverse(v.begin(),v.end())
  35. #define srt(v) sort(v.begin(),v.end())
  36. #define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
  37. #define all(v) v.begin(),v.end()
  38. #define mnv(v) *min_element(v.begin(),v.end())
  39. #define mxv(v) *max_element(v.begin(),v.end())
  40. #define valid(tx,ty) (tx>=0 && tx<n && ty>=0 && ty<m)
  41. #define one(x) __builtin_popcount(x)
  42. //#define pop pop_back
  43. #define setPrec(x) cout << fixed << setprecision(x)
  44. #define sz(a) (int)a.size()
  45. //#define fin cin
  46. //#define fout cout
  47. const int INF = 1e9;
  48. const ll MOD = (ll)1e9+7;
  49. const ll INFL = 1e18;
  50. const ll mnINF = -1e18;
  51. const double diff = 10e-6;
  52. const int maxn = 200005;
  53. const double PI = acos(-1);
  54. using namespace std;
  55.  
  56. int dx[] = {-1, 0, 1};
  57. int dy[] = {-1, 0, 1};
  58.  
  59. // int dx[] = {-1, 0, 0, 1};
  60. // int dy[] = {0, -1, 1, 0};
  61.  
  62. bool vis[30][30];
  63. string arr[30];
  64.  
  65. int n, m;
  66. int cnt = 0;
  67. string temp = "";
  68. bool flag = true;
  69.  
  70. void dfs(int x, int y)
  71. {
  72.     vis[x][y] = true;
  73.     cnt++;
  74.     for(int i=0; i<3; i++) {
  75.         for(int j=0; j<3; j++) {
  76.             int newX = x+dx[i];
  77.             int newY = y+dy[j];
  78.             if(valid(newX, newY) && !vis[newX][newY] && arr[newX][newY]=='1') {
  79.                 dfs(newX, newY);
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. void solve()
  86. {
  87.     n = 0, m = 0;
  88.     string sss;
  89.     while(getline(cin, sss)) {
  90.         if(sss.size()) {
  91.             arr[n] = sss;
  92.             n++;
  93.         }
  94.         else {
  95.             break;
  96.         }
  97.     }
  98.     m = arr[0].size();
  99.     memset(vis, false, sizeof(vis));
  100.    
  101.     int ans = 0;
  102.     for(int i=0; i<n; i++) {
  103.         for(int j=0; j<m; j++) {
  104.             if(!vis[i][j] && arr[i][j]=='1') {
  105.                 cnt = 0;
  106.                 dfs(i, j);
  107.                 ans = max(ans, cnt);
  108.             }
  109.         }
  110.     }
  111.     // nw;
  112.     cout << ans << "\n";
  113. }  
  114.  
  115. void setIO(string name = "") { // name is nonempty for USACO file I/O
  116.  
  117.     ios_base::sync_with_stdio(0); cin.tie(0); // see Fast Input & Output
  118.  
  119.     // alternatively, cin.tie(0)->sync_with_stdio(0);
  120.  
  121.     if (name.size()) {
  122.  
  123.         freopen((name+".in").c_str(), "r", stdin); // see Input & Output
  124.  
  125.         freopen((name+".out").c_str(), "w", stdout);
  126.  
  127.     }
  128.  
  129. }
  130.  
  131. int main()
  132. {
  133. #ifndef ONLINE_JUDGE
  134.     freopen("inputf.in", "r", stdin);
  135.     freopen("outputf.in", "w", stdout);
  136. #endif
  137.     //setIO("breedflip");
  138.     // FastIO;
  139.     int t;
  140.     t = 1;
  141.     cin >> t;
  142.     string str;
  143.     getline(cin, str);
  144.     getline(cin, str);
  145.     for(int i=0; i<t; i++) {
  146.         if(i) nw;
  147.         // cout << "Case " << i+1 << ": ";
  148.         solve();
  149.     }
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement