fork download
  1. #include<bits/stdc++.h>
  2. #include <functional>
  3. using namespace std;
  4. #define khalil ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  5. #define ordered_set tree < int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update >
  6. #define el "\n"
  7. #define ll long long
  8. #define all(v) v.begin(),v.end()
  9. #define ld long double
  10. #define pi acos(-1)
  11. #define tc ll t ; cin >> t ; while (t--)
  12. bool checkbit(int x , int i) // check the value of the ith bit 1 or 0
  13. {
  14. return x&(1<<i) ;
  15. }
  16. long long gcd(ll q, ll v)
  17. {
  18. if (v == 0)
  19. return q;
  20. return gcd(v, q % v);
  21. }
  22. long long lcm(ll a, ll b)
  23. {
  24. return ((a*b)/ gcd(a, b));
  25. }
  26. ll fastpower (ll base , ll exp)
  27. {
  28. ll res = 1 , x = base;
  29. while(exp)
  30. {
  31. if(exp&1) res*=x;
  32. exp>>=1;
  33. x*=x;
  34. }
  35. return res;
  36. }
  37. template < typename T > istream &operator >> (istream &in, vector < T > &v) {
  38. for (auto &x: v) in >> x;
  39. return in;
  40. }
  41. template < typename T > ostream &operator << (ostream &out, vector < T > &v) {
  42. for (auto &x: v) out << x << ' ';
  43. return out;
  44. }
  45. bool comp(pair<int,int> x,pair<int,int>y)
  46. {
  47. return x.second<=y.second ;
  48. }
  49. const int N =2e4+5;
  50. const ll mod=1e9+7;
  51. vector<bool> prime(N,1);
  52. void sieve (int x)
  53. {
  54. prime[0]=prime[1]=0;
  55. for(int i=2;i<=x;i++)
  56. {
  57. if(prime[i])
  58. {
  59. for(int j=i+i;j<=x;j+=i)prime[j]=0;
  60. }
  61. }
  62. }
  63. ll logatob (ll a , ll b)
  64. {
  65. return logl(a)/logl(b) ;
  66. }
  67. void init()
  68. {
  69. }
  70. int dx[8]={0,-1,1,-1,1,0,1,-1};
  71. int dy[8]={1,1,1,0,0,-1,-1,-1};
  72. int main()
  73. {
  74. khalil
  75. //freopen("input.txt","r",stdin) ;
  76. //freopen("output.txt", "w", stdout);
  77. /*tc
  78.   {
  79.   }*/
  80. int n;cin>>n;
  81. cin.ignore();
  82. cin.ignore();
  83. while(n--)
  84. {
  85. int mx = INT_MIN;
  86. string s;
  87. vector<string> v;
  88. map<pair<int,int>,int> mp ;
  89. while(true)
  90. {
  91. getline(cin,s);
  92. if(s=="")break;
  93. v.push_back(s);
  94. }
  95. for(int i=0;i<v.size();i++)
  96. {
  97. for(int j=0;j<v[i].size();j++)
  98. {
  99. if(v[i][j]=='1' and mp[{i,j}]==0)
  100. {
  101. queue<pair<int,int>> q;
  102. q.push({i,j});
  103. mp[{i,j}]=1;
  104. while(!q.empty())
  105. {
  106. pair<int,int> cur = q.front();
  107. mx=max(mx,mp[cur]);
  108. q.pop();
  109. for(int i=0;i<8;i++)
  110. {
  111. int nx=cur.first+dx[i];
  112. int ny=cur.second+dy[i];
  113. if(nx>=0 and nx<v.size() and ny>=0 and ny<v[0].size() and mp[{nx,ny}]==0)
  114. {
  115. mp[{nx,ny}]=mp[cur]+1;
  116. q.push({nx,ny});
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. cout<<mx<<el;
  124. if(n!=0)cout<<el;
  125. }
  126. }
  127.  
Success #stdin #stdout 0.01s 5304KB
stdin
20

000001000000
001011010000
000000000001
000000100100
000110001000
101000001000
000100011011
000011110100
000000000001

0100001
0100000
0000010

000011010000
110101000000
001110001011
001000001100

0
0
0
0

00001
00101
10000
01101
10001
10000
01010
00000
00100
01100
10001
10000
00000
11100
01100
10011
11000
10000
00010
00010
00100

11011010010001110
01000000100000100

010010110010011110
000010000010000110
010001001001000010
000010001001101100
110100010000101010
000000010011010011
001110011000000010
100000000100100100
000100000110010001
000100100000001000
001001001010001100
000001000010001010
000000100100011001
000000000000000000
001000010000000000
000000000010000000

00
01
00
00
01
10
00
11
01
00
00
10
00
11
10
00
01
00
00
11
00
00
10

0000010000
0000000000
0000000010
0100000000
0100000100
0001000000

00
10
00
00
01

00
10
01
10
00
00
11
00
01
10
01
01
00
00
00
00
00
01

11000010000
00101100000
00010000000
01001110001
00100000110
01000000000
00001000000
00000000010

0010101
0010000
0100000
1000010
0001000
0000001
1110100
0110001

0010000010000000000001
0010000101000000100010
1010010110000100100010
0000000000000101100000
0000010001000101010001
0000000110001010100010
0101110000000000100000
0010100000000011100000

0000001001010001
0001000011000001
1001000001000100

0
0
1
0
0
0
0
0
1
0
0
1
0

00101
01000
10001
10000
00100
00000

01000
11000
10000
00000
00001
00000
00000
00000
00001
10000
01001
00000
00000
00000
00110
00011
00000
01001

00000001
01001010
11000000
10100001
10000000
00110100
10000010
11000000
10000000
00000101
00000010
10000110
10000000
01001100
00001011
10000000
00011100
11011000
10010000
00100000
00010000

0000
0010
0001
0110
stdout
9

6

8

-2147483648

21

17

17

22

6

4

17

11

8

20

10

11

6

18

21

3