Browse Source

搜索岛屿最大面积,深搜或广搜都可

爱玲姐姐 5 years ago
parent
commit
b07a33f4fc
1 changed files with 61 additions and 0 deletions
  1. 61 0
      B.cpp

+ 61 - 0
B.cpp

@@ -0,0 +1,61 @@
+#include <bits/stdc++.h>
+using namespace std;
+ 
+const int MXAN = 200 + 10;
+int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
+int a[MXAN][MXAN];
+int sum[MXAN][MXAN];
+int n, m;
+ 
+int Bfs(int x, int y)
+{
+    int res = 0;
+    queue<pair<int, int> > que;
+    que.push(make_pair(x, y));
+    a[x][y] = 0;
+    while (!que.empty())
+    {
+        res++;
+        int x = que.front().first;
+        int y = que.front().second;
+        que.pop();
+        for (int i = 0;i < 4;i++)
+        {
+            int tx = x + Next[i][0];
+            int ty = y + Next[i][1];
+            if (a[tx][ty] == 1)
+            {
+                a[tx][ty] = 0;
+                que.push(make_pair(tx, ty));
+            }
+        }
+    }
+    return res;
+}
+ 
+int main()
+{
+    int t;
+    cin >> t;
+    while (t--)
+    {
+ 
+        cin >> n >> m;
+        for (int i = 1;i <= n;i++)
+            for (int j = 1;j <= m;j++)
+                cin >> a[i][j];
+        int res = 0;
+        for (int i = 1;i <= n;i++)
+            for (int j = 1;j <= m;j++)
+            {
+                if (a[i][j] == 1)
+                {
+                    res = max(res, Bfs(i, j));
+                     
+                }
+            }
+        cout << res << endl;
+    }
+ 
+    return 0;
+}