Sfoglia il codice sorgente

上传文件至 ''

webturing 5 anni fa
parent
commit
c44015c5e4
3 ha cambiato i file con 168 aggiunte e 0 eliminazioni
  1. 63 0
      J.cpp
  2. 43 0
      KDfs.cpp
  3. 62 0
      L.cpp

+ 63 - 0
J.cpp

@@ -0,0 +1,63 @@
+#include<bits/stdc++.h>
+
+using namespace std;
+int a[500][500];
+int n, m;
+
+void input() {
+    cin >> n >> m;
+    for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)cin >> a[i][j];
+}
+
+bool ok() {
+    for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (a[i][j])return false;
+    return true;
+}
+
+void solve() {
+    input();
+    int areas = 0, Max = 0;
+    while (!ok()) {
+        int cnt = 0;
+        queue<pair<int, int>> Q;//创建一个队列
+        ///Q.push(make_pair(0,0));//起点加入队列
+        //a[0][0]=0;
+        int X = 0, Y = 0;
+        for (int i = 0; i < n; i++)
+            for (int j = 0; j < m; j++)
+                if (a[i][j]) {
+                    X = i;
+                    Y = j;
+                    break;//遇到1就停下
+                }
+        Q.push(make_pair(X, Y));
+        a[X][Y] = 0;
+        while (Q.size()) {
+            pair<int, int> cur = Q.front();//获得队列的第一个元素
+            int x = cur.first, y = cur.second;
+            for (int dx = -1; dx <= 1; ++dx)
+                for (int dy = -1; dy <= 1; ++dy) { //按照宽度去扩展
+                    if (abs(dx - dy) != 1)continue;
+                    int nx = x + dx, ny = y + dy;
+                    if (nx < 0 || nx > n - 1 || ny < 0 || ny > n - 1 || a[nx][ny] == 0)continue;
+                    Q.push(make_pair(nx, ny));
+                    a[nx][ny] = 0;
+                }
+            //cout<<cur.first<<cur.second<<endl;//计数
+            ++cnt;
+            Q.pop();//出队
+        }
+        Max = max(cnt, Max);
+    }
+    cout << Max << endl;
+}
+
+int main() {
+    //freopen("a.in","r",stdin);
+    int t;
+    cin >> t;
+    while (t--) {
+        solve();
+    }
+    return 0;
+}

+ 43 - 0
KDfs.cpp

@@ -0,0 +1,43 @@
+#include<bits/stdc++.h>
+
+using namespace std;
+int n, m, flag = 0;
+char s[500][500];
+bool use[500][500];
+
+void dfs(int x, int y) {
+    if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] == 'x' || use[x][y])
+        return;
+    if (s[x][y] == 'L') {
+        use[x][y] = true;
+        flag = 1;
+        return;
+    }
+    use[x][y] = true;
+    dfs(x + 1, y);
+    dfs(x, y + 1);
+    dfs(x - 1, y);
+    dfs(x, y - 1);
+}
+
+int main() {
+
+    int Startx, Starty;
+    while (cin >> n >> m) {
+        flag = 0;
+        for (int i = 0; i < n; i++)
+            for (int j = 0; j < m; j++)
+                use[i][j] = false;
+        for (int i = 0; i < n; i++) {
+            for (int j = 0; j < m; j++) {
+                cin >> s[i][j];
+                if (s[i][j] == 'S') {
+                    Startx = i;
+                    Starty = j;
+                }
+            }
+        }
+        dfs(Startx, Starty);
+        flag ? cout << "I saved him." << endl : cout << "Sorry, I can't save him." << endl;
+    }
+}

+ 62 - 0
L.cpp

@@ -0,0 +1,62 @@
+#include<bits/stdc++.h>
+
+using namespace std;
+int a[500][500];
+int n, m;
+
+void input() {
+    cin >> m >> n;
+    swap(n, m);
+    for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)cin >> a[i][j];
+}
+
+bool ok() {
+    for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (a[i][j])return false;
+    return true;
+}
+
+void solve() {
+    input();
+    int areas = 0;
+    while (!ok()) {
+        queue<pair<int, int>> Q;//创建一个队列
+        ///Q.push(make_pair(0,0));//起点加入队列
+        //a[0][0]=0;
+        int X = 0, Y = 0;
+        for (int i = 0; i < n; i++)
+            for (int j = 0; j < m; j++)
+                if (a[i][j]) {
+                    X = i;
+                    Y = j;
+                    break;//遇到1就停下
+                }
+        Q.push(make_pair(X, Y));
+        a[X][Y] = 0;
+        while (Q.size()) {
+            pair<int, int> cur = Q.front();//获得队列的第一个元素
+            int x = cur.first, y = cur.second;
+            for (int dx = -1; dx <= 1; ++dx)
+                for (int dy = -1; dy <= 1; ++dy) { //按照宽度去扩展
+                    if (abs(dx - dy) != 1)continue;
+                    int nx = x + dx, ny = y + dy;
+                    if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1 || a[nx][ny] == 0)continue;
+                    Q.push(make_pair(nx, ny));
+                    a[nx][ny] = 0;
+                }
+            //cout<<cur.first<<cur.second<<endl;//计数
+            Q.pop();//出队
+        }
+        ++areas;
+    }
+    cout << areas << endl;
+}
+
+int main() {
+    //freopen("b.in.cpp","r",stdin);
+    int t;
+    cin >> t;
+    while (t--) {
+        solve();
+    }
+    return 0;
+}