Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <queue>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6. typedef pair<int, int> pii;
  7.  
  8. int n, m;
  9. bool used[31][31] = {0};
  10. int dx[] = {0, 0, 1, -1};
  11. int dy[] = {-1, 1, 0, 0};
  12.  
  13. int bfs(pii from) {
  14.     int res = 1;
  15.     queue <pii> q;
  16.     used[from.first][from.second] = true;
  17.     q.push(from);
  18.     while (!q.empty()) {
  19.         pii cur = q.front();
  20.         q.pop();
  21.         for (int i = 0; i < 4; i++) {
  22.             pii to = cur;
  23.             to.first += dx[i];
  24.             to.second += dy[i];
  25.             if (to.first > n - 1 || to.first < 0) continue;
  26.             if (to.second > m - 1 || to.second < 0) continue;
  27.             if (used[to.first][to.second]) continue;
  28.             q.push(to);
  29.             res++;
  30.             used[to.first][to.second] = true;
  31.         }
  32.     }
  33.     return res;
  34. }
  35.  
  36. int main() {
  37.     freopen("test.in", "r", stdin);
  38.     freopen("test.out", "w", stdout);
  39.     int maxval = 0;
  40.     scanf("%d %d ", &n, &m);
  41.     for (int i = 0; i < n; i++)
  42.         for (int j = 0; j < m; j++) {
  43.             char x;
  44.             scanf("%c ", &x);
  45.             if (!(x - '0')) continue;
  46.             used[i][j] = true;
  47.         }
  48.     for (int i = 0; i < n; i++)
  49.         for (int j = 0; j < m; j++) {
  50.             if (used[i][j]) continue;
  51.             int cur = bfs(pii(i, j));
  52.             maxval = cur > maxval ? cur : maxval;
  53.         }
  54.     printf("%d", maxval);
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement