Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define pii pair<int, int>
- using namespace std;
- int fx[]= {+0,+0,+1,-1,-1,+1,-1,+1};
- int fy[]= {-1,+1,+0,+0,+1,+1,-1,-1};
- int ans;
- string grid[26];
- bool vis[26][26];
- void bfs(int sx, int sy, int n, int m)
- {
- int cnt = 1;
- vis[sx][sy] = true;
- queue<pii>q;
- q.push(pii(sx, sy));
- while(!q.empty())
- {
- pii top = q.front();
- q.pop();
- for(int i = 0; i < 8; i++)
- {
- int tx = top.first + fx[i];
- int ty = top.second + fy[i];
- if(tx >= 0 && tx < n && ty >= 0 && ty < m && vis[tx][ty] == false && grid[tx][ty] == '1')
- {
- vis[tx][ty] = true;
- grid[tx][ty] = '0';
- q.push(pii(tx, ty));
- cnt++;
- }
- }
- }
- // cout << "cnt = " << cnt << endl;
- ans = max(ans, cnt);
- }
- int main()
- {
- int tc;
- while(cin >> tc)
- {
- while(tc--)
- {
- int row = 0, col;
- while(cin >> grid[row])
- {
- col = grid[row].size();
- if(col == 0 )
- break;
- row++;
- }
- for(int i = 0; i < row; i++)
- {
- for(int j = 0; j < col; j++)
- {
- if(grid[i][j] == '1')
- bfs(i, j, row, col);
- }
- }
- //cout << "len = " << len << endl;
- cout << ans << endl;
- if(tc)
- cout << endl;
- memset(vis, 0, sizeof(vis));
- for(int i = 0; i < row; i++)
- grid[i].clear();
- ans = 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement